版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/81742205
最新poi的jar包。与之前版本存在样式设置区别
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
//创建工作簿workbook
//利用工作簿对象创建工作表sheet
//利用工作表对象创建行row
//利用行对象创建单元格对象cell
HSSFWorkbook wb= new HSSFWorkbook(); //创建工作薄
HSSFSheet sheet=wb.createSheet(); //利用工作薄创建工作表
// 表头样式
HSSFFont font = wb.createFont();//创建字体对象
font.setFontHeightInPoints((short) 24);
HSSFCellStyle cellstyle=wb.createCellStyle();
cellstyle.setBorderBottom(BorderStyle.THIN);//下边框
cellstyle.setBorderLeft(BorderStyle.THIN);//左边框
cellstyle.setBorderTop(BorderStyle.THIN);//上边框
cellstyle.setBorderRight(BorderStyle.THIN);//右边框
cellstyle.setAlignment(HorizontalAlignment.CENTER);//设置字体居中
cellstyle.setFont(font);
// 字段名样式
HSSFFont font2 = wb.createFont();//创建字体对象
font2.setFontHeightInPoints((short) 16);
HSSFCellStyle cellstyle1=wb.createCellStyle();
cellstyle1.setBorderBottom(BorderStyle.THIN);//下边框
cellstyle1.setBorderLeft(BorderStyle.THIN);//左边框
cellstyle1.setBorderTop(BorderStyle.THIN);//上边框
cellstyle1.setBorderRight(BorderStyle.THIN);//右边框
cellstyle1.setAlignment(HorizontalAlignment.CENTER);
cellstyle1.setFont(font2);
// 列表样式
HSSFFont font1 = wb.createFont();//创建字体对象
font1.setFontHeightInPoints((short) 12);
HSSFCellStyle cellstyle2=wb.createCellStyle();
cellstyle2.setBorderBottom(BorderStyle.THIN);//下边框
cellstyle2.setBorderLeft(BorderStyle.THIN);//左边框
cellstyle2.setBorderTop(BorderStyle.THIN);//上边框
cellstyle2.setBorderRight(BorderStyle.THIN);//右边框
cellstyle2.setFont(font1);
HSSFRow row = null;//创建行对象
HSSFCell cell =null;//创建列对象
CellRangeAddress cellRangeAddress =null;// new CellRangeAddress(0, 0, 0, 11);
String yuanShi[] = new String[]{"序号","地区","部门","名称","编码","类型","创建时间"};
// 合并单元格 前两个参数行 后两个列
sheet.addMergedRegion(new CellRangeAddress(0,0,0, 6));//合并单元格
row = sheet.createRow((short)0);//通过工作薄创建行对象
cell = row.createCell((short)0);//通过行对象获得列对象
cell.setCellValue("事项清单列表");//设置列的值
cell.setCellStyle(cellstyle);//设置表格样式
sheet.setDefaultRowHeight((short)30);
//每次
row = sheet.createRow((short)1);//新建行对象
for(int s=0;s<yuanShi.length;s++){
cell = row.createCell((short)s);
cell.setCellValue(yuanShi[s]);
cell.setCellStyle(cellstyle1);
sheet.setColumnWidth((short)0,(short)(15 * 300));
}
for(int j=0;j<object.size();j++){
Object[] obj = object.get(j);
row = sheet.createRow((short)j+2);
cell = row.createCell((short)0);//创建列
cell.setCellValue(j+1);//序号
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)0,(short)(15 * 150));
cell = row.createCell((short)1);
cell.setCellValue(obj[0].toString());//地区
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)1,(short)(15 * 300));
cell = row.createCell((short)2);
cell.setCellValue(obj[1].toString());//部门
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)2,(short)(15 * 300));
cell = row.createCell((short)3);
cell.setCellValue(obj[2].toString());//名称
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)3,(short)(20 * 300));
cell = row.createCell((short)4);
cell.setCellValue(obj[4].toString());//编码
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)4,(short)(15 * 300));
cell = row.createCell((short)5);
cell.setCellValue(obj[5].toString());//类型
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)5,(short)(15 * 300));
cell = row.createCell((short)6);
cell.setCellValue(obj[3].toString());//创建时间
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)6,(short)(30 * 300));
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
// 第六步,将文件存到指定位置
boolean t =false;
try {
ServletOutputStream servletoutputstream = rep.getOutputStream();
String fileName = "";
if(req.getHeader("user-agent").indexOf("MSIE") != -1 || req.getHeader("user-agent").indexOf("rv:11") !=-1) {
fileName = java.net.URLEncoder.encode("导出","utf-8") + ".xls";
}else{
fileName = new String("导出".getBytes("utf-8"),"iso-8859-1")+ ".xls";
}
rep.setHeader("Content-disposition", "attachment; filename="+ fileName);
rep.setContentType("application/vnd.ms-excel;charset=utf-8");
wb.write(servletoutputstream);
servletoutputstream.flush();
t =true;
servletoutputstream.close();
}
catch (Exception e) {
e.printStackTrace();
}
return t;