Browse Source

导出部分调整,自动列宽还有问题

menglu 3 years ago
parent
commit
ff80579a2a

+ 1 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/AlarmApi.java

@@ -29,7 +29,7 @@ public class AlarmApi {
 	public ResponseEntity<FileSystemResource> alarm_export_post_poi(@RequestBody String param) throws Exception {
 		JSONObject paramObject = JSON.parseObject(param);
 		JSONObject result = RestUtil.post_filter_and_page(paramObject);
-		ResponseEntity<FileSystemResource> fsr = ExportUtil.alarm_export(result);
+		ResponseEntity<FileSystemResource> fsr = ExportUtil.alarm_export_poi(result);
 		return fsr;
 	}
 

+ 1 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/fjd_Api.java

@@ -33,7 +33,7 @@ public class fjd_Api {
 	@PostMapping(path = { "/fjd_query_history_batch_export_poi",
 			"/zkt-sdk/fjd_query_history_batch_export_poi" }, produces = "application/json;charset=UTF-8")
 	public ResponseEntity<FileSystemResource> fjd_query_history_batch_export_poi(@RequestBody String param) throws Exception {
-		ResponseEntity<FileSystemResource> fsr = fjd_Util.fjd_query_history_batch_export(param);
+		ResponseEntity<FileSystemResource> fsr = fjd_Util.fjd_query_history_batch_export_poi(param);
 		return fsr;
 	}
 

+ 3 - 3
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/fjd_Util.java

@@ -92,17 +92,17 @@ public class fjd_Util {
 		return JSONObject.toJSONString(result, SerializerFeature.WriteMapNullValue);
 	}
 
-	public static ResponseEntity<FileSystemResource> fjd_query_history_batch_export(String param) throws Exception {
+	public static ResponseEntity<FileSystemResource> fjd_query_history_batch_export_poi(String param) throws Exception {
 		JSONArray parseArray = JSON.parseArray(param);
 		fjd_query_history_batch_fill(parseArray);
-		ResponseEntity<FileSystemResource> fsr = ExportUtil.fjd_export(parseArray);
+		ResponseEntity<FileSystemResource> fsr = ExportUtil.fjd_export_poi(parseArray);
 		return fsr;
 	}
 
 	public static void fjd_query_history_batch_export_hutool(String param, HttpServletResponse response) throws Exception {
 		JSONArray parseArray = JSON.parseArray(param);
 		fjd_query_history_batch_fill(parseArray);
-		ExportUtil.fjd_export(parseArray, response);
+		ExportUtil.fjd_export_hutool(parseArray, response);
 	}
 
 	public static void fjd_query_history_batch_fill(JSONArray parseArray) throws Exception {

+ 29 - 15
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/ExcelCommonUtil.java

@@ -1,7 +1,6 @@
 package com.persagy.ibms.data.sdk.util;
 
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
@@ -11,13 +10,12 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.usermodel.WorkbookFactory;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 public class ExcelCommonUtil {
 
@@ -81,14 +79,7 @@ public class ExcelCommonUtil {
 		return content;
 	}
 
-	public static void Write(String filepath, Map<String, ExcelSheet> sheetMap) throws Exception {
-		Workbook wb;
-		if (filepath.endsWith("xlsx")) {
-			wb = new XSSFWorkbook();
-		} else {
-			wb = new HSSFWorkbook();
-		}
-
+	public static void Write(Map<String, ExcelSheet> sheetMap, Workbook wb) throws Exception {
 		String[] names = sheetMap.keySet().toArray(new String[0]);
 		Arrays.sort(names);
 		for (String name : names) {
@@ -132,17 +123,40 @@ public class ExcelCommonUtil {
 				for (int i = 0; i < sheet.titleColList.size(); i++) {
 					st.autoSizeColumn((short) 0);
 				}
+				setSizeColumn(st, sheet.titleColList.size());
 			}
 			// 锁定第一列
 			if (sheet.titleRowList != null && sheet.titleRowList.size() > 0) {
 				st.createFreezePane(1, 0, 1, 0);
 			}
 		}
+	}
+
+	// 自适应宽度(中文支持)
+	private static void setSizeColumn(Sheet sheet, int size) {
+		for (int columnNum = 0; columnNum < size; columnNum++) {
+			int columnWidth = sheet.getColumnWidth(columnNum) / 256;
+			for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
+				Row currentRow;
+				// 当前行未被使用过
+				if (sheet.getRow(rowNum) == null) {
+					currentRow = sheet.createRow(rowNum);
+				} else {
+					currentRow = sheet.getRow(rowNum);
+				}
 
-		FileOutputStream os = new FileOutputStream(filepath);
-		wb.write(os);
-		os.close();
-		wb.close();
+				if (currentRow.getCell(columnNum) != null) {
+					Cell currentCell = currentRow.getCell(columnNum);
+					if (currentCell.getCellType() == CellType.STRING) {
+						int length = currentCell.getStringCellValue().getBytes().length;
+						if (columnWidth < length) {
+							columnWidth = length;
+						}
+					}
+				}
+			}
+			sheet.setColumnWidth(columnNum, columnWidth * 256);
+		}
 	}
 
 	public static Map<String, ExcelSheet> Read(String filepath, boolean titleCol) throws Exception {

+ 181 - 5
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/ExportUtil.java

@@ -1,6 +1,7 @@
 package com.persagy.ibms.data.sdk.util;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -13,7 +14,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.streaming.SXSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -47,7 +51,7 @@ public class ExportUtil {
 				.body(new FileSystemResource(file));
 	}
 
-	public static ResponseEntity<FileSystemResource> alarm_export(JSONObject result) throws Exception {
+	public static ResponseEntity<FileSystemResource> alarm_export_poi(JSONObject result) throws Exception {
 		JSONArray alarm_export_title = FastJsonReaderUtil.Instance().ReadJSONArray(FileUtil.file("alarm_export_title.json"));
 		List<String> codeList = new CopyOnWriteArrayList<String>();
 		List<String> titleList = new CopyOnWriteArrayList<String>();
@@ -75,7 +79,17 @@ public class ExportUtil {
 		sheetMap.put("Sheet1", sheet);
 		String filepath = UUID.randomUUID().toString() + ".xlsx";
 		try {
-			ExcelCommonUtil.Write(filepath, sheetMap);
+			Workbook wb;
+			if (filepath.endsWith("xlsx")) {
+				wb = new XSSFWorkbook();
+			} else {
+				wb = new HSSFWorkbook();
+			}
+			ExcelCommonUtil.Write(sheetMap, wb);
+			FileOutputStream os = new FileOutputStream(filepath);
+			wb.write(os);
+			wb.close();
+			os.close();
 		} catch (Exception e) {
 			log.error(e.getMessage(), e);
 		}
@@ -83,6 +97,158 @@ public class ExportUtil {
 	}
 
 	public static void alarm_export(JSONObject result, HttpServletResponse response) throws Exception {
+		JSONArray content = result.getJSONArray("content");
+		ExcelSheet sheet = new ExcelSheet();
+		for (int i = 0; i < content.size(); i++) {
+			List<String> contentList = new CopyOnWriteArrayList<String>();
+			sheet.contentListList.add(contentList);
+		}
+		SimpleDateFormat sdf_T = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+		SimpleDateFormat sdf_xie = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+		{
+			sheet.titleColList.add("报警开始时间");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("triggerTime")) {
+					String triggerTime = contentItem.getString("triggerTime");
+					cellValue = sdf_xie.format(sdf_T.parse(triggerTime));
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("持续时间(分钟)");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("durationMilli")) {
+					long durationMilli = contentItem.getLong("durationMilli");
+					long minute = durationMilli / (1000L * 60);
+					cellValue = "" + minute;
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警处理状态");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("treatStateName")) {
+					cellValue = contentItem.getString("treatStateName");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警級別");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("levelName")) {
+					cellValue = contentItem.getString("levelName");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警对象名称");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("objName")) {
+					cellValue = contentItem.getString("objName");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警对象编码");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("objId")) {
+					cellValue = contentItem.getString("objId");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警描述");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("remark")) {
+					cellValue = contentItem.getString("remark");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("报警类型");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("itemName")) {
+					cellValue = contentItem.getString("itemName");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("批注类型");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("treatModeName")) {
+					cellValue = contentItem.getString("treatModeName");
+				}
+				contentList.add(cellValue);
+			}
+		}
+		{
+			sheet.titleColList.add("是否重点关注");
+			for (int i = 0; i < content.size(); i++) {
+				JSONObject contentItem = content.getJSONObject(i);
+				List<String> contentList = sheet.contentListList.get(i);
+				String cellValue = null;
+				if (contentItem.containsKey("concern")) {
+					long durationMilli = contentItem.getLong("concern");
+					cellValue = durationMilli == 1 ? "是" : "否";
+				}
+				contentList.add(cellValue);
+			}
+		}
+		Map<String, ExcelSheet> sheetMap = new ConcurrentHashMap<String, ExcelSheet>();
+		sheetMap.put("Sheet1", sheet);
+		try {
+			Workbook wb = new XSSFWorkbook();
+			ExcelCommonUtil.Write(sheetMap, wb);
+			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
+			String xlsFileName = sdf.format(new Date()) + ".xlsx";
+			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();
+			wb.write(out);
+			wb.close();
+			out.close();
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+		}
+	}
+
+	public static void alarm_export_hutool(JSONObject result, HttpServletResponse response) throws Exception {
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
 		JSONArray alarm_export_title = FastJsonReaderUtil.Instance().ReadJSONArray(FileUtil.file("alarm_export_title.json"));
 		JSONArray content = result.getJSONArray("content");
@@ -124,7 +290,7 @@ public class ExportUtil {
 		IoUtil.close(out);
 	}
 
-	public static ResponseEntity<FileSystemResource> fjd_export(JSONArray parseArray) throws Exception {
+	public static ResponseEntity<FileSystemResource> fjd_export_poi(JSONArray parseArray) throws Exception {
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 		SimpleDateFormat sdf_ = new SimpleDateFormat("yyyyMMddHHmmss");
 		Map<String, ExcelSheet> sheetMap = new ConcurrentHashMap<String, ExcelSheet>();
@@ -148,14 +314,24 @@ public class ExportUtil {
 
 		String filepath = UUID.randomUUID().toString() + ".xlsx";
 		try {
-			ExcelCommonUtil.Write(filepath, sheetMap);
+			Workbook wb;
+			if (filepath.endsWith("xlsx")) {
+				wb = new XSSFWorkbook();
+			} else {
+				wb = new HSSFWorkbook();
+			}
+			ExcelCommonUtil.Write(sheetMap, wb);
+			FileOutputStream os = new FileOutputStream(filepath);
+			wb.write(os);
+			wb.close();
+			os.close();
 		} catch (Exception e) {
 			log.error(e.getMessage(), e);
 		}
 		return ExportUtil.export(new File(filepath));
 	}
 
-	public static void fjd_export(JSONArray parseArray, HttpServletResponse response) throws Exception {
+	public static void fjd_export_hutool(JSONArray parseArray, HttpServletResponse response) throws Exception {
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 		SimpleDateFormat sdf_ = new SimpleDateFormat("yyyyMMddHHmmss");
 		String xlsFileName = sdf.format(new Date()) + ".xlsx";