/** * 导出excel * @param model * @param request * @param userAgent * @param id * @return */ @RequestMapping(value = "export.htm") public void export(ModelMap model,HttpServletRequest request,HttpServletResponse response,UserAgent userAgent,@RequestParam(value = "taxpayerId",required = false) Integer id) { //获取填充数据 TaxpayerInfo selectByPrimaryKey = taxpayerInfoManager.selectByPrimaryKey(id); List<SupportOlder> supportOlderList=supportOlderManager.getListByTaxpayerId(id); //创建excel ExcelUtil excelUtil=new ExcelUtil(); //获取模板存放路径 String line=File.separator; String realPath=request.getRealPath("")+"\\excelmodel\\"; System.out.println(realPath); //windows下 if("\\".equals(line)){ realPath = realPath.replace("/", "\\"); // 将/换成\\ realPath=realPath+line; } //linux下 if("/".equals(line)){ realPath = realPath.replace("\\", "/"); realPath=realPath+line; } String path=realPath+"supportOlder.xls"; String fileName="赡养老人支出明细.xls"; //给excel设置模板、sheet名称 excelUtil.setSrcPath(path); excelUtil.setSheetName("Sheet1"); excelUtil.getSheet(); //开始填充数据 String cardType=""; cardType = getCardType(selectByPrimaryKey.getCardType()==null?"":selectByPrimaryKey.getCardType()); excelUtil.setCellStrValue(1, 1, cardType); excelUtil.setCellStrValue(1, 3, selectByPrimaryKey.getCardNo()); excelUtil.setCellStrValue(2, 1, selectByPrimaryKey.getName()); excelUtil.setCellStrValue(2, 3, selectByPrimaryKey.getIdentifyNo()); excelUtil.setCellStrValue(3, 1, selectByPrimaryKey.getWithholdingAgent()); excelUtil.setCellStrValue(3, 3, selectByPrimaryKey.getAgentNo()); int row=4; int col=-1; Integer num=0; for (SupportOlder supportOlder : supportOlderList) { row++; num++; col=1; excelUtil.setCellStrValue(row, 0, num.toString()); excelUtil.setCellStrValue(row, col++, supportOlder.getName()); excelUtil.setCellStrValue(row, col++, getCardType(supportOlder.getCardType())); excelUtil.setCellStrValue(row, col++, supportOlder.getCardNo()); } //导出excel excelUtil.exportToWeb(response,fileName); } |
工具类为ExcelUtil:
package com.zhiyuancorp.web.util;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.