|
@@ -1,12 +1,19 @@
|
|
|
package com.persagy.ibms.data.sdk.util;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.poi.xssf.streaming.SXSSFSheet;
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
@@ -14,15 +21,21 @@ import org.springframework.http.ResponseEntity;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.ibms.core.util.FastJsonReaderUtil;
|
|
|
+
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelWriter;
|
|
|
|
|
|
public class ExportUtil {
|
|
|
- public static ResponseEntity<FileSystemResource> export(File file) {
|
|
|
+ private static ResponseEntity<FileSystemResource> export(File file) {
|
|
|
if (file == null) {
|
|
|
return null;
|
|
|
}
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
- headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".xlsx");
|
|
|
+ headers.add("Content-Disposition", "attachment; filename=" + sdf.format(new Date()) + ".xlsx");
|
|
|
headers.add("Pragma", "no-cache");
|
|
|
headers.add("Expires", "0");
|
|
|
headers.add("Last-Modified", new Date().toString());
|
|
@@ -32,31 +45,83 @@ public class ExportUtil {
|
|
|
.body(new FileSystemResource(file));
|
|
|
}
|
|
|
|
|
|
- public static ResponseEntity<FileSystemResource> history_alarm_export(JSONObject paramObject){
|
|
|
- JSONObject result = RWDAlarmUtil.post_filter_and_page(paramObject);
|
|
|
+ public static ResponseEntity<FileSystemResource> alarm_export(JSONObject result) throws Exception {
|
|
|
+ JSONArray alarm_export_title = FastJsonReaderUtil.Instance()
|
|
|
+ .ReadJSONArray(new File(Constant.GetPath() + Constant.getSeperator() + "alarm_export_title.json"));
|
|
|
+ List<String> codeList = new ArrayList<String>();
|
|
|
+ List<String> titleList = new ArrayList<String>();
|
|
|
+ Map<String, String> titleMap = new HashMap<String, String>();
|
|
|
+ for (int i = 0; i < alarm_export_title.size(); i++) {
|
|
|
+ JSONObject title = alarm_export_title.getJSONObject(i);
|
|
|
+ String jsonKey = title.getString("jsonKey");
|
|
|
+ String headerName = title.getString("headerName");
|
|
|
+ codeList.add(jsonKey);
|
|
|
+ titleList.add(headerName);
|
|
|
+ titleMap.put(jsonKey, headerName);
|
|
|
+ }
|
|
|
JSONArray content = result.getJSONArray("content");
|
|
|
- List<String> title = new ArrayList<String>();
|
|
|
- title.add("itemName");
|
|
|
- title.add("treatState");
|
|
|
ExcelSheet sheet = new ExcelSheet();
|
|
|
- sheet.titleColList = title;
|
|
|
+ sheet.titleColList = titleList;
|
|
|
for (int i = 0; i < content.size(); i++) {
|
|
|
JSONObject contentItem = content.getJSONObject(i);
|
|
|
List<String> contentList = new ArrayList<String>();
|
|
|
- for (String titleItem : title) {
|
|
|
+ for (String titleItem : codeList) {
|
|
|
contentList.add(contentItem.getString(titleItem));
|
|
|
}
|
|
|
sheet.contentListList.add(contentList);
|
|
|
}
|
|
|
Map<String, ExcelSheet> sheetMap = new HashMap<String, ExcelSheet>();
|
|
|
sheetMap.put("Sheet1", sheet);
|
|
|
- String filepath = "abc.xlsx";
|
|
|
+ String filepath = UUID.randomUUID().toString() + ".xlsx";
|
|
|
try {
|
|
|
- ExcelUtil.Write(filepath, sheetMap);
|
|
|
+ ExcelCommonUtil.Write(filepath, sheetMap);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return ExportUtil.export(new File(filepath));
|
|
|
}
|
|
|
|
|
|
+ public static void alarm_export(JSONObject result, HttpServletResponse response) throws Exception {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
|
|
|
+ JSONArray alarm_export_title = FastJsonReaderUtil.Instance()
|
|
|
+ .ReadJSONArray(new File(Constant.GetPath() + Constant.getSeperator() + "alarm_export_title.json"));
|
|
|
+ JSONArray content = result.getJSONArray("content");
|
|
|
+ String xlsFileName = sdf.format(new Date()) + ".xlsx";
|
|
|
+ ExcelWriter writer = ExcelUtil.getBigWriter();
|
|
|
+ // 设置所有列为自动宽度,不考虑合并单元格
|
|
|
+ SXSSFSheet sheet = (SXSSFSheet) writer.getSheet();
|
|
|
+ sheet.trackAllColumnsForAutoSizing();
|
|
|
+ List<String> rowHead = new ArrayList<String>();
|
|
|
+ for (int i = 0; i < alarm_export_title.size(); i++) {
|
|
|
+ JSONObject title = alarm_export_title.getJSONObject(i);
|
|
|
+ String jsonKey = title.getString("jsonKey");
|
|
|
+ String headerName = title.getString("headerName");
|
|
|
+ rowHead.add(jsonKey);
|
|
|
+ writer.addHeaderAlias(jsonKey, headerName);
|
|
|
+ }
|
|
|
+ // isWriteKeyAsHead为true的时候自动添加表头,与writeHeadRow不能同时存在,否则为双表头
|
|
|
+ writer.writeHeadRow(rowHead);
|
|
|
+ // 表头冻结
|
|
|
+ writer.setFreezePane(1);
|
|
|
+ writer.setOnlyAlias(true);
|
|
|
+ // setOnlyAlias必须在Write之前添加,否则总会在第一列多产生一个表头,也就是是说事后诸葛亮是不生效的
|
|
|
+ writer.write(content, false);
|
|
|
+ // 此方法必须在指定列数据完全写出后调用才有效
|
|
|
+ // writer.autoSizeColumnAll(); write方法中列数量变成了-1,导致autoSizeColumnAll不生效
|
|
|
+ for (int i = 0; i < rowHead.size(); i++) {
|
|
|
+ // 调整每一列宽度
|
|
|
+ sheet.autoSizeColumn((short) i);
|
|
|
+ // 解决自动设置列宽中文失效的问题
|
|
|
+ int columnWidth = (sheet.getColumnWidth(i) * 16 / 10) > 25000 ? 25000 : (sheet.getColumnWidth(i) * 16 / 10);
|
|
|
+ sheet.setColumnWidth(i, columnWidth);
|
|
|
+ }
|
|
|
+ xlsFileName = URLEncoder.encode(xlsFileName, "UTF-8");
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + xlsFileName);
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
+ writer.flush(out, true);
|
|
|
+ writer.close();
|
|
|
+ IoUtil.close(out);
|
|
|
+ }
|
|
|
+
|
|
|
}
|