POI报表生成(HSSF)
java实现Excel数据导出:
HSSF是POI项目对Excel '97(-2007)文件格式的纯Java实现。XSSF是POI项目对Excel 2007 OOXML(.xlsx)文件格式的纯Java实现。
Jakarta POI 是一套用于访问微软格式文档的Java API。Jakarta POI有很多组件组成,其中有用于操作Excel格式文件的HSSF和用于操作Word的HWPF,在各种组件中目前只有用于操作Excel的HSSF相对成熟。官方主页,API文档
功能(部分官网文档)
主要API
版本控制
<dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.10</version></dependency><dependency><groupId>info.folone</groupId><artifactId>poi-scala_2.9.3</artifactId><version>0.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency>
生成报表
//创建HSSFWorkbook对象 设置工作薄HSSFWorkbook hswb = new HSSFWorkbook();//设置单元格样式HSSFCellStyle titleCellStyle = hswb.createCellStyle();//设置内容居中titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);titleCellStyle.setWrapText(true);// 自动换行titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中titleCellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());// 设置单元格的背景颜色titleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);titleCellStyle.setBorderTop((short) 1);// 边框的大小titleCellStyle.setBorderBottom((short) 1);titleCellStyle.setBorderLeft((short) 1);titleCellStyle.setBorderRight((short) 1);//设置字体HSSFFont titleFont = hswb.createFont();titleFont.setFontName("宋体" );titleFont.setFontHeightInPoints((short) 11);titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗titleCellStyle.setFont(titleFont); //将字体注入//创建HSSFSheet对象(第一个sheet表格)HSSFSheet sheet = hswb.createSheet("工作表");//设置固定列宽int []cloumnWidth = {
13,21,21,16,16,21,19,22,17};for(int c = 0;c < cloumnWidth.length ;c++){
sheet.setColumnWidth(c, 255*cloumnWidth[c]);}/**sheet.autoSizeColumn((short) 0); 设置自动列宽会根据设定列宽的上面最后一条写入的数据设定列宽*///在sheet里创建第一行 (标题行)HSSFRow row=sheet.createRow(0);row.setRowStyle(titleCellStyle);row.setHeight((short) 800);// 设定行的高度//5、创建row中的单元格,从0开始//创建单元格并按行设置单元格内容row.createCell(0).setCellValue("Sub Brunch"); //AHSSFCell cell = row.createCell(0);//我们第一列设置宽度为0,不会显示,因此第0个单元格不需要设置样式cell = row.createCell(1);//从第1个单元格开始,设置每个单元格样式cell.setCellValue("x");//设置单元格中内容cell.setCellStyle(titleCellStyle);//设置单元格样式cell = row.createCell(2);//第二个单元格cell.setCellValue("y");cell.setCellStyle(titleCellStyle);cell = row.createCell(3);//第三个单元格cell.setCellValue("value");cell.setCellStyle(titleCellStyle);//6、输入数据(单元格)for(int i = 1; i <= list.size(); i++){
cell = row.createCell(i);cell.setCellValue("value");}//单元格合并,有两种方式1、sheet.addMergedRegion(new Region(1,(short)1,1,(short)11));//参数(第一行,最后一行,第一列,最后一列)2、sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1));//参数(第一行,最后一行,第一列,最后一列)//保存到本地路径try {
//判断文件夹是否存在 不存在创建isChartPathExist(agingReport.getFilePath());FileOutputStream out = new FileOutputStream(agingReport.getFilePath()+agingReport.getId()+".xls");hswb.write(out);out.close();logger.info((new Date()).toString() + "报告已生成!");} catch (Exception e) {
e.printStackTrace();}
//判断文件夹是否存在,如果不存在则新建private static void isChartPathExist(String dirPath) {
File file = new File(dirPath);if (!file.exists()) {
file.mkdirs();}}
直接服务器下载
// 输出Excel文件OutputStream output = response.getOutputStream();//设置响应头response.reset();response.setHeader("Content-disposition", "attachment;filename="+ new String(("下载"+fileName+ ".xls").getBytes(), "ISO-8859-1"));response.setContentType("application/msexcel;charset=GBK");hswb.write(output);output.close();
读取指定路径下载
// 输出Excel文件OutputStream output = response.getOutputStream();response.reset();response.reset();response.setHeader("Content-disposition", "attachment;filename="+ new String(("下载" +fileName+".xls").getBytes(), "ISO-8859-1"));response.setContentType("application/msexcel;charset=GBK");HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelList.get(0).getFilePath()));wb.write(output);output.close();
前端请求
请求文件路径不能使用AJAX
直接GET请求路径下载
location.href = '/pad_marketing_manage/agingReport//downExcel?list=' +id;