POI创建表格 工作簿 单元格样式
- 注入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16-beta2</version>
</dependency>
//创建工作簿
Workbook wb = new HSSFWorkbook();
//在工作簿中创建工作表,工作表默认名为sheet0,参数为工作表名称
Sheet sheet = wb.createSheet("导出信息");
· //合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 7, 8));
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));
sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 8));
sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 3));
sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 8));
sheet.addMergedRegion(new CellRangeAddress(5, 6, 0, 1));
sheet.addMergedRegion(new CellRangeAddress(5, 6, 2, 3));
sheet.addMergedRegion(new CellRangeAddress(5, 6, 6,7 ));
sheet.addMergedRegion(new CellRangeAddress(5, 5, 4, 5));
··
就是这种效果啦!
//设置字体
Font cellfont = wb.createFont();
cellfont.setBold(true);//是否粗体
cellfont.setFontHeightInPoints((short) 14);//字体大小
Font titlefont = wb.createFont();
titlefont.setFontHeightInPoints((short) 12);//字体大小
//水平居中 标题
CellStyle cellStyle = wb.createCellStyle();
//水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setFont(cellfont);
//水平居中
CellStyle titleStyle = wb.createCellStyle();
//水平居中
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setFont(titlefont);
不要在意那么多细节 名字懒得换了
- valueMap 这个是 获取到的数据 上面还有其他方法 没写
int allsun=0;
for (int i = 0; i < result.size(); i++) {
Map<String, Object> valueMap = result.get(i);
//创建一个行 1
Row row1 = sheet.createRow(0);
//创建一个单元格
Cell cell1 = row1.createCell(0);
//设置单元格的值
cell1.setCellValue("标题名字");
cell1.setCellStyle(cellStyle);
//创建一个行 2
Row row2 = sheet.createRow(1);
//创建一个单元格
Cell cell2 = row2.createCell(0);
//设置单元格的值
cell2.setCellValue("属性1");
cell2.setCellStyle(titleStyle);
Cell cell19 = row2.createCell(1);
//设置单元格的值
cell19.setCellValue((String) valueMap.get("shuxing_!"));
cell19.setCellStyle(titleStyle);
if (cell19 != null) {
Cell cell3 = row2.createCell(3);
//设置单元格的值
cell3.setCellValue("属性2");
cell3.setCellStyle(titleStyle);
Cell cell26 = row2.createCell(4);
//设置单元格的值
cell26.setCellValue((String) valueMap.get("shuxing_2"));
cell26.setCellStyle(titleStyle);
Cell cell4 = row2.createCell(6);
//设置单元格的值
cell4.setCellValue("属性3");
cell4.setCellStyle(titleStyle);
Cell cell30 = row2.createCell(7);
//循环增加数量
String toolNum= (String) valueMap.get("sguxing_3");
allsun=Integer.parseInt(toolNum)+allsun;
//设置单元格的值
cell30.setCellValue(allsun);
cell30.setCellStyle(titleStyle);
//创建一个行 3
Row row3 = sheet.createRow(2);
//创建一个单元格
Cell cell5 = row3.createCell(0);
//设置单元格的值
cell5.setCellValue("属性5");
cell5.setCellStyle(titleStyle);
Cell cell31 = row3.createCell(1);
//设置单元格的值
cell31.setCellValue((String) valueMap.get("shuxing5"));
cell31.setCellStyle(titleStyle);
Cell cell6 = row3.createCell(4);
//设置单元格的值
cell6.setCellValue("属性6");
cell6.setCellStyle(titleStyle);
Cell cell32 = row3.createCell(5);
//设置单元格的值
cell32.setCellValue((String) valueMap.get("shuxing6"));
cell32.setCellStyle(titleStyle);
//创建一个行 4
Row row8 = sheet.createRow(3);
//创建一个单元格
Cell cel36 = row8.createCell(0);
//设置单元格的值
cel36.setCellValue("属性7");
cel36.setCellStyle(titleStyle);
Cell cell37 = row8.createCell(1);
//设置单元格的值
cell37.setCellValue((String) valueMap.get("shuxing7"));
cell37.setCellStyle(titleStyle);
Cell cel38 = row8.createCell(4);
//设置单元格的值
cel38.setCellValue("属性8");
cel38.setCellStyle(titleStyle);
Cell cell39 = row8.createCell(5);
//设置单元格的值
cell39.setCellValue((String) valueMap.get("shuxing8"));
cell39.setCellStyle(titleStyle);
//创建一个行 4
Row row4 = sheet.createRow(4);
//创建一个单元格
Cell cell7 = row4.createCell(0);
//设置单元格的值
cell7.setCellValue("日期");
cell7.setCellStyle(titleStyle);
//创建一个单元格
Cell cell33 = row4.createCell(1);
//设置单元格的值
cell33.setCellValue(valueMap.get("jdate").toString());
cell33.setCellStyle(titleStyle);
Cell cell8 = row4.createCell(2);
//设置单元格的值
cell8.setCellValue("属性9");
cell8.setCellStyle(titleStyle);
//创建一个单元格
Cell cell40 = row4.createCell(3);
//设置单元格的值
cell40.setCellValue("属性10");
cell40.setCellStyle(titleStyle);
//创建一个单元格
Cell cell41 = row4.createCell(4);
//设置单元格的值
cell41.setCellValue((String) valueMap.get("shuxing10"));
cell41.setCellStyle(titleStyle);
//创建一个单元格
Cell cell9 = row4.createCell(5);
//设置单元格的值
cell9.setCellValue("属性11");
cell9.setCellStyle(titleStyle);
//创建一个单元格
Cell cell34 = row4.createCell(6);
//设置单元格的值
cell34.setCellValue((String) valueMap.get("属性11"));
cell34.setCellStyle(titleStyle);
Cell cell10 = row4.createCell(7);
//设置单元格的值
cell10.setCellValue("属性12");
cell10.setCellStyle(titleStyle);
//创建一个单元格
Cell cell35 = row4.createCell(8);
//设置单元格的值
cell35.setCellValue((String) valueMap.get("shuxing 12"));
cell35.setCellStyle(titleStyle);
Cell cell11 = row4.createCell(9);
//设置单元格的值
cell11.setCellValue("属性13");
cell11.setCellStyle(titleStyle);
//创建一个单元格
Cell cell36 = row4.createCell(10);
//设置单元格的值
cell36.setCellValue((String) valueMap.get("shuxing13"));
cell36.setCellStyle(titleStyle);
//创建一个行 4
Row row5 = sheet.createRow(5);
Cell cell12 = row5.createCell(0);
//设置单元格的值
cell12.setCellValue("时间");
cell12.setCellStyle(titleStyle);
Cell cell13 = row5.createCell(2);
//设置单元格的值
cell13.setCellValue("姓名");
cell13.setCellStyle(titleStyle);
Cell cell14 = row5.createCell(4);
//设置单元格的值
cell14.setCellValue("状态");
cell14.setCellStyle(titleStyle);
Cell cell17 = row5.createCell(6);
//设置单元格的值
cell17.setCellValue("时间");
cell17.setCellStyle(titleStyle);
Cell cell18 = row5.createCell(8);
//设置单元格的值
cell18.setCellValue("姓名");
cell18.setCellStyle(titleStyle);
//创建一个行 5 状态 下面的两个单元格
Row row6 = sheet.createRow(6);
Cell cell15 = row6.createCell(4);
//设置单元格的值
cell15.setCellValue("可用");
cell15.setCellStyle(titleStyle);
Cell cell16 = row6.createCell(5);
//设置单元格的值
cell16.setCellValue("不可");
cell16.setCellStyle(titleStyle);
}
//创建一个行 循环输出第八行之后的数据
Row row7 = sheet.createRow(7 + i);
Date create_date = (Date) valueMap.get("date");
Cell cell20 = row7.createCell(0);
//设置单元格的值
cell20.setCellValue(create_date.toString());
cell20.setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(7 + i, 7 + i, 0, 1));//增加数据之后合并单元格
String ws_s_name = (String) valueMap.get("name");
Cell cell21 = row7.createCell(2);
//设置单元格的值
cell21.setCellValue(name);//赋值
cell21.setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(7 + i, 7 + i, 2, 3));//增加数据之后合并单元格
String zhuang_tai = (String) valueMap.get("zhuangtai");
if (zhuang_tai.equals("可用")) {
Cell cell22 = row7.createCell(4);
//设置单元格的值
cell22.setCellValue(zhuangtai);
cell22.setCellStyle(titleStyle);
} else {
Cell cell23 = row7.createCell(5);
//设置单元格的值
cell23.setCellValue(zhuangtai);
cell23.setCellStyle(titleStyle);
}
Date date = (Date) valueMap.get("date");
Cell cell24 = row7.createCell(6);
//设置单元格的值
if (date == null || date.equals("")) {
cell24.setCellValue(" ");
sheet.addMergedRegion(new CellRangeAddress(7 + i, 7 + i, 6, 7));//增加数据之后合并单元格
} else {
cell24.setCellValue(date.toString());
sheet.addMergedRegion(new CellRangeAddress(7 + i, 7 + i, 6, 7));//增加数据之后合并单元格
}
cell24.setCellStyle(titleStyle);
String name = (String) valueMap.get("ren");
Cell cell29 = row7.createCell(8);
//设置单元格的值
cell29.setCellValue(name);
cell29.setCellStyle(titleStyle);
}
response.setHeader("content-disposition", "attachment;filename="+new String(codedFileName.getBytes("GBK"),"ISO-8859-1") + ".xls" );
ServletOutputStream out = response.getOutputStream();
wb.write(out);
//关闭输出流
out.close();
*这样就完事啦 *