原文链接:https://www.cnblogs.com/lfyu/p/8318201.html 创建excel大致分这几步: 1、创建HSSFWorkbook对象(也就是excel文档对象) 2、通过HSSFWorkbook对象创建sheet对象(也就是excel中的sheet) 3、通过sheet对象创建HSSFROW对象(row行对象) 4、通过HSSFROW对象创建列cell并set值(列名) controller 层 /** * excel自定义导出 * @param hAqscTieupsummary * @param request * @param response * @param dataGrid * @param modelMap * @return */ @SuppressWarnings("deprecation") @RequestMapping(params = "exportEXL") public String exportEXL(HAqscTieupsummaryEntity hAqscTieupsummary, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap) { try { String dateType = "yyyy"; SimpleDateFormat df = new SimpleDateFormat(dateType);// 设置日期格式 SimpleDateFormat df1 = new SimpleDateFormat("yyyy.MM.dd");// 设置日期格式 // 创建HSSFWorkbook对象(excel的文档对象) HSSFWorkbook wb = new HSSFWorkbook(); HSSFRow row = null; HSSFCell cell = null; // 建立新的sheet对象(excel的表单) 并设置sheet名字 HSSFSheet sheet = wb.createSheet("占压管线台账信息"); sheet.setDefaultRowHeightInPoints(30);// 设置缺省列高sheet.setDefaultColumnWidth(20);//设置缺省列宽 //----------------标题样式--------------------- HSSFCellStyle titleStyle = wb.createCellStyle(); //标题样式 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); Font ztFont = wb.createFont(); ztFont.setItalic(false); // 设置字体为斜体字 ztFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色” ztFont.setFontHeightInPoints((short)16); // 将字体大小设置为18px ztFont.setFontName("宋体"); // 将“宋体”字体应用到当前单元格上 ztFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗 //ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线) // ztFont.setStrikeout(true); // 是否添加删除线 titleStyle.setFont(ztFont); //------------------------------------------- //----------------二级标题格样式---------------------------------- HSSFCellStyle titleStyle2 = wb.createCellStyle(); //表格样式 titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); Font ztFont2 = wb.createFont(); ztFont2.setItalic(false); // 设置字体为斜体字 ztFont2.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色” ztFont2.setFontHeightInPoints((short)11); // 将字体大小设置为18px ztFont2.setFontName("宋体"); // 字体应用到当前单元格上 ztFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗 //ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线) //ztFont.setStrikeout(true); // 是否添加删除线 titleStyle2.setFont(ztFont2); //---------------------------------------------------------- //----------------单元格样式---------------------------------- HSSFCellStyle cellStyle = wb.createCellStyle(); //表格样式 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 Font cellFont = wb.createFont(); cellFont.setItalic(false); // 设置字体为斜体字 cellFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色” cellFont.setFontHeightInPoints((short)10); // 将字体大小设置为18px cellFont.setFontName("宋体"); // 字体应用到当前单元格上 //cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cellStyle.setFont(cellFont); cellStyle.setWrapText(true);//设置自动换行 //---------------------------------------------------------- // ----------------------创建第一行--------------- // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 row = sheet.createRow(0); // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 cell = row.createCell(0); // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 19)); // 设置单元格内容 cell.setCellValue("占压城市地下管线、输油气管道、化工产品管道违法违规建设汇总表"); cell.setCellStyle(titleStyle); // ---------------------------------------------- // ------------------创建第二行(单位、填表日期)--------------------- row = sheet.createRow(1); // 创建第二行 cell = row.createCell(0); cell.setCellValue("填报单位名称(盖章): "); cell.setCellStyle(titleStyle2); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 3)); cell = row.createCell(4); sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5)); TSBaseUser tb = ResourceUtil.getSessionUserName(); //获取当前登录用户信息 String uid = tb.getId(); String deptId = userDao.getDeptId(uid); String deptName = userDao.getDeptName(deptId); cell.setCellValue(deptName); //cell.setCellValue("*****"); cell.setCellStyle(titleStyle2); cell = row.createCell(13); // 填表时间 sheet.addMergedRegion(new CellRangeAddress(1, 1, 13, 16)); cell.setCellValue("填表时间:"+df1.format(new Date())); cell.setCellStyle(titleStyle2); //HSSFCell cell14 = row.createCell(15); // 填表时间 //cell14.setCellValue(); //cell14.setCellValue("2017.11.30"); // cell14.setCellStyle(titleStyle2); // ---------------------------------------------- // ------------------创建表头start--------------------- row = sheet.createRow(2); // 创建第三行 sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0)); cell = row.createCell(0); cell.setCellValue("序号"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1)); cell = row.createCell(1); cell.setCellValue("隐患等级评定"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 2, 2)); cell = row.createCell(2); cell.setCellValue("隐患名称"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 5)); cell = row.createCell(3); cell.setCellValue("位置描述"); cell.setCellStyle(cellStyle); cell = row.createCell(4); cell.setCellStyle(cellStyle); cell = row.createCell(5); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 10)); cell = row.createCell(6); cell.setCellValue("管线情况"); cell.setCellStyle(cellStyle); cell = row.createCell(7); cell.setCellStyle(cellStyle); cell = row.createCell(8); cell.setCellStyle(cellStyle); cell = row.createCell(9); cell.setCellStyle(cellStyle); cell = row.createCell(10); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 11, 13)); cell = row.createCell(11); cell.setCellValue("占压物情况"); cell.setCellStyle(cellStyle); cell = row.createCell(12); cell.setCellStyle(cellStyle); cell = row.createCell(13); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 14, 14)); cell = row.createCell(14); cell.setCellValue("占压物用途"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 15, 15)); cell = row.createCell(15); cell.setCellValue("已采用的安全防护措施"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 16, 16)); cell = row.createCell(16); cell.setCellValue("备注"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 17, 17)); cell = row.createCell(17); cell.setCellValue("联系人电话"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 18, 18)); cell = row.createCell(18); cell.setCellValue("是否已和区管委和供热办联系"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 19, 19)); cell = row.createCell(19); cell.setCellValue("是否采取防范措施"); cell.setCellStyle(cellStyle); //--------------------------- 创建第四行-------------------- row = sheet.createRow(3); sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 3)); cell = row.createCell(3); cell.setCellValue("所在区县"); cell.setCellStyle(cellStyle); cell = row.createCell(0); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 4)); cell = row.createCell(4); cell.setCellValue("所在街道"); cell.setCellStyle(cellStyle); cell = row.createCell(1); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 5)); cell = row.createCell(5); cell.setCellValue("详细地址"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 6, 6)); cell = row.createCell(6); cell.setCellValue("管线建成时间"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 7, 7)); cell = row.createCell(7); cell.setCellValue("管线埋深"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 8, 8)); cell = row.createCell(8); cell.setCellValue("管径"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 9, 9)); cell = row.createCell(9); cell.setCellValue("管线压力等级"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 10, 10)); cell = row.createCell(10); cell.setCellValue("占压管线长度"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 11, 11)); cell = row.createCell(11); cell.setCellValue("占压单位(个人)名称"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 12, 12)); cell = row.createCell(12); cell.setCellValue("占压物建成时间"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 13, 13)); cell = row.createCell(13); cell.setCellValue("占压物面积(平方米)"); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 14, 14)); cell = row.createCell(14); cell.setCellValue("经营、出租、自用、居住"); cell.setCellStyle(cellStyle); cell = row.createCell(15); cell.setCellStyle(cellStyle); cell = row.createCell(16); cell.setCellStyle(cellStyle); cell = row.createCell(17); cell.setCellStyle(cellStyle); cell = row.createCell(18); cell.setCellStyle(cellStyle); cell = row.createCell(19); cell.setCellStyle(cellStyle); //-------------------------表头end--------------------- CriteriaQuery cq = new CriteriaQuery(HAqscTieupsummaryEntity.class, dataGrid); org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hAqscTieupsummary, request.getParameterMap()); List hAqscTieupsummarys = this.hAqscTieupsummaryService .getListByCriteriaQuery(cq, false); for (int i = 0; i < hAqscTieupsummarys.size(); i++) { //向表格插入数据 List data = new ArrayList(); //将前台传来的数据存入到list中 // System.out.println(hAqscTieupsummarys.get(i).getSeqNum()); HAqscTieupsummaryEntity entity = hAqscTieupsummarys.get(i); data.add(entity.getSeqNum()); String yhjb = entity.getYhDjpd(); String hyjb = dao.getHyjb(yhjb); data.add(hyjb); //隐患级别 data.add(entity.getYhName()); String countryName = dao.getCountryByCode(entity.getAtcounty()); //区县 data.add(countryName); String code = entity.getAtdistrict(); String streetName = dao.getStreetByCode(code); //街道 data.add(streetName); data.add(entity.getAddress()); Date buildtime = entity.getPipelineBuildtime(); if (buildtime!=null) { String format = df.format(buildtime); data.add(format); }else{ data.add(""); } data.add(entity.getPipelineDepth()); data.add(entity.getPipeSize()); data.add(entity.getPipelinePr()); data.add(entity.getTppipelineLength()); data.add(entity.getTieupName()); Date goodsBuildtime = entity.getTieupgoodsBuildtime(); if (buildtime!=null) { String format = df.format(goodsBuildtime); data.add(format); }else{ data.add(""); } data.add(entity.getTieupgoodsArea()); String useType = entity.getTieupgoodsUse(); data.add(dao.getUseType(useType)); data.add(entity.getUseSecuritymeasures()); data.add(entity.getRemark()); data.add(entity.getTelephone()); data.add(dao.getIsContact(entity.getIsContact())); data.add(entity.getIsUsesecuritymeasures()); int rowNum = 4+i; //从第四行开始 row = sheet.createRow(rowNum); for (int j = 0; j < data.size(); j++) { //将数据添加到单元格中 // System.out.println(data.get(j)); sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, j, j)); cell = row.createCell(j); cell.setCellValue(""+data.get(j)+""); cell.setCellStyle(cellStyle); } } // 输出Excel文件 OutputStream output = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=details.xls"); //filename = 文件名 response.setContentType("application/msexcel"); wb.write(output); output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }