Java用POI往execl表格中写数据,并下载下来有两种方式
1、用poil的API创建表格,并设计好表格格式,然后往里面写数据
1 /** 2 * execl导出 创建表格式,并写入数据 3 * @param sheetNmae sheet名 4 * @param tiltle 表头 5 * @param values 内容 6 * @param workbook 7 * @return 8 */ 9 public static HSSFWorkbook getHSSFWorkbook(String sheetNmae, 10 String []tiltle,String[][] values,HSSFWorkbook workbook){ 11 12 //创建一个Hssfworkbook 对应一个execl文件 13 if(workbook==null){ 14 workbook=new HSSFWorkbook(); 15 } 16 //在workbook中添加一个sheet,对应execl文件中的sheet 17 HSSFSheet sheet=workbook.createSheet(sheetNmae); 18 sheet.setDefaultColumnWidth((short)13); 19 //在sheet中添加表头第0行 20 HSSFRow row=sheet.createRow(0); 21 //创建单元格,设置表头 22 HSSFCellStyle style=(HSSFCellStyle)workbook.createCellStyle(); 23 style.setBorderTop(BorderStyle.THIN); 24 style.setBorderBottom(BorderStyle.THIN); 25 style.setBorderLeft(BorderStyle.THIN); 26 style.setBorderRight(BorderStyle.THIN); 27 //字体样式 28 HSSFFont font=workbook.createFont(); 29 font.setFontName(\"微软雅黑\"); 30 style.setFont(font); 31 32 //声明列对象 33 HSSFCell cell=null; 34 35 //创建标题 36 for(int i=0;i<tiltle.length;i++){ 37 cell=row.createCell(i); 38 cell.setCellValue(tiltle[i]); 39 cell.setCellStyle(style); 40 } 41 //创建内容 42 //声明列对象 43 HSSFCell cell2=null; 44 for(int i=0;i<values.length;i++){ 45 row = sheet.createRow(i + 1); 46 for(int j=0;j<values[i].length;j++){ 47 //将内容按顺序赋给对应的列对象 48 cell2=row.createCell(j); 49 cell2.setCellValue(values[i][j]); 50 cell2.setCellStyle(style); 51 } 52 } 53 54 return workbook; 55 } 56
1 /** 2 * 3 * @param response 4 * @param companyCostVo 前端传过来的对象 5 * @throws Exception 6 */ 7 @RequestMapping(\"/download\") 8 public void download(HttpServletResponse response, CompanyCostVo companyCostVo) throws Exception { 9 10 Integer year=Integer.valueOf(redisService.valueOperations().get(\"audityear\").toString()); 11 companyCostVo.setYear(year); 12 //获取数据 13 List<CompanyCostVo> companyCostVos=companyCostService.selectCompanyCost(companyCostVo); 14 //exexl标题 15 String[] ={\"序号\",\"公司名称\",\"公交总车辆数\",\"公交收费车辆数\",\"公交收费单价\",\"公交实收金额\",\"公交应收金额\", 16 \"农客总车辆数\",\"农客收费车辆数\",\"农客收费单价\",\"农客实收金额\",\"农客应收金额\",\"总实收金额\",\"总应收金额\"}; 17 //execl文件名 18 String fileName=year+\"年企业审计费用清算信息表.xls\"; 19 //sheetming 20 String sheetName=\"费用清算信息\"; 21 int len=companyCostVos.size(); 22 String [][] content=new String[len][14]; 23 for(int i=0;i<len;i++){ 24 //content[i]=new String[len]; 25 CompanyCostVo cCostVo=companyCostVos.get(i); 26 content[i][0]=(i+1)+\"\"; 27 content[i][1]=cCostVo.getCompanyName(); 28 content[i][2]=\"无\"; 29 if(cCostVo.getVehicleNumGj()!=null){ 30 content[i][2]=cCostVo.getVehicleNumGj().toString(); 31 } 32 content[i][3]=\"无\"; 33 if(cCostVo.getVehicleNumChargGj()!=null){ 34 content[i][3]=cCostVo.getVehicleNumChargGj().toString(); 35 } 36 content[i][4]=\"无\"; 37 if(cCostVo.getUnitPriceGj()!=null){ 38 content[i][4]=cCostVo.getUnitPriceGj().toString(); 39 } 40 content[i][5]=\"无\"; 41 if(cCostVo.getAccountAmountGj()!=null){ 42 content[i][5]=cCostVo.getAccountAmountGj().toString(); 43 } 44 content[i][6]=\"无\"; 45 if(cCostVo.getAccountPayableGj()!=null){ 46 content[i][6]=cCostVo.getAccountPayableGj().toString(); 47 } 48 content[i][7]=\"无\"; 49 if(cCostVo.getVehicleNumNk()!=null){ 50 content[i][7]=cCostVo.getVehicleNumNk().toString(); 51 } 52 content[i][8]=\"无\"; 53 if(cCostVo.getVehicleNumChargNk()!=null){ 54 content[i][8]=cCostVo.getVehicleNumChargNk().toString(); 55 } 56 content[i][9]=\"无\"; 57 if(cCostVo.getUnitPriceNk()!=null){ 58 content[i][9]=cCostVo.getUnitPriceNk().toString(); 59 } 60 content[i][10]=\"无\"; 61 if(cCostVo.getAccountAmountNk()!=null){ 62 content[i][10]=cCostVo.getAccountAmountNk().toString(); 63 } 64 content[i][11]=\"无\"; 65 if(cCostVo.getAccountPayableNk()!=null){ 66 content[i][11]=cCostVo.getAccountPayableNk().toString(); 67 } 68 content[i][12]=\"无\"; 69 if(cCostVo.getAccountAmount()!=null){ 70 content[i][12]=cCostVo.getAccountAmount().toString(); 71 } 72 content[i][13]=\"无\"; 73 if(cCostVo.getAccountPayable()!=null){ 74 content[i][13]=cCostVo.getAccountPayable().toString(); 75 } 76 77 } 78 79 //创建hssfworkbook 80 HSSFWorkbook workbook=ExcelUtils.getHSSFWorkbook(sheetName, , content, null); 81 //相应到客户端 82 try{ 83 this.setResponseHeader(response, fileName); 84 OutputStream osStream=response.getOutputStream(); 85 workbook.write(osStream); 86 osStream.flush(); 87 osStream.close(); 88 }catch(Exception e){ 89 e.printStackTrace(); 90 } 91 }
1 //发送响应流方法 2 public void setResponseHeader(HttpServletResponse response, String fileName) { 3 try { 4 try { 5 fileName = new String(fileName.getBytes(),\"UTF-8\"); 6 } catch (UnsupportedEncodingException e) { 7 e.printStackTrace(); 8 } 9 response.setContentType(\"application/octet-stream;charset=UTF-8\"); 10 response.setHeader(\"Content-Disposition\", \"attachment;filename=\"+ fileName); 11 response.addHeader(\"Pargam\", \"no-cache\"); 12 response.addHeader(\"Cache-Control\", \"no-cache\"); 13 } catch (Exception ex) { 14 ex.printStackTrace(); 15 } 16 }
2、自己创建好Execl表的模板,然后直接往里面写入数据
1 /** 2 * 导出异常车辆Ecexl 3 * @param response 4 * @param materialGjChangeDetailVo 5 * @throws Exception 6 */ 7 @RequestMapping(\"/downloadEcexl\") 8 @ResponseBody 9 public void downloadExecl(HttpServletResponse response, MaterialGjChangeDetailVo materialGjChangeDetailVo) throws Exception{ 10 // 所属公司 11 SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal(); 12 if(!\"1\".equals(sysUser.getType())) { 13 materialGjChangeDetailVo.setCompanyId(sysUser.getCompanyInfoId()); 14 materialGjChangeDetailVo.setYear(Integer.valueOf(redisService.valueOperations().get(\"audityear\").toString())); 15 } 16 //模板地址,是模板在项目中的地址 17 String filePathName = \"/templates/新增及更换公交车异常车辆明细表.xls\"; 18 InputStream in=this.getClass().getResourceAsStream(filePathName); 19 POIFSFileSystem poifsFileSystem=new POIFSFileSystem(in); 20 HSSFWorkbook workbook=new HSSFWorkbook(poifsFileSystem); 21 HSSFSheet sheet=workbook.getSheet(\"Sheet1\"); 22 sheet.setForceFormulaRecalculation(true); 23 //设置模板中的一些格式,这里主要是处理某些表格高亮 24 HSSFCellStyle style=(HSSFCellStyle)workbook.createCellStyle(); 25 style = workbook.createCellStyle(); 26 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); 27 style.setFillPattern(FillPatternType.SOLID_FOREGROUND); 28 29 List<MaterialGjChangeDetailVo> ts = materialGjChangeDetailService.selectMaterialGjChangeDetail(materialGjChangeDetailVo); 30 String companyName=null; 31 if(ts!=null&& ts.size()!=0){ 32 companyName=ts.get(0).getCompanyName(); 33 //从第一行第一列开始写入数据,列与行的下标都是从0开始的 34 sheet.getRow(0).getCell(0).setCellValue(\"新增及更换公交车明细表(\"+materialGjChangeDetailVo.getYear()+\"年度)\"); 35 sheet.getRow(2).getCell(0).setCellValue(\"被审计单位:\"+companyName); 36 for(int i=0,len1=ts.size();i<len1;i++){ 37 String falg=null; 38 if(ts.get(i).getFlag()!=null){ 39 falg=ts.get(i).getFlag(); 40 } 41 for(int j=0,len=falg.length();j<len;j++){ 42 sheet.getRow(5+i).getCell(0).setCellValue(i+1);//第5+i行 第i列 从0开始 0 43 if(ts.get(i).getPlateNumber()!=null){ 44 sheet.getRow(5+i).getCell(1).setCellValue(ts.get(i).getPlateNumber());//1 45 if(falg!=null&&falg!=null&&falg.charAt(1)==\'0\'){ 46 sheet.getRow(5+i).getCell(1).setCellStyle(style); 47 } 48 } 49 if(ts.get(i).getPlateColor()!=null){ 50 if (ts.get(i).getPlateColor().equals(\"100201\")) { 51 sheet.getRow(5+i).getCell(2).setCellValue(\"黄色\");//2 52 } else if (ts.get(i).getPlateColor().equals(\"100202\")) { 53 sheet.getRow(5+i).getCell(2).setCellValue(\"蓝色\");//2 54 } else if (ts.get(i).getPlateColor().equals(\"100203\")) { 55 sheet.getRow(5+i).getCell(2).setCellValue(\"其他\");//2 56 } 57 58 if(falg!=null&&falg.charAt(2)==\'0\'){ 59 sheet.getRow(5+i).getCell(2).setCellStyle(style); 60 } 61 } 62 if(ts.get(i).getVehicleBrandRegDate()!=null){ 63 sheet.getRow(5+i).getCell(3).setCellValue(ts.get(i).getVehicleBrandRegDate());//3 64 if(falg!=null&&falg.charAt(3)==\'0\'){ 65 sheet.getRow(5+i).getCell(3).setCellStyle(style); 66 } 67 } 68 if(ts.get(i).getVehicleModel()!=null){ 69 sheet.getRow(5+i).getCell(4).setCellValue(ts.get(i).getVehicleModel());//4 70 if(falg!=null&&falg.charAt(4)==\'0\'){ 71 sheet.getRow(5+i).getCell(4).setCellStyle(style); 72 } 73 } 74 if(ts.get(i).getManufacturers()!=null){ 75 sheet.getRow(5+i).getCell(5).setCellValue(ts.get(i).getManufacturers());//5 76 if(falg!=null&&falg.charAt(5)==\'0\'){ 77 sheet.getRow(5+i).getCell(5).setCellStyle(style); 78 } 79 } 80 if(ts.get(i).getBrand()!=null){ 81 82 sheet.getRow(5+i).getCell(6).setCellValue(ts.get(i).getBrand());//6 83 if(falg!=null&&falg.charAt(6)==\'0\'){ 84 sheet.getRow(5+i).getCell(6).setCellStyle(style); 85 } 86 } 87 if(ts.get(i).getVehicleLength()!=null){ 88 89 sheet.getRow(5+i).getCell(7).setCellValue(ts.get(i).getVehicleLength());//7 90 if(falg!=null&&falg.charAt(7)==\'0\'){ 91 sheet.getRow(5+i).getCell(7).setCellStyle(style); 92 } 93 } 94 if(ts.get(i).getVehicleType()!=null){ 95 if (ts.get(i).getVehicleType().contains(\"100701\")) { 96 sheet.getRow(5+i).getCell(8).setCellValue(\"纯电动\");//8 97 } else if (ts.get(i).getVehicleType().contains(\"100702\")) { 98 sheet.getRow(5+i).getCell(8).setCellValue(\"燃料电池\");//8 99 } else if (ts.get(i).getVehicleType().contains(\"100703\")) { 100 sheet.getRow(5+i).getCell(8).setCellValue(\"超级电容\");//8 101 } else if (ts.get(i).getVehicleType().contains(\"100704\")) { 102 sheet.getRow(5+i).getCell(8).setCellValue(\"插电式\");//8 103 } else if (ts.get(i).getVehicleType().contains(\"100705\")) { 104 sheet.getRow(5+i).getCell(8).setCellValue(\"非插电式\");//8 105 } else if (ts.get(i).getVehicleType().contains(\"100706\")) { 106 sheet.getRow(5+i).getCell(8).setCellValue(\"其他\");//8 107 } 108 if(falg!=null&&falg.charAt(8)==\'0\'){ 109 sheet.getRow(5+i).getCell(8).setCellStyle(style); 110 } 111 } 112 113 114 if(ts.get(i).getExtendInfo()!=null){ 115 if (ts.get(i).getExtendInfo()) { 116 sheet.getRow(5+i).getCell(9).setCellValue(\"是\");//9 117 } else{ 118 sheet.getRow(5+i).getCell(9).setCellValue(\"否\");//9 119 } 120 if(falg!=null&&falg.charAt(9)==\'0\'){ 121 sheet.getRow(5+i).getCell(9).setCellStyle(style); 122 } 123 } 124 if(ts.get(i).getPurchaseInvoiceInfo()!=null){ 125 if (ts.get(i).getPurchaseInvoiceInfo()) { 126 sheet.getRow(5+i).getCell(10).setCellValue(\"是\");//10 127 } else{ 128 sheet.getRow(5+i).getCell(10).setCellValue(\"否\");//10 129 } 130 131 if(falg!=null&&falg.charAt(10)==\'0\'){ 132 sheet.getRow(5+i).getCell(10).setCellStyle(style); 133 } 134 } 135 break; 136 } 137 } 138 } 139 try{ 140 this.setResponseHeader(response, companyName+\"-新增及更换公交车异常车辆明细表.xls\"); 141 OutputStream osStream=response.getOutputStream(); 142 workbook.write(osStream); 143 osStream.flush(); 144 osStream.close(); 145 }catch(Exception e){ 146 e.printStackTrace(); 147 } 148 }
1 /** 2 * 3 * @param response 4 * @param fileName 文件名 5 */ 6 public void setResponseHeader(HttpServletResponse response, String fileName) { 7 try { 8 try { 9 fileName = new String(fileName.getBytes(),\"ISO8859-1\"); 10 } catch (UnsupportedEncodingException e) { 11 e.printStackTrace(); 12 } 13 response.setContentType(\"application/octet-stream;charset=ISO8859-1\"); 14 response.setHeader(\"Content-Disposition\", \"attachment;filename=\"+ fileName); 15 response.addHeader(\"Pargam\", \"no-cache\"); 16 response.addHeader(\"Cache-Control\", \"no-cache\"); 17 } catch (Exception ex) { 18 ex.printStackTrace(); 19 } 20 }
以上就是使用poi往execl表中写入数据的两种方式,需要注意的是,前端过来的请求不能是ajax请求,因为ajax无法解析二进制流的输出。具体原因客参考:https://www.cnblogs.com/houqx/p/10106562.html
继续阅读与本文标签相同的文章
-
定时器Cron配置方法
2026-05-19栏目: 教程
-
你有一个阿里云十周年大礼包待领取
2026-05-19栏目: 教程
-
新能力|营销利器“轻会员”来袭!
2026-05-19栏目: 教程
-
Serverless 与容器决战在即?有了弹性伸缩就不一样了
2026-05-19栏目: 教程
-
2pc 3pc 详述
2026-05-19栏目: 教程
