Java用POI往execl表格中写数据,并下载下来有两种方式
1、用poil的API创建表格,并设计好表格格式,然后往里面写数据
内容
文件
(workbook== workbook= 添加一个sheet,对应execl文件中的sheet
HSSFSheet sheet= sheet.setDefaultColumnWidth(()13 添加表头第0行
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!=