POI之Excel導出
阿新 • • 發佈:2018-11-22
lose bottom ucc setborder 數據字典 xls ++ body ice
1,在maven的pom文件中添加依賴
1 <dependency>
2 <groupId>org.apache.poi</groupId>
3 <artifactId>poi-ooxml</artifactId>
4 <version>${poi.version}</version>
5 </dependency>
2,在Controller中添加轉發接口
1 @ResponseBody
2 @RequestMapping(value="exportExcel", method={RequestMethod.GET, RequestMethod.POST})
3 public String exportExcel(HttpServletRequest request, String name, String data) {
4 JSONObject result = new JSONObject();
5 result.put("code", 0);
6 result.put("msg", "success");
7 try {
8 HSSFWorkbook workbook = new HSSFWorkbook();
9 Method method = IExportExcelService.class .getDeclaredMethod(name + "Export", new Class[]{HSSFWorkbook.class, String.class});
10 method.invoke(exportExcelService, workbook, data);
11 String filename = name + GhFormatUtils.getFileDate() + ".xls";
12 String realpath = request.getServletContext().getRealPath("/");
13 String filepath = filedir + "/" + filename;
14 FileOutputStream fos = new FileOutputStream(realpath + filepath);
15 workbook.write(fos);
16 fos.close();
17 workbook.close();
18 result.put("data", filepath);
19 } catch (Exception e) {
20 e.printStackTrace();
21 result.put("code", -1);
22 result.put("msg", e.getMessage());
23 }
24 return result.toString();
25 }
3,在Service層編寫相應生成格式的pdf
1 //公共函數
2 public void exportExcel(HSSFSheet sheet, String[] keys, JSONArray data) throws Exception {
3 sheet.setDefaultColumnWidth(15);
4 JSONObject head = (JSONObject)data.remove(0);
5 int lastRowNum = sheet.getLastRowNum();
6 //創建標題行
7 HSSFRow headRow = sheet.createRow(++lastRowNum);
8 HSSFCellStyle headerStyle = sheet.getWorkbook().createCellStyle();
9 headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
10 headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
11 headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
12 headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
13 headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
14 headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
15 for(int i = 0; i < keys.length; i++) {
16 HSSFCell cell = headRow.createCell(i);
17 cell.setCellValue(head.getString(keys[i]));
18 cell.setCellStyle(headerStyle);
19 }
20 HSSFCellStyle dataStyle = sheet.getWorkbook().createCellStyle();
21 dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
22 dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
23 dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
24 dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
25 //創建數據明細行
26 for(int i = 0; i < data.size(); i++) {
27 HSSFRow row = sheet.createRow(++lastRowNum);
28 JSONObject rowData = data.getJSONObject(i);
29 for(int j = 0; j < keys.length; j++) {
30 HSSFCell cell = row.createCell(j);
31 cell.setCellValue(rowData.getString(keys[j]));
32 cell.setCellStyle(dataStyle);
33 }
34 }
35 }
36 //具體處理函數
37 @Override
38 public void offerExport(HSSFWorkbook workbook, String dataStr) throws Exception {
39 JSONObject data = JSONObject.parseObject(dataStr);
40 JSONArray detail = data.getJSONArray("detail");
41 Map<String, String> dict = basMgmtService.getDictMap("col1");
42 //數據處理,用數據字典翻譯數據
43 for(int i = 0; i < detail.size(); i++) {
44 JSONObject obj = detail.getJSONObject(i);
45 obj.put("col1", dict.get(obj.getString("col1")));
46 }
47 JSONObject head = new JSONObject();
48 String keys[] = {"col1","col2"};
49 head.putAll(new HashMap<String, String>(){{
50 put(keys[0], "第一列名稱");
51 put(keys[1], "第二列名稱");
52 //......
53 }});
54 detail.add(0, head);
55 //創建工作頁
56 HSSFSheet sheet = workbook.createSheet(data.getString("titleName"));
57 //創建表頭標題
58 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, keys.length - 1));
59 HSSFRow titleRow = sheet.createRow(0);
60 HSSFCell titleCell = titleRow.createCell(0);
61 titleCell.setCellValue(data.getString("titleName"));
62 HSSFCellStyle titleStyle = workbook.createCellStyle();
63 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
64 titleCell.setCellStyle(titleStyle);
65 exportExcel(sheet, keys, detail);
66 }
4,前端請求
1 //公共導出Excel
2 function commExcel(url, name, data, appPath) {
3 $.ajax({
4 url : url,
5 type : ‘post‘,
6 dataType : "json",
7 data : {
8 name : name,
9 data : JSON.stringify(data)
10 },
11 async : true,
12 success : function(result,status,xhr) {
13 if(result.code == 0) {
14 $("<a href=" + appPath + result.data + " />")[0].click();
15 } else {
16 var msg = result.msg;
17 if(!msg) {
18 msg = "請求失敗,請手動刷新頁面!";
19 }
20 top.layer.alert(msg);
21 }
22 },
23 error : function(xhr,status,err) {
24 top.layer.alert(err);
25 }
26 });
27 }
28 //頁面導出Excel
29 function exportExcel() {
30 var url = "${adminPath}/finance/export/exportExcel";
31 var data = {
32 titleName : "excel名稱",
33 customerName: "用戶名稱",
34 detail : detail
35 };
36 commExcel(url, "offer", data, "${appPath}");
37 }
POI之Excel導出