标签:date 工作 基本 就是 客户 字段名 merge ipa str
最近做了poi模块,总结一下。
poi是常用的报表导出工具,一般业务需求基本都可以满足,我们第一步导入基本的jar包,这里导入的是3.10的版本,不是最新的。
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency>
然后基本的导出代码如下:
public HSSFWorkbook export() throws Exception { // 创建新工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle cellStyle = titlStyle(workbook); //bigdataip=192.168.100.104, qxname=wwwwwww, port=9595, totalNum=210217 // 创建表头以及字段名称 HSSFSheet sheet = workbook.createSheet(name); sheet.setColumnWidth(0, 256*15); sheet.setColumnWidth(1, 256*30); sheet.setColumnWidth(2, 256*18); sheet.setColumnWidth(3, 256*18); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10)); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue( ""); cell.setCellStyle(cellStyle); HSSFCellStyle cellStyleNei = workbook.createCellStyle(); // 新建font实体 HSSFFont hssfFontNei = workbook.createFont(); // 字体大小 hssfFontNei.setFontHeightInPoints((short) 14); hssfFontNei.setFontName("宋体"); // 粗体 hssfFontNei.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyleNei.setFont(hssfFontNei); HSSFRow row1 = sheet.createRow(1); HSSFCell cell1 = row1.createCell(0); cell1.setCellValue("xxx"); cell1.setCellStyle(cellStyleNei); HSSFCell cell2 = row1.createCell(1); cell2.setCellValue("xxx"); cell2.setCellStyle(cellStyleNei); HSSFCell cell3 = row1.createCell(2); cell3.setCellValue("xxx"); cell3.setCellStyle(cellStyleNei); HSSFCell cell4 = row1.createCell(3); cell4.setCellValue("xxx"); cell4.setCellStyle(cellStyleNei); HSSFCellStyle cellStyleNeiR = workbook.createCellStyle(); // 新建font实体 HSSFFont hssfFontNeiR = workbook.createFont(); // 字体大小 hssfFontNeiR.setFontHeightInPoints((short) 12); hssfFontNeiR.setFontName("宋体"); // 粗体 hssfFontNeiR.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyleNeiR.setFont(hssfFontNeiR); for(int i =0 ;i<dataList.size();i++) { HSSFRow rowf = sheet.createRow(i+2); HSSFCell cellf1 = rowf.createCell(0); cellf1.setCellValue(dataList.get(i).get("key值")); cellf1.setCellStyle(cellStyleNeiR); HSSFCell cellf2 = rowf.createCell(1); cellf2.setCellValue(dataList.get(i).get("key值")); cellf2.setCellStyle(cellStyleNeiR); HSSFCell cellf3 = rowf.createCell(2); cellf3.setCellValue(dataList.get(i).get("key值")); cellf3.setCellStyle(cellStyleNeiR); HSSFCell cellf4 = rowf.createCell(3); cellf4.setCellValue(dataList.get(i).get("key值")); cellf4.setCellStyle(cellStyleNeiR); } // 输出到磁盘中 放开这段代码可以直接将excel导出到桌面 /*String ip_yh = tools.getIpAddress(request); FileSystemView fsv = FileSystemView.getFileSystemView(); File com=fsv.getHomeDirectory(); String cdopt = com.getPath(); cdopt =cdopt.replaceAll("\\\\","/"); File file = new File(cdopt +"/"+ name +ip_yh+ "数据统计.xls" ); FileOutputStream fos = new FileOutputStream(file); workbook.write(fos); fos.close();*/ return workbook ; }
然后我们写servlet:
@RequestMapping(value = "/cprint") @ResponseBody public void download(HttpServletRequest request, HttpServletResponse response) { poiExcle poiexcle = new poiExcle(); try { wb = poiexcle.export(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { String fileName = "数据统计.xls"; response.setContentType("application/octet-stream"); response.setHeader("name", fileName); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setDateHeader("Expires", 0); response.setHeader("Content-disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); wb.write(response.getOutputStream()); // 输出流控制workbook response.getOutputStream().flush(); response.getOutputStream().close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
最主要的代码就是这一段:
response.setContentType("application/octet-stream"); response.setHeader("name", fileName); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setDateHeader("Expires", 0); response.setHeader("Content-disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); wb.write(response.getOutputStream()); // 输出流控制workbook response.getOutputStream().flush(); response.getOutputStream().close();
将 HSSFWorkbook 对象写到 response里的输出流,一定要记得将 filename 设置一下编码,不然会显示 ____.xls
ps.刚实习,时间不太够,先这样简单记录一下,后面慢慢整理。
poi 制作 excel 并且以文件流的方式 传输到客户端下载
标签:date 工作 基本 就是 客户 字段名 merge ipa str
原文地址:https://www.cnblogs.com/aresblank/p/9412852.html