Sfoglia il codice sorgente

excel文件流没关闭

menglu 3 anni fa
parent
commit
9eb5c55efd

+ 46 - 35
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/ExeclReadEquipStaticInfoUtil.java

@@ -1,44 +1,55 @@
 package com.persagy.ibms.data.sdk.util;
 
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+import org.apache.poi.ss.usermodel.Cell;
+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.ss.util.CellRangeAddress;
+
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-import java.io.File;
-import java.util.*;
 
 public class ExeclReadEquipStaticInfoUtil {
 
-    public static JSONObject ReadEquipStaticInfo(String firstName, String sheetName) throws Exception {
-        String filepath=Constant.GetPath() + Constant.getSeperator() + firstName+".xlsx";
-        Workbook workbook = WorkbookFactory.create(new File(filepath));
-        Sheet sheet = workbook.getSheet(sheetName);
-        JSONObject object=new JSONObject(new LinkedHashMap());
-        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
-            //获取合并单元格的值
-            CellRangeAddress ca = sheet.getMergedRegion(i);
-            int firstRow = ca.getFirstRow();
-            Row fRow = sheet.getRow(firstRow);
-            Cell fCell = fRow.getCell(0);
-            String caVal= getCellValue(fCell) ;
-            //获取合并单元格之后的值,形成树形结构
-            List dic = new JSONArray();
-            for (int j=firstRow;j<= ca.getLastRow();j++)
-            {
-                Row row=sheet.getRow(j);
-                JSONObject obj = new JSONObject();
-                obj.put("name", getCellValue(row.getCell(1)));
-                obj.put("code", getCellValue(row.getCell(2)));
-                obj.put("enable", row.getCell(3).getBooleanCellValue());
-                dic.add(obj);
-            }
-            object.put(caVal,dic);
-        }
-        return object;
-    }
+	public static JSONObject ReadEquipStaticInfo(String firstName, String sheetName) throws Exception {
+		String filepath = Constant.GetPath() + Constant.getSeperator() + firstName + ".xlsx";
+		FileInputStream fis = new FileInputStream(new File(filepath));
+		Workbook workbook = WorkbookFactory.create(new BufferedInputStream(fis));
+		Sheet sheet = workbook.getSheet(sheetName);
+		JSONObject object = new JSONObject(new LinkedHashMap());
+		for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
+			// 获取合并单元格的值
+			CellRangeAddress ca = sheet.getMergedRegion(i);
+			int firstRow = ca.getFirstRow();
+			Row fRow = sheet.getRow(firstRow);
+			Cell fCell = fRow.getCell(0);
+			String caVal = getCellValue(fCell);
+			// 获取合并单元格之后的值,形成树形结构
+			List dic = new JSONArray();
+			for (int j = firstRow; j <= ca.getLastRow(); j++) {
+				Row row = sheet.getRow(j);
+				JSONObject obj = new JSONObject();
+				obj.put("name", getCellValue(row.getCell(1)));
+				obj.put("code", getCellValue(row.getCell(2)));
+				obj.put("enable", row.getCell(3).getBooleanCellValue());
+				dic.add(obj);
+			}
+			object.put(caVal, dic);
+		}
+		fis.close();
+		return object;
+	}
 
-    public static String getCellValue(Cell cell){
-        if(cell == null) return "";
-        return cell.getStringCellValue();
-    }
+	public static String getCellValue(Cell cell) {
+		if (cell == null)
+			return "";
+		return cell.getStringCellValue();
+	}
 }