diff --git a/com.sq.customization/.classpath b/com.sq.customization/.classpath index eca7bdb..676ac3a 100644 --- a/com.sq.customization/.classpath +++ b/com.sq.customization/.classpath @@ -3,5 +3,23 @@ + + + + + + + + + + + + + + + + + + diff --git a/com.sq.customization/META-INF/MANIFEST.MF b/com.sq.customization/META-INF/MANIFEST.MF index e9dfa65..2db3415 100644 --- a/com.sq.customization/META-INF/MANIFEST.MF +++ b/com.sq.customization/META-INF/MANIFEST.MF @@ -20,7 +20,6 @@ Require-Bundle: org.eclipse.ui, com.teamcenter.rac.pca.kernel, com.teamcenter.rac.pse.pca, com.teamcenter.rac.cme.kernel, - org.apache.poi.39, TcSoaAdministrationRac;bundle-version="13000.3.0", TcSoaBomRac;bundle-version="13000.3.0", TcSoaCommon;bundle-version="13000.3.0", @@ -33,3 +32,22 @@ Bundle-ActivationPolicy: lazy Export-Package: com.sq.customization, com.sq.customization.dialogs, com.sq.customization.handlers +Bundle-ClassPath: lib/cglib-3.3.0.jar, + lib/commons-collections4-4.4.jar, + lib/commons-compress-1.21.jar, + lib/commons-io-2.11.0.jar, + lib/easyexcel-3.2.0.jar, + lib/easyexcel-core-3.2.0.jar, + lib/easyexcel-support-3.2.0.jar, + lib/log4j-api-2.17.1.jar, + lib/poi-5.2.1.jar, + lib/poi-examples-5.2.1.jar, + lib/poi-excelant-5.2.1.jar, + lib/poi-ooxml-5.2.1.jar, + lib/poi-ooxml-full-5.2.1.jar, + lib/serializer-2.7.2.jar, + lib/slf4j-api-1.7.36.jar, + lib/xalan-2.7.2.jar, + lib/xml-apis-1.4.01.jar, + lib/xmlbeans-5.0.3.jar, + . diff --git a/com.sq.customization/build.properties b/com.sq.customization/build.properties index c637e8e..cbdcc94 100644 --- a/com.sq.customization/build.properties +++ b/com.sq.customization/build.properties @@ -5,4 +5,22 @@ bin.includes = plugin.xml,\ META-INF/,\ .,\ icons/,\ - build.properties + build.properties,\ + lib/cglib-3.3.0.jar,\ + lib/commons-collections4-4.4.jar,\ + lib/commons-compress-1.21.jar,\ + lib/commons-io-2.11.0.jar,\ + lib/easyexcel-3.2.0.jar,\ + lib/easyexcel-core-3.2.0.jar,\ + lib/easyexcel-support-3.2.0.jar,\ + lib/log4j-api-2.17.1.jar,\ + lib/poi-5.2.1.jar,\ + lib/poi-examples-5.2.1.jar,\ + lib/poi-excelant-5.2.1.jar,\ + lib/poi-ooxml-5.2.1.jar,\ + lib/poi-ooxml-full-5.2.1.jar,\ + lib/serializer-2.7.2.jar,\ + lib/slf4j-api-1.7.36.jar,\ + lib/xalan-2.7.2.jar,\ + lib/xml-apis-1.4.01.jar,\ + lib/xmlbeans-5.0.3.jar diff --git a/com.sq.customization/lib/commons-collections4-4.4.jar b/com.sq.customization/lib/commons-collections4-4.4.jar new file mode 100644 index 0000000..da06c3e Binary files /dev/null and b/com.sq.customization/lib/commons-collections4-4.4.jar differ diff --git a/com.sq.customization/lib/commons-compress-1.21.jar b/com.sq.customization/lib/commons-compress-1.21.jar new file mode 100644 index 0000000..4892334 Binary files /dev/null and b/com.sq.customization/lib/commons-compress-1.21.jar differ diff --git a/com.sq.customization/lib/commons-io-2.11.0.jar b/com.sq.customization/lib/commons-io-2.11.0.jar new file mode 100644 index 0000000..be507d9 Binary files /dev/null and b/com.sq.customization/lib/commons-io-2.11.0.jar differ diff --git a/com.sq.customization/lib/log4j-api-2.17.1.jar b/com.sq.customization/lib/log4j-api-2.17.1.jar new file mode 100644 index 0000000..605c45d Binary files /dev/null and b/com.sq.customization/lib/log4j-api-2.17.1.jar differ diff --git a/com.sq.customization/lib/poi-5.2.1.jar b/com.sq.customization/lib/poi-5.2.1.jar new file mode 100644 index 0000000..9e068d3 Binary files /dev/null and b/com.sq.customization/lib/poi-5.2.1.jar differ diff --git a/com.sq.customization/lib/poi-examples-5.2.1.jar b/com.sq.customization/lib/poi-examples-5.2.1.jar new file mode 100644 index 0000000..70c8d11 Binary files /dev/null and b/com.sq.customization/lib/poi-examples-5.2.1.jar differ diff --git a/com.sq.customization/lib/poi-excelant-5.2.1.jar b/com.sq.customization/lib/poi-excelant-5.2.1.jar new file mode 100644 index 0000000..93d47d4 Binary files /dev/null and b/com.sq.customization/lib/poi-excelant-5.2.1.jar differ diff --git a/com.sq.customization/lib/poi-ooxml-5.2.1.jar b/com.sq.customization/lib/poi-ooxml-5.2.1.jar new file mode 100644 index 0000000..f6a9086 Binary files /dev/null and b/com.sq.customization/lib/poi-ooxml-5.2.1.jar differ diff --git a/com.sq.customization/lib/poi-ooxml-full-5.2.1.jar b/com.sq.customization/lib/poi-ooxml-full-5.2.1.jar new file mode 100644 index 0000000..67fa1f4 Binary files /dev/null and b/com.sq.customization/lib/poi-ooxml-full-5.2.1.jar differ diff --git a/com.sq.customization/lib/slf4j-api-1.7.36.jar b/com.sq.customization/lib/slf4j-api-1.7.36.jar new file mode 100644 index 0000000..7d3ce68 Binary files /dev/null and b/com.sq.customization/lib/slf4j-api-1.7.36.jar differ diff --git a/com.sq.customization/lib/xmlbeans-5.0.3.jar b/com.sq.customization/lib/xmlbeans-5.0.3.jar new file mode 100644 index 0000000..63b0ab8 Binary files /dev/null and b/com.sq.customization/lib/xmlbeans-5.0.3.jar differ diff --git a/com.sq.customization/src/com/sq/customization/dialogs/AutoTransToolDialog.java b/com.sq.customization/src/com/sq/customization/dialogs/AutoTransToolDialog.java index 7d1905e..7bbe02b 100644 --- a/com.sq.customization/src/com/sq/customization/dialogs/AutoTransToolDialog.java +++ b/com.sq.customization/src/com/sq/customization/dialogs/AutoTransToolDialog.java @@ -33,8 +33,13 @@ 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.xssf.streaming.SXSSFSheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; import com.sq.customization.bean.CustRuleBean; import com.sq.customization.bean.DesignPartBean; import com.sq.customization.bean.SleeveBean; @@ -397,11 +402,11 @@ public class AutoTransToolDialog extends AbstractAIFDialog { headerRow.createCell(3).setCellValue("FCC"); headerRow.createCell(4).setCellValue("动态扭矩"); headerRow.createCell(5).setCellValue("KCDS"); - headerRow.createCell(6).setCellValue("头部形式"); - headerRow.createCell(7).setCellValue("头部尺寸"); - headerRow.createCell(8).setCellValue("工具组合"); - headerRow.createCell(9).setCellValue("工具"); - headerRow.createCell(10).setCellValue("工具套筒"); +// headerRow.createCell(6).setCellValue("头部形式"); + headerRow.createCell(6).setCellValue("头部尺寸"); + headerRow.createCell(7).setCellValue("工具组合"); + headerRow.createCell(8).setCellValue("工具"); + headerRow.createCell(9).setCellValue("工具套筒"); //读取自定义的表 readRuleExcel(excelfile); readElectricfile(electricfile,1); @@ -1309,7 +1314,7 @@ public class AutoTransToolDialog extends AbstractAIFDialog { if(workline != null) { Map clause = new HashMap<>(); clause.put("item_id", queryid); - + System.out.println("获取到的工位id:"+queryid); List result2 = TCUtil.queryBOMStructObject(session,workline,"queryItem",clause); if(result2 != null) { for(TCComponent stationline:result2) { @@ -1335,23 +1340,28 @@ public class AutoTransToolDialog extends AbstractAIFDialog { if(pipei_resource == null || pipei_resource.length() == 0) continue; //先判断工位下是否有此名称的资源,有的直接拿来用 + System.out.println("先判断工位下是否有此名称的资源,有的直接拿来用"); Map clause3 = new HashMap<>(); clause3.put("object_name", pipei_resource); List Resourceresult2 = TCUtil.queryBOMStructObject2(session,stationline,"queryItem",clause3); - if(Resourceresult2.size() > 0) { + if(Resourceresult2 != null && Resourceresult2.size() > 0) { + System.out.println("工位下直接拿来用"+pipei_resource); TCUtil.connectObject(session, Resourceresult2.get(0), (TCComponent)op, "MEResource"); } else { + System.out.println("工位下没有资源:"+pipei_resource); Map clause22 = new HashMap<>(); clause22.put("object_name", pipei_resource); + clause22.put("object_type", "Item"); TCComponent[] query_items = TCUtil.Generalquery2(session, "queryItem", clause22); if(query_items != null && query_items.length > 0) { - + System.out.println("工位下没有资源:"+pipei_resource); TCComponentBOMLine newResourceline = station.add((TCComponentItem) query_items[0],null, null, false); TCUtil.connectObject(session, newResourceline, (TCComponent)op, "MEResource"); } else { //创建新的item + System.out.println("工位下没有资源,创建新的资源:"+pipei_resource); TCComponentItem item = TCUtil.createItem(session, "", "A", "Item", pipei_resource); TCComponentBOMLine newResourceline = station.add(item,null, null, false); TCUtil.connectObject(session, newResourceline, (TCComponent)op, "MEResource"); @@ -1361,7 +1371,9 @@ public class AutoTransToolDialog extends AbstractAIFDialog { for(String split_value:split) { clause2.clear(); clause2.put("object_name", split_value); - TCComponent[] tool_resource = TCUtil.Generalquery2(session, "queryItem", clause); + clause2.put("object_type", "Design Part"); + System.out.println("资源添加工具或套筒:"+split_value); + TCComponent[] tool_resource = TCUtil.Generalquery2(session, "queryItem", clause2); if(tool_resource != null && tool_resource.length > 0) { newResourceline.add((TCComponentItem) tool_resource[0],null, null, false); } @@ -2224,23 +2236,28 @@ public class AutoTransToolDialog extends AbstractAIFDialog { if(pipei_resource == null || pipei_resource.length() == 0) continue; //先判断工位下是否有此名称的资源,有的直接拿来用 + System.out.println("先判断工位下是否有此名称的资源,有的直接拿来用"); Map clause3 = new HashMap<>(); clause3.put("object_name", pipei_resource); List Resourceresult2 = TCUtil.queryBOMStructObject2(session,stationline,"queryItem",clause3); - if(Resourceresult2.size() > 0) { + if(Resourceresult2 != null && Resourceresult2.size() > 0) { + System.out.println("工位下直接拿来用"+pipei_resource); TCUtil.connectObject(session, Resourceresult2.get(0), (TCComponent)oldop, "MEResource"); } else { + System.out.println("工位下没有资源:"+pipei_resource); Map clause22 = new HashMap<>(); clause22.put("object_name", pipei_resource); + clause22.put("object_type", "Item"); TCComponent[] query_items = TCUtil.Generalquery2(session, "queryItem", clause22); if(query_items != null && query_items.length > 0) { - + System.out.println("工位下没有资源:"+pipei_resource); TCComponentBOMLine newResourceline = station.add((TCComponentItem) query_items[0],null, null, false); TCUtil.connectObject(session, newResourceline, (TCComponent)oldop, "MEResource"); } else { //创建新的item + System.out.println("工位下没有资源,创建新的资源:"+pipei_resource); TCComponentItem item = TCUtil.createItem(session, "", "A", "Item", pipei_resource); TCComponentBOMLine newResourceline = station.add(item,null, null, false); TCUtil.connectObject(session, newResourceline, (TCComponent)oldop, "MEResource"); @@ -2250,7 +2267,9 @@ public class AutoTransToolDialog extends AbstractAIFDialog { for(String split_value:split) { clause2.clear(); clause2.put("object_name", split_value); - TCComponent[] tool_resource = TCUtil.Generalquery2(session, "queryItem", clause); + clause2.put("object_type", "Design Part"); + System.out.println("资源添加工具或套筒:"+split_value); + TCComponent[] tool_resource = TCUtil.Generalquery2(session, "queryItem", clause2); if(tool_resource != null && tool_resource.length > 0) { newResourceline.add((TCComponentItem) tool_resource[0],null, null, false); } @@ -2264,6 +2283,7 @@ public class AutoTransToolDialog extends AbstractAIFDialog { } } + } } @@ -2308,46 +2328,86 @@ public class AutoTransToolDialog extends AbstractAIFDialog { //读取BOM表 - private void readExcel(File excelfile,int start_rownum,int idcol2,int svppscol2,int fcccol2 +// private void readExcel(File excelfile,int start_rownum,int idcol2,int svppscol2,int fcccol2 +// ,int fccdescol2,int dongtaicol,int kcdscol2) { +// +// Workbook work = null; +// try(InputStream is = new FileInputStream(excelfile);){ +// if (excelfile.getAbsolutePath().endsWith(".xls")) +// work = new HSSFWorkbook(is); +// else { +// work = new XSSFWorkbook(is); +// } +// Sheet sheet = work.getSheetAt(0); +// int count = sheet.getPhysicalNumberOfRows(); +// for(int i = start_rownum; i < count ;i++) { +// Row row = sheet.getRow(i); +// String id = ExcelUtil.getStringCellValue(row.getCell(idcol2-1)); +// if(id == null || id.length() == 0) +// break; +// String svpps = ExcelUtil.getStringCellValue(row.getCell(svppscol2-1)); +// String fcc = ExcelUtil.getStringCellValue(row.getCell(fcccol2-1)); +// String fccdes = ExcelUtil.getStringCellValue(row.getCell(fccdescol2-1)); +// String dongtai = ExcelUtil.getStringCellValue(row.getCell(dongtaicol-1)); +// String kcds = ExcelUtil.getStringCellValue(row.getCell(kcdscol2-1)); +// System.out.println("id:"+id+" svpps:"+svpps+" fcc:"+fcc+" fccdes:"+fccdes+" dongtai:"+dongtai+" kcds:"+kcds); +// String key = id; +// if(!DesignPartBeanMap.containsKey(key)) { +// DesignPartBean bean = new DesignPartBean(); +// bean.setDongtai(dongtai); +// bean.setFCC(fcc); +// bean.setFCCDES(fccdes); +// bean.setKCDS(kcds); +// bean.setSVPPS(svpps); +// DesignPartBeanMap.put(key, bean); +// } +// } +// } catch (FileNotFoundException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } + private void readExcel(File excelfile,int start_rownum,int idcol2,int svppscol2,int fcccol2 ,int fccdescol2,int dongtaicol,int kcdscol2) { - Workbook work = null; - try(InputStream is = new FileInputStream(excelfile);){ - if (excelfile.getAbsolutePath().endsWith(".xls")) - work = new HSSFWorkbook(is); - else { - work = new XSSFWorkbook(is); - } - Sheet sheet = work.getSheetAt(0); - int count = sheet.getPhysicalNumberOfRows(); - for(int i = start_rownum; i < count ;i++) { - Row row = sheet.getRow(i); - String id = ExcelUtil.getStringCellValue(row.getCell(idcol2-1)); - if(id == null || id.length() == 0) - break; - String svpps = ExcelUtil.getStringCellValue(row.getCell(svppscol2-1)); - String fcc = ExcelUtil.getStringCellValue(row.getCell(fcccol2-1)); - String fccdes = ExcelUtil.getStringCellValue(row.getCell(fccdescol2-1)); - String dongtai = ExcelUtil.getStringCellValue(row.getCell(dongtaicol-1)); - String kcds = ExcelUtil.getStringCellValue(row.getCell(kcdscol2-1)); - System.out.println("id:"+id+" svpps:"+svpps+" fcc:"+fcc+" fccdes:"+fccdes+" dongtai:"+dongtai+" kcds:"+kcds); - String key = id; - if(!DesignPartBeanMap.containsKey(key)) { - DesignPartBean bean = new DesignPartBean(); - bean.setDongtai(dongtai); - bean.setFCC(fcc); - bean.setFCCDES(fccdes); - bean.setKCDS(kcds); - bean.setSVPPS(svpps); - DesignPartBeanMap.put(key, bean); - } - } - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + + // 自定义监听器 + AnalysisEventListener> listener = new AnalysisEventListener>() { + @Override + public void invoke(Map rowData, AnalysisContext context) { + String id = rowData.get(idcol2-1) != null ? rowData.get(idcol2-1) : ""; + String dongtai = rowData.get(dongtaicol-1) != null ? rowData.get(dongtaicol-1) : ""; + String fcc = rowData.get(fcccol2-1) != null ? rowData.get(fcccol2-1) : ""; + String fccdes = rowData.get(fccdescol2-1) != null ? rowData.get(fccdescol2-1) : ""; + String kcds = rowData.get(kcdscol2-1) != null ? rowData.get(kcdscol2-1) : ""; + String svpps = rowData.get(svppscol2-1) != null ? rowData.get(svppscol2-1) : ""; + + String key = id; + if (!DesignPartBeanMap.containsKey(key)) { + DesignPartBean bean = new DesignPartBean(); + // 设置值,确保不会为 null + bean.setDongtai(dongtai); + bean.setFCC(fcc); + bean.setFCCDES(fccdes); + bean.setKCDS(kcds); + bean.setSVPPS(svpps); + DesignPartBeanMap.put(key, bean); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + System.out.println("读取完成,共 " + DesignPartBeanMap.size() + " 行数据"); + } + }; + + // 执行读取 + EasyExcel.read(excelfile, listener) + .sheet(0) // 指定 sheet 索引 + .headRowNumber(start_rownum) // 从第0行开始读取(包含标题行则设为1) + .doRead(); } //读取规则表 private void readRuleExcel(File excelfile) { @@ -2719,47 +2779,72 @@ public class AutoTransToolDialog extends AbstractAIFDialog { } /** * 读取套筒表*/ - private void readsleevefile(File excelfile,int start_rownum) { - Workbook work = null; - try(InputStream is = new FileInputStream(excelfile);){ - if (excelfile.getAbsolutePath().endsWith(".xls")) - work = new HSSFWorkbook(is); - else { - work = new XSSFWorkbook(is); - } - Sheet sheet = work.getSheetAt(0); - int count = sheet.getPhysicalNumberOfRows(); - for(int i = start_rownum; i < count ;i++) { - Row row = sheet.getRow(i); - String resourceType = ExcelUtil.getStringCellValue(row.getCell(1)); - String inputSize = ExcelUtil.getStringCellValue(row.getCell(6)); - String outSize = ExcelUtil.getStringCellValue(row.getCell(7)); - String length = ExcelUtil.getStringCellValue(row.getCell(11)); - String headform = ExcelUtil.getStringCellValue(row.getCell(16)); - if(resourceType != null && resourceType.length() > 0) { - // 使用 computeIfAbsent 简化操作 - SleeveBean tool = new SleeveBean(); - tool.setResourceType(resourceType); - tool.setLength(length); - tool.setHeadform(headform); - tool.setInputSize(inputSize); - tool.setOutSize(outSize); - - SleeveMap.computeIfAbsent(outSize, key -> new ArrayList<>()).add(tool); - - } - else { - break; - } - - } - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + private void readsleevefile(File excelfile, int start_rownum) { + Workbook work = null; + try (InputStream is = new FileInputStream(excelfile)) { + if (excelfile.getAbsolutePath().endsWith(".xls")) { + work = new HSSFWorkbook(is); + } else { + work = new XSSFWorkbook(is); + } + Sheet sheet = work.getSheetAt(0); + int count = sheet.getPhysicalNumberOfRows(); + + // 临时存储所有 SleeveBean,按 outSize 分组 + Map> tempMap = new HashMap<>(); + + for (int i = start_rownum; i < count; i++) { + Row row = sheet.getRow(i); + String resourceType = ExcelUtil.getStringCellValue(row.getCell(1)); + String inputSize = ExcelUtil.getStringCellValue(row.getCell(6)); + String outSize = ExcelUtil.getStringCellValue(row.getCell(7)); + String length = ExcelUtil.getStringCellValue(row.getCell(11)); + String headform = ExcelUtil.getStringCellValue(row.getCell(16)); + + if (resourceType != null && resourceType.length() > 0) { + SleeveBean tool = new SleeveBean(); + tool.setResourceType(resourceType); + tool.setLength(length); + tool.setHeadform(headform); + tool.setInputSize(inputSize); + tool.setOutSize(outSize); + + // 添加到临时 Map 中 + tempMap.computeIfAbsent(outSize, key -> new ArrayList<>()).add(tool); + } else { + break; + } + } + + // 对每个 outSize 的 List 按 length 排序 + for (Map.Entry> entry : tempMap.entrySet()) { + List sleeveList = entry.getValue(); + sleeveList.sort((bean1, bean2) -> { + // 将 length 转换为数字进行比较 + double length1 = parseLength(bean1.getLength()); + double length2 = parseLength(bean2.getLength()); + return Double.compare(length1, length2); + }); + // 将排序后的列表放入 SleeveMap + SleeveMap.put(entry.getKey(), sleeveList); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private double parseLength(String length) { + if (length == null || length.trim().isEmpty()) { + return 0.0; // 默认值,可以根据需求调整 + } + try { + return Double.parseDouble(length.trim()); + } catch (NumberFormatException e) { + System.err.println("无法解析 length: " + length); + return 0.0; // 解析失败时返回默认值 + } } private List getStation(TCComponentBOMLine child){ List result = null; diff --git a/com.sq.customization/src/com/sq/customization/util/ExcelUtil.java b/com.sq.customization/src/com/sq/customization/util/ExcelUtil.java index c4f1ce4..da9cadc 100644 --- a/com.sq.customization/src/com/sq/customization/util/ExcelUtil.java +++ b/com.sq.customization/src/com/sq/customization/util/ExcelUtil.java @@ -1,43 +1,85 @@ package com.sq.customization.util; import java.text.DecimalFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; public class ExcelUtil { - public static String getStringCellValue(Cell cell) - { - String strCell = ""; - if(cell == null) - return ""; - switch(cell.getCellType()){ - case Cell.CELL_TYPE_STRING : - strCell = cell.getStringCellValue(); - break; - case Cell.CELL_TYPE_NUMERIC : - if(DateUtil.isCellDateFormatted(cell)) - strCell = cell.getDateCellValue().toString(); - else - // strCell = String.valueOf(cell.getNumericCellValue()); - strCell = new DecimalFormat("#").format(cell.getNumericCellValue()); - break; - case Cell.CELL_TYPE_BOOLEAN : - strCell = String.valueOf(cell.getBooleanCellValue()); - break; - case Cell.CELL_TYPE_FORMULA : - strCell = cell.getCellFormula(); - break; - case Cell.CELL_TYPE_BLANK : - strCell = ""; - break; - default: - strCell = ""; - break; - } - if(strCell == null || strCell.equals("")) - return ""; - - return strCell; - } + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#"); + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + +// public static String getStringCellValue(Cell cell) +// { +// String strCell = ""; +// if(cell == null) +// return ""; +// switch(cell.getCellType()){ +// case Cell.CELL_TYPE_STRING : +// strCell = cell.getStringCellValue(); +// break; +// case Cell.CELL_TYPE_NUMERIC : +// if(DateUtil.isCellDateFormatted(cell)) +// strCell = cell.getDateCellValue().toString(); +// else +// // strCell = String.valueOf(cell.getNumericCellValue()); +// strCell = new DecimalFormat("#").format(cell.getNumericCellValue()); +// break; +// case Cell.CELL_TYPE_BOOLEAN : +// strCell = String.valueOf(cell.getBooleanCellValue()); +// break; +// case Cell.CELL_TYPE_FORMULA : +// strCell = cell.getCellFormula(); +// break; +// case Cell.CELL_TYPE_BLANK : +// strCell = ""; +// break; +// default: +// strCell = ""; +// break; +// } +// if(strCell == null || strCell.equals("")) +// return ""; +// +// return strCell; +// } + public static String getStringCellValue(Cell cell) { + if (cell == null) { + return ""; + } + + String strCell; + switch (cell.getCellType()) { + case STRING: + strCell = cell.getStringCellValue(); + break; + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + LocalDateTime dateTime = DateUtil.getLocalDateTime(cell.getNumericCellValue(), true); + strCell = dateTime.format(DATE_FORMATTER); + } else { + strCell = DECIMAL_FORMAT.format(cell.getNumericCellValue()); + } + break; + case BOOLEAN: + strCell = String.valueOf(cell.getBooleanCellValue()); + break; + case FORMULA: + strCell = cell.getCellFormula(); + break; + case BLANK: + strCell = ""; + break; + case ERROR: + strCell = String.valueOf(cell.getErrorCellValue()); + break; + default: + strCell = ""; + break; + } + + return strCell == null || strCell.isEmpty() ? "" : strCell.trim(); + } } diff --git a/com.sq.customization/src/com/sq/customization/util/XSSFReaderExample.java b/com.sq.customization/src/com/sq/customization/util/XSSFReaderExample.java new file mode 100644 index 0000000..19398d8 --- /dev/null +++ b/com.sq.customization/src/com/sq/customization/util/XSSFReaderExample.java @@ -0,0 +1,134 @@ +package com.sq.customization.util; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.xml.sax.*; +import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.helpers.XMLReaderFactory; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +public class XSSFReaderExample { + +// // 定义 DesignPartBean 类(假设的 bean 类) +// public static class DesignPartBean { +// private String dongtai; +// private String fcc; +// private String fccdes; +// private String kcds; +// private String svpps; +// +// // Getters and Setters +// public String getDongtai() { return dongtai; } +// public void setDongtai(String dongtai) { this.dongtai = dongtai; } +// public String getFcc() { return fcc; } +// public void setFcc(String fcc) { this.fcc = fcc; } +// public String getFccdes() { return fccdes; } +// public void setFccdes(String fccdes) { this.fccdes = fccdes; } +// public String getKcds() { return kcds; } +// public void setKcds(String kcds) { this.kcds = kcds; } +// public String getSvpps() { return svpps; } +// public void setSvpps(String svpps) { this.svpps = svpps; } +// } +// +// // 读取指定 sheet 的指定列数据并存入 Map +// public static Map readExcel(String filePath, int sheetIndex, int[] targetColumns) throws Exception { +// Map designPartBeanMap = new HashMap<>(); +// +// try (OPCPackage pkg = OPCPackage.open(filePath)) { +// XSSFReader xssfReader = new XSSFReader(pkg); +// SharedStringsTable sst = xssfReader.getSharedStringsTable(); // 获取共享字符串表 +// +// XMLReader parser = XMLReaderFactory.createXMLReader(); +// parser.setContentHandler(new DefaultHandler() { +// private Map rowData = new HashMap<>(); +// private int currentRow = -1; // 当前处理的行 +// private int currentCell = -1; // 当前处理的单元格 +// +// // 开始解析每个元素 +// public void startElement(String uri, String localName, String qName, Attributes attributes) { +// if ("row".equals(qName)) { +// currentRow++; +// } +// if ("v".equals(qName)) { // 获取单元格的值 +// currentCell++; +// } +// } +// +// // 获取单元格的值 +// public void characters(char[] ch, int start, int length) { +// String cellValue = new String(ch, start, length).trim(); +// // 只处理指定的列 +// for (int targetColumn : targetColumns) { +// if (currentCell == targetColumn) { +// rowData.put(currentCell, cellValue); +// } +// } +// } +// +// // 处理行结束时的逻辑 +// public void endElement(String uri, String localName, String qName) { +// if ("row".equals(qName)) { +// if (!rowData.isEmpty()) { +// DesignPartBean bean = new DesignPartBean(); +// // 假设列顺序依次为 dongtai, fcc, fccdes, kcds, svpps +// // 请根据实际列索引调整映射关系 +// bean.setDongtai(rowData.get(targetColumns[0])); // 第一列 +// if (targetColumns.length > 1) bean.setFcc(rowData.get(targetColumns[1])); // 第二列 +// if (targetColumns.length > 2) bean.setFccdes(rowData.get(targetColumns[2])); // 第三列 +// if (targetColumns.length > 3) bean.setKcds(rowData.get(targetColumns[3])); // 第四列 +// if (targetColumns.length > 4) bean.setSvpps(rowData.get(targetColumns[4])); // 第五列 +// +// // 使用某列作为 key(例如第一列 dongtai) +// String key = rowData.get(targetColumns[0]); // 可以根据需求调整 key +// if (key != null && !key.isEmpty()) { +// designPartBeanMap.put(key, bean); +// } +// } +// rowData.clear(); +// currentCell = -1; // 重置单元格计数器 +// } +// } +// }); +// +// // 获取指定 sheet 的数据流 +// Iterator sheetsData = xssfReader.getSheetsData(); +// InputStream targetSheet = null; +// for (int i = 0; i <= sheetIndex; i++) { +// if (!sheetsData.hasNext()) { +// throw new IllegalArgumentException("指定的 sheet 索引超出范围"); +// } +// targetSheet = sheetsData.next(); +// } +// if (targetSheet != null) { +// parser.parse(new InputSource(targetSheet)); // 解析指定 sheet 的流式数据 +// targetSheet.close(); // 关闭流 +// } +// } +// return designPartBeanMap; +// } +// +// public static void main(String[] args) throws Exception { +// // 指定需要读取的列(例如第0、1、2、3、4列) +// int[] targetColumns = {0, 1, 2, 3, 4}; // 根据实际需求调整列索引 +// String filePath = "C:\\Users\\Administrator\\Desktop\\RAP4E零件俱乐部2024-12-04.xlsx"; +// int sheetIndex = 0; // 读取第一个 sheet,索引从 0 开始 +// +// Map result = readExcel(filePath, sheetIndex, targetColumns); +// +// // 打印结果验证 +// for (Map.Entry entry : result.entrySet()) { +// DesignPartBean bean = entry.getValue(); +// System.out.println("Key: " + entry.getKey() + +// ", Dongtai: " + bean.getDongtai() + +// ", FCC: " + bean.getFcc() + +// ", FCCDES: " + bean.getFccdes() + +// ", KCDS: " + bean.getKcds() + +// ", SVPPS: " + bean.getSvpps()); +// } +// } +}