加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java下载execl表格

发布时间:2020-12-14 06:41:00 所属栏目:Java 来源:网络整理
导读:Java用POI往execl表格中写数据,并下载下来有两种方式 1、用poil的API创建表格,并设计好表格格式,然后往里面写数据 (workbook== workbook= HSSFSheet sheet= sheet.setDefaultColumnWidth(( )13 HSSFRow row=sheet.createRow(0 HSSFCellStyle style= HSSFF

Java用POI往execl表格中写数据,并下载下来有两种方式

1、用poil的API创建表格,并设计好表格格式,然后往里面写数据

(workbook== workbook= HSSFSheet sheet= sheet.setDefaultColumnWidth(()13 HSSFRow row=sheet.createRow(0 HSSFCellStyle style= HSSFFont font= font.setFontName("微软雅黑" HSSFCell cell= ( i=0;i cell= HSSFCell cell2= ( i=0;i row = sheet.createRow(i + 1 ( j=0;j cell2=
@RequestMapping("/download" download(HttpServletResponse response,CompanyCostVo companyCostVo) Integer year=Integer.valueOf(redisService.valueOperations().get("audityear" List companyCostVos= String[]title={"序号","公司名称","公交总车辆数","公交收费车辆数","公交收费单价","公交实收金额","公交应收金额" "农客总车辆数","农客收费车辆数","农客收费单价","农客实收金额","农客应收金额","总实收金额","总应收金额" String fileName=year+"年企业审计费用清算信息表.xls" String sheetName="费用清算信息" len= String [][] content= String[len][14 ( i=0;i CompanyCostVo cCostVo= content[i][0]=(i+1)+"" content[i][1]= content[i][2]="无" (cCostVo.getVehicleNumGj()!= content[i][2]= content[i][3]="无" (cCostVo.getVehicleNumChargGj()!= content[i][3]= content[i][4]="无" (cCostVo.getUnitPriceGj()!= content[i][4]= content[i][5]="无" (cCostVo.getAccountAmountGj()!= content[i][5]= content[i][6]="无" (cCostVo.getAccountPayableGj()!= content[i][6]= content[i][7]="无" (cCostVo.getVehicleNumNk()!= content[i][7]= content[i][8]="无" (cCostVo.getVehicleNumChargNk()!= content[i][8]= content[i][9]="无" (cCostVo.getUnitPriceNk()!= content[i][9]= content[i][10]="无" (cCostVo.getAccountAmountNk()!= content[i][10]= content[i][11]="无" (cCostVo.getAccountPayableNk()!= content[i][11]= content[i][12]="无" (cCostVo.getAccountAmount()!= content[i][12]= content[i][13]="无" (cCostVo.getAccountPayable()!= content[i][13]= HSSFWorkbook workbook=ExcelUtils.getHSSFWorkbook(sheetName,title,content, OutputStream osStream= } }
fileName = String(fileName.getBytes(),"UTF-8" } response.setContentType("application/octet-stream;charset=UTF-8" response.setHeader("Content-Disposition","attachment;filename="+ response.addHeader("Pargam","no-cache" response.addHeader("Cache-Control","no-cache" } }

2、自己创建好Execl表的模板,然后直接往里面写入数据

@RequestMapping("/downloadEcexl" downloadExecl(HttpServletResponse response,MaterialGjChangeDetailVo materialGjChangeDetailVo) SysUser sysUser = (!"1" materialGjChangeDetailVo.setYear(Integer.valueOf(redisService.valueOperations().get("audityear" String filePathName = "/templates/新增及更换公交车异常车辆明细表.xls" InputStream in= POIFSFileSystem poifsFileSystem= HSSFWorkbook workbook= HSSFSheet sheet=workbook.getSheet("Sheet1" sheet.setForceFormulaRecalculation( HSSFCellStyle style= style = List ts = String companyName= (ts!=&& ts.size()!=0 companyName=ts.get(0 sheet.getRow(0).getCell(0).setCellValue("新增及更换公交车明细表("+materialGjChangeDetailVo.getYear()+"年度)" sheet.getRow(2).getCell(0).setCellValue("被审计单位:"+ ( i=0,len1=ts.size();i String falg= (ts.get(i).getFlag()!= falg= ( j=0,len=falg.length();j sheet.getRow(5+i).getCell(0).setCellValue(i+1); (ts.get(i).getPlateNumber()!= sheet.getRow(5+i).getCell(1).setCellValue(ts.get(i).getPlateNumber()); (falg!=&&falg!=&&falg.charAt(1)=='0' sheet.getRow(5+i).getCell(1 (ts.get(i).getPlateColor()!= (ts.get(i).getPlateColor().equals("100201" sheet.getRow(5+i).getCell(2).setCellValue("黄色"); } (ts.get(i).getPlateColor().equals("100202" sheet.getRow(5+i).getCell(2).setCellValue("蓝色"); } (ts.get(i).getPlateColor().equals("100203" sheet.getRow(5+i).getCell(2).setCellValue("其他"); (falg!=&&falg.charAt(2)=='0' sheet.getRow(5+i).getCell(2 (ts.get(i).getVehicleBrandRegDate()!= sheet.getRow(5+i).getCell(3).setCellValue(ts.get(i).getVehicleBrandRegDate()); (falg!=&&falg.charAt(3)=='0' sheet.getRow(5+i).getCell(3 (ts.get(i).getVehicleModel()!= sheet.getRow(5+i).getCell(4).setCellValue(ts.get(i).getVehicleModel()); (falg!=&&falg.charAt(4)=='0' sheet.getRow(5+i).getCell(4 (ts.get(i).getManufacturers()!= sheet.getRow(5+i).getCell(5).setCellValue(ts.get(i).getManufacturers()); (falg!=&&falg.charAt(5)=='0' sheet.getRow(5+i).getCell(5 (ts.get(i).getBrand()!= sheet.getRow(5+i).getCell(6).setCellValue(ts.get(i).getBrand()); (falg!=&&falg.charAt(6)=='0' sheet.getRow(5+i).getCell(6 (ts.get(i).getVehicleLength()!= sheet.getRow(5+i).getCell(7).setCellValue(ts.get(i).getVehicleLength()); (falg!=&&falg.charAt(7)=='0' sheet.getRow(5+i).getCell(7 (ts.get(i).getVehicleType()!= (ts.get(i).getVehicleType().contains("100701" sheet.getRow(5+i).getCell(8).setCellValue("纯电动"); } (ts.get(i).getVehicleType().contains("100702" sheet.getRow(5+i).getCell(8).setCellValue("燃料电池"); } (ts.get(i).getVehicleType().contains("100703" sheet.getRow(5+i).getCell(8).setCellValue("超级电容"); } (ts.get(i).getVehicleType().contains("100704" sheet.getRow(5+i).getCell(8).setCellValue("插电式"); } (ts.get(i).getVehicleType().contains("100705" sheet.getRow(5+i).getCell(8).setCellValue("非插电式"); } (ts.get(i).getVehicleType().contains("100706" sheet.getRow(5+i).getCell(8).setCellValue("其他"); (falg!=&&falg.charAt(8)=='0' sheet.getRow(5+i).getCell(8 (ts.get(i).getExtendInfo()!= sheet.getRow(5+i).getCell(9).setCellValue("是"); } sheet.getRow(5+i).getCell(9).setCellValue("否"); (falg!=&&falg.charAt(9)=='0' sheet.getRow(5+i).getCell(9 (ts.get(i).getPurchaseInvoiceInfo()!= sheet.getRow(5+i).getCell(10).setCellValue("是"); } sheet.getRow(5+i).getCell(10).setCellValue("否"); (falg!=&&falg.charAt(10)=='0' sheet.getRow(5+i).getCell(10 .setResponseHeader(response,companyName+"-新增及更换公交车异常车辆明细表.xls" OutputStream osStream= } }
fileName = String(fileName.getBytes(),"ISO8859-1" } response.setContentType("application/octet-stream;charset=ISO8859-1" response.setHeader("Content-Disposition","attachment;filename="+ response.addHeader("Pargam","no-cache" response.addHeader("Cache-Control","no-cache" } }

以上就是使用poi往execl表中写入数据的两种方式,需要注意的是,前端过来的请求不能是ajax请求,因为ajax无法解析二进制流的输出。具体原因客参考:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读