文章目錄
- 前言
- 一、準備工作
- 二、實作類源碼
-
- 1.
- 2.展示
- 總結
前言
本文提供了一些利用Java設計Excel表格的參數,自定義設定需求表格,話不多說,直接上代碼
一、準備工作
這裡可以參考作者的上一篇文章,導入poi的包就可以。
自帶詳細注釋,小白也能看懂。
二、實作類源碼
1.
代碼如下(示例):
//todo
//查詢資料
List<HospitalExcel> list = new ArrayList<>();
HospitalExcel hospitalExcel1 = new HospitalExcel();
HospitalExcel hospitalExcel2 = new HospitalExcel();
hospitalExcel1.setHospName("11111");
hospitalExcel1.setCCityName("兒童醫院");
hospitalExcel2.setHospName("1");
list.add(hospitalExcel1);
list.add(hospitalExcel2);
try {
//建立工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("工作薄");//工作薄名稱
//設定每格資料的樣式
HSSFFont ParamFontStyle = workbook.createFont();
CellStyle cellParamStyle = workbook.createCellStyle();
cellParamStyle.setAlignment(HorizontalAlignment.CENTER);//垂直居中
cellParamStyle.setVerticalAlignment(VerticalAlignment.CENTER);//水準居中
cellParamStyle.setWrapText(false);//自動換行
ParamFontStyle.setFontHeightInPoints((short) 13);//字型大小
ParamFontStyle.setFontName("等線");
cellParamStyle.setFont(ParamFontStyle);
//設定表頭的樣式
HSSFFont ParamFontStyle1 = workbook.createFont();
CellStyle cellParamStyle1 = workbook.createCellStyle();
cellParamStyle1.setAlignment(HorizontalAlignment.LEFT);
cellParamStyle1.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
cellParamStyle1.setWrapText(false);//自動換行
ParamFontStyle1.setFontHeightInPoints((short) 15);
ParamFontStyle1.setFontName("黑體");
ParamFontStyle1.setBold(true);//是否打開加粗
cellParamStyle1.setFont(ParamFontStyle1);
//設定标題的樣式
HSSFFont ParamFontStyle2 = workbook.createFont();
CellStyle cellParamStyle2 = workbook.createCellStyle();
cellParamStyle2.setAlignment(HorizontalAlignment.CENTER);//垂直居中
cellParamStyle2.setVerticalAlignment(VerticalAlignment.CENTER);//水準居中
cellParamStyle2.setWrapText(true);//自動換行
ParamFontStyle2.setFontHeightInPoints((short) 14);
ParamFontStyle2.setFontName("黑體");
ParamFontStyle2.setBold(true);
cellParamStyle2.setFont(ParamFontStyle2);
//定義列的寬度
//sheet.setDefaultColumnWidth(40 * 1024);預設寬度
sheet.setColumnWidth(0,888);
sheet.setColumnWidth(1,5000);
sheet.setColumnWidth(2,5000);
sheet.setColumnWidth(3,5000);
sheet.setColumnWidth(4,5000);
sheet.setColumnWidth(5,5000);
sheet.setColumnWidth(6,5000);
sheet.setColumnWidth(7,5000);
sheet.setColumnWidth(8,5000);
sheet.setColumnWidth(9,5000);
sheet.setColumnWidth(10,5000);
//設定表頭
HSSFRow rows = sheet.createRow(0);
HSSFCell cell = rows.createCell(0);//一列的第一個單元格(下面的類似)
HSSFRow rows1 = sheet.createRow(1);
HSSFCell cell1 = rows1.createCell(0);
HSSFRow rows2 = sheet.createRow(2);
HSSFCell cell2 = rows2.createCell(0);
HSSFRow rows3 = sheet.createRow(3);
HSSFCell cell3 = rows3.createCell(0);
//合并單元格
CellRangeAddress region = new CellRangeAddress(0, 3, 5, 10);
sheet.addMergedRegion(region);
CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 4);
sheet.addMergedRegion(region1);
CellRangeAddress region2 = new CellRangeAddress(1, 1, 0, 4);
sheet.addMergedRegion(region2);
CellRangeAddress region3 = new CellRangeAddress(2, 2, 0, 4);
sheet.addMergedRegion(region3);
CellRangeAddress region4 = new CellRangeAddress(3, 3, 0, 4);
sheet.addMergedRegion(region4);
cell.setCellStyle(cellParamStyle1);
cell1.setCellStyle(cellParamStyle1);
cell2.setCellStyle(cellParamStyle1);
cell3.setCellStyle(cellParamStyle1);
cell.setCellValue("緻:" );
cell1.setCellValue("号碼:111111111111111111111");
cell2.setCellValue("金額:1111111111111111111");
cell3.setCellValue("日期:11111111111111111111");//填入值
//列名
HSSFRow row1 = sheet.createRow(4);
HSSFCell row1Cell0 = row1.createCell(0);
row1Cell0.setCellValue("序号");
row1Cell0.setCellStyle(cellParamStyle1);
HSSFCell row1Cell = row1.createCell(1);
row1Cell.setCellValue("=案件号 Claim");
row1Cell.setCellStyle(cellParamStyle2);
HSSFCell row1Cell1 = row1.createCell(2);
row1Cell1.setCellValue("=公司 Insurer");
row1Cell1.setCellStyle(cellParamStyle2);
HSSFCell row1Cell2 = row1.createCell(3);
row1Cell2.setCellValue("會員号 Membership");
row1Cell2.setCellStyle(cellParamStyle2);
HSSFCell row1Cell3 = row1.createCell(4);
row1Cell3.setCellValue("姓名 Patient's Name");
row1Cell3.setCellStyle(cellParamStyle2);
HSSFCell row1Cell4 = row1.createCell(5);
row1Cell4.setCellValue("日期 Service Date");
row1Cell4.setCellStyle(cellParamStyle2);
HSSFCell row1Cell5 = row1.createCell(6);
row1Cell5.setCellValue("号碼 Invoice");
row1Cell5.setCellStyle(cellParamStyle2);
HSSFCell row1Cell6 = row1.createCell(7);
row1Cell6.setCellValue("金額 Amount Billed");
row1Cell6.setCellStyle(cellParamStyle2);
HSSFCell row1Cell7 = row1.createCell(8);
row1Cell7.setCellValue("付金額 Amount Paid");
row1Cell7.setCellStyle(cellParamStyle2);
HSSFCell row1Cell8 = row1.createCell(9);
row1Cell8.setCellValue("付金額 Amount Rejected");
row1Cell8.setCellStyle(cellParamStyle2);
HSSFCell row1Cell9 = row1.createCell(10);
row1Cell9.setCellValue("付原因 Explanation");
row1Cell9.setCellStyle(cellParamStyle2);
//放入圖檔
BufferedImage bufferImg = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
InputStream is = this.getClass().getResourceAsStream("/e6ab3c4617327341fdc1dc6eb355c26.jpg");//擷取圖檔。本文放在resources下
bufferImg = ImageIO.read(is);
ImageIO.write(bufferImg, "jpg", byteArrayOut);
//畫圖的頂級管理器,一個sheet隻能擷取一個
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于設定圖檔的屬性
HSSFClientAnchor anchor = new HSSFClientAnchor(150, 220, 1023, 0, (short) 9, 0, (short) 10, 3);
anchor.setAnchorType(ClientAnchor.AnchorType.byId(3));
//插入圖檔
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
//資料存入表格
int sheet1Colume = 0;
for (HospitalExcel hospitalExcel : list) {
String uname = hospitalExcel.getHospName();
HSSFRow row2 = sheet.createRow(sheet1Colume + 5);
HSSFCell row2Cell = row2.createCell(0);
row2Cell.setCellStyle(cellParamStyle);
row2Cell.setCellValue(sheet1Colume + 1);//序号
HSSFCell row2Cell1 = row2.createCell(1);
row2Cell1.setCellStyle(cellParamStyle);
row2Cell1.setCellValue(uname);
HSSFCell row2Cell2 = row2.createCell(2);
row2Cell2.setCellStyle(cellParamStyle);
row2Cell2.setCellValue("uid");
HSSFCell row2Cell3 = row2.createCell(3);
row2Cell3.setCellStyle(cellParamStyle);
row2Cell3.setCellValue("inputs");
HSSFCell row2Cell4 = row2.createCell(4);
row2Cell4.setCellStyle(cellParamStyle);
row2Cell4.setCellValue("handles");
HSSFCell row2Cell5 = row2.createCell(5);
row2Cell5.setCellStyle(cellParamStyle);
row2Cell5.setCellValue("overs");
HSSFCell row2Cell6 = row2.createCell(6);
row2Cell6.setCellStyle(cellParamStyle);
row2Cell6.setCellValue("overs");
HSSFCell row2Cell7 = row2.createCell(7);
row2Cell7.setCellStyle(cellParamStyle);
row2Cell7.setCellValue("overs");
HSSFCell row2Cell8 = row2.createCell(8);
row2Cell8.setCellStyle(cellParamStyle);
row2Cell8.setCellValue("overs");
HSSFCell row2Cell9 = row2.createCell(9);
row2Cell9.setCellStyle(cellParamStyle);
row2Cell9.setCellValue("overs");
HSSFCell row2Cell10 = row2.createCell(10);
row2Cell10.setCellStyle(cellParamStyle);
row2Cell10.setCellValue("overs");
sheet1Colume++;
}
String fileName = new String("eob".getBytes(), "UTF-8") + ".xls";
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setCharacterEncoding("UTF-8");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return new Result<>(ResultEnum.OK);
} catch (Exception ex) {
ex.printStackTrace();
}
2.展示
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP35kMjRlTzUEVOBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLzQTN0MDO0QTM4AzNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
總結
本文作者用作自己練習和記錄,希望對你有幫助,共同學習。