最近在spring boot项目中的任务是后端导出excel表,做了比较长的时间,由于前端还未把派工表导出的页面写好,我后端测试不好做,只能使用debug进行测试,一直出不来excel表,在经过多次尝试后,终于成功了。以下是我的代码分享。
1 package com.nbst.controller.mes; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import org.apache.poi.hssf.usermodel.HSSFCell; 15 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 16 import org.apache.poi.hssf.usermodel.HSSFDataFormat; 17 import org.apache.poi.hssf.usermodel.HSSFFont; 18 import org.apache.poi.hssf.usermodel.HSSFRow; 19 import org.apache.poi.hssf.usermodel.HSSFSheet; 20 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 21 import org.apache.poi.ss.util.CellRangeAddress; 22 import org.springframework.beans.factory.annotation.Autowired; 23 import org.springframework.stereotype.Controller; 24 import org.springframework.util.StringUtils; 25 import org.springframework.web.bind.annotation.RequestMapping; 26 27 import com.nbst.comnutil.Tools; 28 import com.nbst.dao.mapper.mes.DepartmentMapper; 29 import com.nbst.dao.mapper.mes.DispatchedWorkersMapper; 30 import com.nbst.dao.mapper.mes.UserMapper; 31 import com.nbst.model.base.Department; 32 import com.nbst.model.base.User; 33 import com.nbst.model.mes.DispatchedWorkers; 34 import com.nbst.service.mes.IDownloadService; 35 36 @Controller 37 @RequestMapping("/download") 38 public class DownloadController { 39 40 @Autowired 41 DispatchedWorkersMapper dispatchedWorkersMapper; 42 43 @Autowired 44 IDownloadService downloadService; 45 46 @Autowired 47 DepartmentMapper departmentMapper; 48 49 @Autowired 50 UserMapper userMapper; 58 59 @RequestMapping(value = "/downloadDisp.action") 60 public void getUserinfoSalaryTable(HttpServletRequest request, HttpServletResponse response, 61 DispatchedWorkers record, String fstartTime, String fendTime) throws IOException { 62 Map map = new HashMap(); 63 if (!StringUtils.isEmpty(fstartTime) && !StringUtils.isEmpty(fendTime)) { 64 map.put("fDate", fstartTime); 65 } else { 66 map = Tools.transBean2Map(record); 67 } 68 List<DispatchedWorkers> list = dispatchedWorkersMapper.findByCondition(map); 69 70 // 初始一个workbook 71 HSSFWorkbook workbook = new HSSFWorkbook(); 72 73 // 设置单元格类型 74 HSSFCellStyle cellStyle = workbook.createCellStyle(); 75 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平布局:居中 76 cellStyle.setWrapText(true); 77 78 HSSFSheet sheet = workbook.createSheet(); 79 // 在sheet中添加表头第0行 80 // 用于格式化单元格的数据 81 HSSFDataFormat format = workbook.createDataFormat(); 82 HSSFCellStyle stylecol = workbook.createCellStyle(); 83 stylecol.setDataFormat(format.getFormat("@")); 84 85 CellRangeAddress aa = new CellRangeAddress(0, 0, 0, 6); // 合并单元格 86 HSSFRow rowthead = sheet.createRow((int) 0); 87 88 sheet.addMergedRegion(aa); 89 HSSFRow row = sheet.createRow((int) 1); 90 sheet.autoSizeColumn(1, true); 91 92 HSSFFont font = workbook.createFont(); 93 HSSFFont fontthead = workbook.createFont(); 94 // font.setFontName("黑体"); 95 fontthead.setFontHeightInPoints((short) 16);// 设置字体大小 96 // font.setFontName("仿宋_GB2312"); 97 fontthead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 98 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 99 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 100 101 // 创建单元格,并设置值表头 设置表头居中 102 HSSFCellStyle style = workbook.createCellStyle(); 103 HSSFCellStyle stylethead = workbook.createCellStyle(); 104 stylethead.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 105 stylethead.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框 106 stylethead.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 107 stylethead.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 108 stylethead.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 109 110 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 111 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框 112 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 113 style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 114 style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 115 stylethead.setFont(fontthead); 116 117 HSSFCell cellthead = rowthead.createCell(0); 118 119 Date date = new Date(); 120 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 121 122 cellthead.setCellValue(sdf.format(date)+ "派工表");//表内设置表头信息 123 cellthead.setCellStyle(stylethead); 124 125 HSSFCell cell = row.createCell(0); 126 127 cell.setCellValue("部门");// 部门id 128 cell.setCellStyle(style); 129 sheet.setDefaultColumnStyle(0, stylecol); 130 131 cell = row.createCell(1); 132 cell.setCellValue("员工");// 员工ID 133 cell.setCellStyle(style); 134 sheet.setDefaultColumnStyle(1, stylecol); 135 sheet.setColumnWidth(1, (int) (35.7 * 200)); 136 137 cell = row.createCell(2); 138 cell.setCellValue("时间"); 139 cell.setCellStyle(style); 140 sheet.setDefaultColumnStyle(2, stylecol); 141 sheet.setColumnWidth(2, (int) (35.7 * 320)); 142 143 cell = row.createCell(3); 144 cell.setCellValue("制单人"); 145 cell.setCellStyle(style); 146 sheet.setDefaultColumnStyle(3, stylecol); 147 sheet.setColumnWidth(3, (int) (35.7 * 400)); 148 149 cell = row.createCell(4); 150 cell.setCellValue("设备");// 设备id 151 cell.setCellStyle(style); 152 sheet.setDefaultColumnStyle(4, stylecol); 153 sheet.setColumnWidth(4, (int) (35.7 * 60)); 154 155 cell = row.createCell(5); 156 cell.setCellValue("工序"); 157 cell.setCellStyle(style); 158 sheet.setDefaultColumnStyle(5, stylecol); 159 sheet.setColumnWidth(5, (int) (35.7 * 120)); 160 cell = row.createCell(6); 161 cell.setCellValue("派工数量"); 162 cell.setCellStyle(style); 163 sheet.setDefaultColumnStyle(6, stylecol); 164 sheet.setColumnWidth(6, (int) (35.7 * 120)); 165 166 // 写入实体数据 167 for (int i = 0; i < list.size(); i++) { 168 169 Map m0 = new HashMap();// 部门ID-->部门名称 170 m0.put("id", list.get(0).getfDepid()); 171 List<Department> l0 = departmentMapper.findByCondition(map); 172 173 Map m1 = new HashMap();// 员工ID-->员工姓名 174 m1.put("id", list.get(0).getfWorker()); 175 List<User> l1 = userMapper.findByCondition(map); 176 177 Map m2 = new HashMap();// 设备ID-->设备名称 178 m2.put("id", list.get(0).getfDepid()); 179 List<Department> l2 = departmentMapper.findByCondition(map); 180 181 row = sheet.createRow((int) i + 2); 182 // ,创建单元格,并设置值 183 cell = row.createCell(0);// 部门 184 cell.setCellValue(l0.get(i).getfName()); 185 cell.setCellStyle(style); 186 187 cell = row.createCell(1);// 员工 188 cell.setCellValue(l1.get(i).getName()); 189 cell.setCellStyle(style); 190 191 cell = row.createCell(2);// 时间 192 cell.setCellValue(list.get(i).getfDate()); 193 cell.setCellStyle(style); 194 195 cell = row.createCell(3);// 制单人 196 cell.setCellValue(list.get(i).getfBiller()); 197 cell.setCellStyle(style); 198 199 cell = row.createCell(4);// 设备 200 cell.setCellValue(l2.get(i).getfName()); 201 cell.setCellStyle(style); 202 203 cell = row.createCell(5);// 工序 204 cell.setCellValue(list.get(i).getfOper()); 205 cell.setCellStyle(style); 206 cell = row.createCell(6);// 派工数量 207 cell.setCellValue(list.get(i).getfTargetQty()); 208 cell.setCellStyle(style); 209 } 210 sheet.autoSizeColumn((short) 0); // 调整第1列宽度 211 sheet.setColumnWidth(0, "部门".length() * 256 * 2 + 4);// 汉字由手动设置宽度 212 sheet.setColumnWidth(1, "员工".length() * 256 * 2);// 汉字由手动设置宽度 213 sheet.setColumnWidth(2, "时间".length() * 256 * 3);// 汉字由手动设置宽度 214 sheet.setColumnWidth(3, "制单人".length() * 256 * 2);// 手动设置宽度 215 sheet.setColumnWidth(4, "设备".length() * 256 * 2);// 手动设置宽度 216 sheet.setColumnWidth(5, "工序".length() * 256 * 2 + 10);// 手动设置宽度 217 sheet.setColumnWidth(6, "派工数量".length() * 256 * 2 + 10);// 手动设置宽度 218 String filename = ""; 219 220 filename = "派工表"+sdf.format(date) +".xls";//保存的文件名。 221 222 filename = java.net.URLEncoder.encode(filename, "UTF-8"); 223 response.setContentType("application/octet-stream"); 224 response.setHeader("Content-disposition", "attachment;filename=" + filename); 225 OutputStream ouputStream = response.getOutputStream(); 226 workbook.write(ouputStream);//以流的形式输出 227 ouputStream.flush(); 228 ouputStream.close(); 229 } 230 }
pom.xm;需要添加依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency>
其实很多大佬都不建议直接将导出方法写在controller层,因为对任何一个项目而言,这种方法不够规范,而且一旦接口调用出问题,没有DownloadserviceImpl的支持,不好统一维护。但是由于这只是个代码展示,所以为了方便就这样写了。(希望大家在构建项目时可以按照service,serviceImpl,controller这样一层一层的编码)