Java使用poi根据模板导出Excel
最近因项目需求要根据给定的Excel模板将后台数据导入到模板Excel中,为以后方便使用,现将代码整理出来,供大家参考:
下面数是给定的Excel模板:
// 前端js代码
function reportSummary() {
var startTime=$("#sTime").datepicker("getValue");
var endTime=$("#eTime").datepicker("getValue");
if(startTime!=endTime){
alert("请选择某一天的报表汇总数据导出!");
return;
}
var flag=check(startTime,endTime);
if(flag){
$.confirm("导出"+startTime+"至"+endTime+"期间报表汇总?", "确认导出", function (r) {
if(r) {
window.location.href = "${ctx}/worksheet/reportSummary?sTime="+ startTime + "&eTime=" + endTime;
}
});
}
}
// 控制层controller代码
@Logger(action = "导出", logger = "导出报表汇总页面Excel",model="报表报送")
@RequestMapping(value = "/reportSummary")
public void reportSummary(@RequestParam(value="sTime") String startTime,
@RequestParam(value = "eTime") String endTime,
HttpServletRequest request, HttpServletResponse response) throws WebUIException {
ServletOutputStream out=null;
try {
//设置相应到客户端的文件名编码
// 解决乱码
response.setCharacterEncoding("UTF-8");
//定义下载的文件名字
String fileName = "各监所每日基本情况报表汇总.xlsx";
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
//获取响应报文输出流对象
out = response.getOutputStream();
//调用业务方法,获得需要下载的excel文件
XSSFWorkbook book=null;
UserBean user = AuthSystemFacade.getLoginUserInfo();
String orgCode = user.getOrgCode();
if("1100".equals(orgCode)){//市局
book =dailyBasicWorksheetService.reportSummary(request,startTime, endTime);
}else if("1140".equals(orgCode)){//分局
book =dailyBasicWorksheetService.reportSummaryFj(user,request,startTime, endTime);
}
book.write(out);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 业务层service代码
XSSFWorkbook reportSummaryFj(UserBean user,HttpServletRequest request, String startTime, String endTime) throws Exception;
// 业务层serviceImpl代码
@Override
public XSSFWorkbook reportSummaryFj(UserBean user,HttpServletRequest request, String startTime, String endTime) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(new Date());
Map<String, Object> summaryCountData = this.getDao().summaryData(user.getCusNumber(),"2",startTime, endTime);
//分局报表汇总数据 WEB-INF/static/excelMoudle/fjBbhz.xlsx
List<DailyBasicWorksheet> DailyBasicWorksheets = this.getDao().findSummaryData(user.getCusNumber(),startTime, endTime);
String filePath = request.getSession().getServletContext().getRealPath("")+"\\WEB-INF\\static\\excelMoudle\\fjBbhz.xlsx";
//创建Excel文件的输入流对象
FileInputStream fis = new FileInputStream(filePath);
//根据模板创建excel工作簿
XSSFWorkbook workBook = new XSSFWorkbook(fis);
//获取创建的工作簿第一页
XSSFSheet sheet = workBook.getSheetAt(0);
//给指定的sheet命名
workBook.setSheetName(0,"分局报表汇总");
//分局总值班长
XSSFRow row5 = sheet.getRow(4);
//分局机关值班长一
XSSFRow row6 = sheet.getRow(5);
//分局机关值班长二
XSSFRow row7 = sheet.getRow(6);
//分局管教值班长
XSSFRow row8 = sheet.getRow(7);
//清河医院
XSSFRow row9 = sheet.getRow(8);
//服务保障单位
XSSFRow row10 = sheet.getRow(9);
//前进
XSSFRow row11 = sheet.getRow(10);
//垦华
XSSFRow row12 = sheet.getRow(11);
//潮白
XSSFRow row13 = sheet.getRow(12);
//清园
XSSFRow row14 = sheet.getRow(13);
//柳林
XSSFRow row15 = sheet.getRow(14);
//合计
XSSFRow row39 = sheet.getRow(15);
XSSFCell row39Cell8 = row39.getCell(7);
row39Cell8.setCellValue(""+summaryCountData.get("ZGGJ_ZS").toString());
XSSFCell row39Cell9 = row39.getCell(8);
row39Cell9.setCellValue(""+summaryCountData.get("ZGGJ_JG").toString());
XSSFCell row39Cell10 = row39.getCell(9);
row39Cell10.setCellValue(""+summaryCountData.get("ZGGJ_JQ").toString());
XSSFCell row39Cell11 = row39.getCell(10);
row39Cell11.setCellValue(""+summaryCountData.get("ZBGJ_ZS").toString());
XSSFCell row39Cell12 = row39.getCell(11);
row39Cell12.setCellValue(""+summaryCountData.get("ZBGJ_JG").toString());
XSSFCell row39Cell13 = row39.getCell(12);
row39Cell13.setCellValue(""+summaryCountData.get("ZBGJ_JQ").toString());
XSSFCell row39Cell14 = row39.getCell(13);
row39Cell14.setCellValue(""+summaryCountData.get("BQ_JL").toString());
XSSFCell row39Cell15 = row39.getCell(14);
row39Cell15.setCellValue(""+summaryCountData.get("PROBLEM_COUNT").toString());
XSSFCell row39Cell16 = row39.getCell(15);//监管问题
row39Cell16.setCellValue(""+summaryCountData.get("SUPERVISION_PROBLEM_COUNT").toString());
XSSFCell row39Cell17 = row39.getCell(16);//其他问题
row39Cell17.setCellValue(""+summaryCountData.get("OTHER_PROBLEM_COUNT").toString());
XSSFCell row39Cell18 = row39.getCell(17);
row39Cell18.setCellValue(""+summaryCountData.get("ZC_ZFS").toString());
XSSFCell row39Cell19 = row39.getCell(18);
row39Cell19.setCellValue(""+summaryCountData.get("ZY_ZFS").toString());
XSSFCell row39Cell20 = row39.getCell(19);
row39Cell20.setCellValue(""+summaryCountData.get("JHZS_ZFS").toString());
XSSFCell row39Cell21 = row39.getCell(20);
row39Cell21.setCellValue(""+summaryCountData.get("JWZX_ZFS").toString());
XSSFCell row39Cell22 = row39.getCell(21);
row39Cell22.setCellValue(""+summaryCountData.get("TXLJ_ZFS").toString());
XSSFCell row39Cell23 = row39.getCell(22);//外出就医
row39Cell23.setCellValue(""+summaryCountData.get("WCJY_ZFS").toString());
XSSFCell row39Cell25 = row39.getCell(23);//中心医院
row39Cell25.setCellValue(""+summaryCountData.get("ZXYYZY_ZFS").toString());
XSSFCell row39Cell26 = row39.getCell(24);//清河医院
row39Cell26.setCellValue(""+summaryCountData.get("QHYYZY_ZFS").toString());
XSSFCell row39Cell27 = row39.getCell(25);//利康医院
row39Cell27.setCellValue(""+summaryCountData.get("LKYYZY_ZFS").toString());
XSSFCell row39Cell29 = row39.getCell(26);
row39Cell29.setCellValue(""+summaryCountData.get("SHYYZY_ZFS").toString());
XSSFCell row39Cell30 = row39.getCell(27);
row39Cell30.setCellValue(""+summaryCountData.get("SHYYZY_KYMJ").toString());
XSSFCell row39Cell31 = row39.getCell(28);
row39Cell31.setCellValue(""+summaryCountData.get("JY_ZFS").toString());
XSSFCell row39Cell32 = row39.getCell(29);
row39Cell32.setCellValue(""+summaryCountData.get("LY_ZFS").toString());
XSSFCell row39Cell33 = row39.getCell(30);
row39Cell33.setCellValue(""+summaryCountData.get("XSY_MAN_ZFS").toString());
XSSFCell row39Cell34 = row39.getCell(31);
row39Cell34.setCellValue(""+summaryCountData.get("XSY_WOMAN_ZFS").toString());
XSSFCell row39Cell35 = row39.getCell(32);//调出数
row39Cell35.setCellValue(""+summaryCountData.get("ZF_DCS").toString());
XSSFCell row39Cell36 = row39.getCell(33);//
row39Cell36.setCellValue(""+summaryCountData.get("SF_ZFS").toString());
XSSFCell row39Cell37 = row39.getCell(34);//
row39Cell37.setCellValue(""+summaryCountData.get("JS_ZFS").toString());
XSSFCell row39Cell38 = row39.getCell(35);//
row39Cell38.setCellValue(""+summaryCountData.get("SW_ZFS").toString());
XSSFCell row39Cell39 = row39.getCell(36);//姓名
row39Cell39.setCellValue(user.getRealName());
XSSFCell row39Cell40 = row39.getCell(37);//日期
row39Cell40.setCellValue(time);
for(DailyBasicWorksheet dailyBasicWorksheet : DailyBasicWorksheets){
String unitId = dailyBasicWorksheet.getUnitId();
String unitName = dailyBasicWorksheet.getUnitName();
if("分局总值班长".equals(unitName)){//分局
this.setCellValue(row5,dailyBasicWorksheet);
}else if("分局机关值班长二".equals(unitName)){
this.setCellValue(row7,dailyBasicWorksheet);
}else if("分局机关值班长一".equals(unitName)){
this.setCellValue(row6,dailyBasicWorksheet);
}else if("分局管教值班长".equals(unitName)){
this.setCellValue(row8,dailyBasicWorksheet);
}else if("清河医院".equals(unitName)){
this.setCellValue(row9,dailyBasicWorksheet);
}else if("服务保障单位".equals(unitName)){
this.setCellValue(row10,dailyBasicWorksheet);
}else if("1142".equals(unitId)){//柳林
this.setCellValue(row15,dailyBasicWorksheet);
}else if("1145".equals(unitId)){//前进 row11
this.setCellValue(row11,dailyBasicWorksheet);
}else if("1146".equals(unitId)){//潮白 row13
this.setCellValue(row13,dailyBasicWorksheet);
}else if("1147".equals(unitId)){//清园 row14
this.setCellValue(row14,dailyBasicWorksheet);
}else if("1149".equals(unitId)){//垦华 row12
this.setCellValue(row12,dailyBasicWorksheet);
}
}
return workBook;
}
public void setCellValue(XSSFRow row,DailyBasicWorksheet dailyBasicWorksheet){
XSSFCell row12Cell2 = row.getCell(1);//姓名
row12Cell2.setCellValue(dailyBasicWorksheet.getOnePositionLeader());
XSSFCell row12Cell3 = row.getCell(2);//联系电话
row12Cell3.setCellValue(dailyBasicWorksheet.getOnePositionPhone());
XSSFCell row12Cell4 = row.getCell(3);//姓名
row12Cell4.setCellValue(dailyBasicWorksheet.getTwoPositionLeader());
XSSFCell row12Cell5 = row.getCell(4);//联系电话
row12Cell5.setCellValue(dailyBasicWorksheet.getTwoPositionPhone());
XSSFCell row12Cell6 = row.getCell(5);//姓名
row12Cell6.setCellValue(dailyBasicWorksheet.getZhzXm());
XSSFCell row12Cell7 = row.getCell(6);//联系电话
row12Cell7.setCellValue(dailyBasicWorksheet.getZhzPhone());
XSSFCell row12Cell8 = row.getCell(7);//在岗 总数
row12Cell8.setCellValue(""+dailyBasicWorksheet.getZggjZs());
XSSFCell row12Cell9 = row.getCell(8);//在岗 机关
row12Cell9.setCellValue(""+dailyBasicWorksheet.getZggjJg());
XSSFCell row12Cell10 = row.getCell(9);
row12Cell10.setCellValue(""+dailyBasicWorksheet.getZggjJq());
XSSFCell row12Cell11 = row.getCell(10);
row12Cell11.setCellValue(""+dailyBasicWorksheet.getZbgjZs());
XSSFCell row12Cell12 = row.getCell(11);
row12Cell12.setCellValue(""+dailyBasicWorksheet.getZbgjJg());
XSSFCell row12Cell13 = row.getCell(12);
row12Cell13.setCellValue(""+dailyBasicWorksheet.getZbgjJq());
XSSFCell row12Cell14 = row.getCell(13);
row12Cell14.setCellValue(""+dailyBasicWorksheet.getBqJl());
XSSFCell row12Cell15 = row.getCell(14);
row12Cell15.setCellValue(""+dailyBasicWorksheet.getProblemCount());
XSSFCell row12Cell16 = row.getCell(15);//监管问题
row12Cell16.setCellValue(""+dailyBasicWorksheet.getSupervisionProblemCount());
XSSFCell row12Cell17 = row.getCell(16);//其他问题
row12Cell17.setCellValue(""+dailyBasicWorksheet.getOtherProblemCount());
XSSFCell row12Cell18 = row.getCell(17);
row12Cell18.setCellValue(""+dailyBasicWorksheet.getZcZfs());
XSSFCell row12Cell19 = row.getCell(18);
row12Cell19.setCellValue(""+dailyBasicWorksheet.getZyZfs());
XSSFCell row12Cell20 = row.getCell(19);
row12Cell20.setCellValue(""+dailyBasicWorksheet.getJhzsZfs());
XSSFCell row12Cell21 = row.getCell(20);
row12Cell21.setCellValue(""+dailyBasicWorksheet.getJwzxZfs());
XSSFCell row12Cell22 = row.getCell(21);
row12Cell22.setCellValue(""+dailyBasicWorksheet.getTxljZfs());
XSSFCell row12Cell23 = row.getCell(22);
row12Cell23.setCellValue(""+dailyBasicWorksheet.getWcjyZfs());
XSSFCell row12Cell24 = row.getCell(23);
row12Cell24.setCellValue(""+dailyBasicWorksheet.getZxyyzyZfs());
XSSFCell row12Cell25 = row.getCell(24);
row12Cell25.setCellValue(""+dailyBasicWorksheet.getQhyyzyZfs());
XSSFCell row12Cell26 = row.getCell(25);
row12Cell26.setCellValue(""+dailyBasicWorksheet.getLkyyzyZfs());
XSSFCell row12Cell27 = row.getCell(26);
row12Cell27.setCellValue(""+dailyBasicWorksheet.getShyyzyZfs());
XSSFCell row12Cell28 = row.getCell(27);
row12Cell28.setCellValue(""+dailyBasicWorksheet.getShyyzyKymj());
XSSFCell row12Cell29 = row.getCell(28);
row12Cell29.setCellValue(""+dailyBasicWorksheet.getJyZfs());
XSSFCell row12Cell30 = row.getCell(29);
row12Cell30.setCellValue(""+dailyBasicWorksheet.getLyZfs());
XSSFCell row12Cell31 = row.getCell(30);
row12Cell31.setCellValue(""+dailyBasicWorksheet.getXsyManZfs());
XSSFCell row12Cell32 = row.getCell(31);
row12Cell32.setCellValue(""+dailyBasicWorksheet.getXsyWomanZfs());
XSSFCell row12Cell33 = row.getCell(32);
row12Cell33.setCellValue(""+dailyBasicWorksheet.getZfDcs());
XSSFCell row12Cell34 = row.getCell(33);
row12Cell34.setCellValue(""+dailyBasicWorksheet.getSfZfs());
XSSFCell row12Cell35 = row.getCell(34);
row12Cell35.setCellValue(""+dailyBasicWorksheet.getJsZfs());
XSSFCell row12Cell36 = row.getCell(35);
row12Cell36.setCellValue(""+dailyBasicWorksheet.getSwZfs());
XSSFCell row12Cell37 = row.getCell(36);//姓名
row12Cell37.setCellValue(dailyBasicWorksheet.getReportPerson());
XSSFCell row12Cell38 = row.getCell(37);//日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
row12Cell38.setCellValue(sdf.format(dailyBasicWorksheet.getReportTime()));
XSSFCell row12Cell39 = row.getCell(38);//备注
row12Cell39.setCellValue(dailyBasicWorksheet.getRemark());
}