1. 程式人生 > >spring boot後端匯出excel表

spring boot後端匯出excel表

最近在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這樣一層一層的編碼)