From 3614b4f694d9d40708df6477d56a40b9311ad48a Mon Sep 17 00:00:00 2001 From: ZengTao <2773468879@qq.com> Date: 星期三, 31 一月 2024 14:50:17 +0800 Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/CanadaMes --- springboot-vue3/src/main/java/com/example/springboot/component/ExcelToJsonConverter.java | 48 ++++++++++++++++++++++++++++++++++-------------- 1 files changed, 34 insertions(+), 14 deletions(-) diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/ExcelToJsonConverter.java b/springboot-vue3/src/main/java/com/example/springboot/component/ExcelToJsonConverter.java index d1098ba..a361cef 100644 --- a/springboot-vue3/src/main/java/com/example/springboot/component/ExcelToJsonConverter.java +++ b/springboot-vue3/src/main/java/com/example/springboot/component/ExcelToJsonConverter.java @@ -12,22 +12,24 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; public class ExcelToJsonConverter { + public static void main(String[] args) { - String excelFilePath = "JsonFile/state.xlsx"; + String excelFilePath = "src/main/resources/JsonFile/alarm.xlsx"; String sheetName = "Sheet1"; int addressColumnIndex = 0; int nameColumnIndex = 1; - int unitColumnIndex = 2; - String outputFilePath = "JsonFile/state.json"; + //int unitColumnIndex = 2; + String outputFilePath = "src/main/resources/JsonFile/PlcAlarm.json"; - try (Workbook workbook = new HSSFWorkbook(new FileInputStream(excelFilePath))) { + try (Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath))) { Sheet sheet = workbook.getSheet(sheetName); - List<JSONObject> jsonList = new ArrayList<>(); + List<LinkedHashMap<String, Object>> jsonList = new ArrayList<>(); Iterator<Row> rowIterator = sheet.iterator(); // Skip the header row @@ -35,30 +37,50 @@ rowIterator.next(); } + int plcAddressLength = 0; // 璁板綍鎵�鏈� addressLenght 鐨勫拰 + int addressIndex = 0; // 鑷鐨勫湴鍧�绱㈠紩 while (rowIterator.hasNext()) { Row row = rowIterator.next(); Cell nameCell = row.getCell(nameColumnIndex); Cell addressCell = row.getCell(addressColumnIndex); - Cell unitCell = row.getCell(unitColumnIndex); + // Cell unitCell = row.getCell(unitColumnIndex); String name = nameCell.getStringCellValue(); String address = addressCell.getStringCellValue(); - String unit = unitCell.getStringCellValue(); + // String unit = unitCell.getStringCellValue(); - String addressIndex = extractAddressIndex(address); + // String addressIndex = extractAddressIndex(address); - JSONObject jsonObject = new JSONObject(); + LinkedHashMap<String, Object> jsonObject = new LinkedHashMap<>(); jsonObject.put("codeId", name); jsonObject.put("addressIndex", addressIndex); - jsonObject.put("unit", unit); + int addressLength = 0; + if (address.contains("~")) { + addressLength = 14; + } else { + addressLength = 2; + } + // jsonObject.put("addressLenght", String.valueOf(addressLength)); + + //jsonObject.put("unit", unit); + + plcAddressLength += addressLength; jsonList.add(jsonObject); + addressIndex++; } + LinkedHashMap<String, Object> resultObject = new LinkedHashMap<>(); + resultObject.put("plcAddressBegin", "DB100.0"); + resultObject.put("plcAddressLenght", String.valueOf(plcAddressLength)); + // resultObject.put("dataType", "word"); + resultObject.put("dataType", "bit"); + resultObject.put("parameteInfor", jsonList); + Gson gson = new Gson(); - String jsonOutput = gson.toJson(jsonList); + String jsonOutput = gson.toJson(resultObject); try (FileWriter fileWriter = new FileWriter(outputFilePath)) { fileWriter.write(jsonOutput); @@ -81,6 +103,4 @@ } return ""; } - - -} \ No newline at end of file +} -- Gitblit v1.8.0