1.采用easyexcel读取BOM的excel,客户文件有问题

2.调整资源放置
main
xuezhou 2 weeks ago
parent d7a1b14b5e
commit 32a871c418

@ -3,5 +3,23 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/cglib-3.3.0.jar"/>
<classpathentry kind="lib" path="lib/commons-collections4-4.4.jar"/>
<classpathentry kind="lib" path="lib/commons-compress-1.21.jar"/>
<classpathentry kind="lib" path="lib/commons-io-2.11.0.jar"/>
<classpathentry kind="lib" path="lib/easyexcel-3.2.0.jar"/>
<classpathentry kind="lib" path="lib/easyexcel-core-3.2.0.jar"/>
<classpathentry kind="lib" path="lib/easyexcel-support-3.2.0.jar"/>
<classpathentry kind="lib" path="lib/log4j-api-2.17.1.jar"/>
<classpathentry kind="lib" path="lib/poi-5.2.1.jar"/>
<classpathentry kind="lib" path="lib/poi-examples-5.2.1.jar"/>
<classpathentry kind="lib" path="lib/poi-excelant-5.2.1.jar"/>
<classpathentry kind="lib" path="lib/poi-ooxml-5.2.1.jar"/>
<classpathentry kind="lib" path="lib/poi-ooxml-full-5.2.1.jar"/>
<classpathentry kind="lib" path="lib/serializer-2.7.2.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.7.36.jar"/>
<classpathentry kind="lib" path="lib/xalan-2.7.2.jar"/>
<classpathentry kind="lib" path="lib/xml-apis-1.4.01.jar"/>
<classpathentry kind="lib" path="lib/xmlbeans-5.0.3.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -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,
.

@ -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

@ -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<String,String> clause = new HashMap<>();
clause.put("item_id", queryid);
System.out.println("获取到的工位id:"+queryid);
List<TCComponent> 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<String,String> clause3 = new HashMap<>();
clause3.put("object_name", pipei_resource);
List<TCComponent> 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<String,String> 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<String,String> clause3 = new HashMap<>();
clause3.put("object_name", pipei_resource);
List<TCComponent> 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<String,String> 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,31 +2328,66 @@ public class AutoTransToolDialog extends AbstractAIFDialog {
//读取BOM表
// 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);
// 自定义监听器
AnalysisEventListener<Map<Integer, String>> listener = new AnalysisEventListener<Map<Integer, String>>() {
@Override
public void invoke(Map<Integer, String> 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);
@ -2341,13 +2396,18 @@ public class AutoTransToolDialog extends AbstractAIFDialog {
DesignPartBeanMap.put(key, bean);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@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) {
@ -2721,14 +2781,18 @@ 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"))
try (InputStream is = new FileInputStream(excelfile)) {
if (excelfile.getAbsolutePath().endsWith(".xls")) {
work = new HSSFWorkbook(is);
else {
} else {
work = new XSSFWorkbook(is);
}
Sheet sheet = work.getSheetAt(0);
int count = sheet.getPhysicalNumberOfRows();
// 临时存储所有 SleeveBean按 outSize 分组
Map<String, List<SleeveBean>> tempMap = new HashMap<>();
for (int i = start_rownum; i < count; i++) {
Row row = sheet.getRow(i);
String resourceType = ExcelUtil.getStringCellValue(row.getCell(1));
@ -2736,8 +2800,8 @@ public class AutoTransToolDialog extends AbstractAIFDialog {
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);
@ -2745,22 +2809,43 @@ public class AutoTransToolDialog extends AbstractAIFDialog {
tool.setInputSize(inputSize);
tool.setOutSize(outSize);
SleeveMap.computeIfAbsent(outSize, key -> new ArrayList<>()).add(tool);
}
else {
// 添加到临时 Map 中
tempMap.computeIfAbsent(outSize, key -> new ArrayList<>()).add(tool);
} else {
break;
}
}
// 对每个 outSize 的 List 按 length 排序
for (Map.Entry<String, List<SleeveBean>> entry : tempMap.entrySet()) {
List<SleeveBean> 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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
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<TCComponent> getStation(TCComponentBOMLine child){
List<TCComponent> result = null;
Map<String,String> clause = new HashMap<>();

@ -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)
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 Cell.CELL_TYPE_STRING :
case 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());
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 Cell.CELL_TYPE_BOOLEAN :
case BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA :
case FORMULA:
strCell = cell.getCellFormula();
break;
case Cell.CELL_TYPE_BLANK :
case BLANK:
strCell = "";
break;
case ERROR:
strCell = String.valueOf(cell.getErrorCellValue());
break;
default:
strCell = "";
break;
}
if(strCell == null || strCell.equals(""))
return "";
return strCell;
return strCell == null || strCell.isEmpty() ? "" : strCell.trim();
}
}

@ -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<String, DesignPartBean> readExcel(String filePath, int sheetIndex, int[] targetColumns) throws Exception {
// Map<String, DesignPartBean> 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<Integer, String> 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<InputStream> 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<String, DesignPartBean> result = readExcel(filePath, sheetIndex, targetColumns);
//
// // 打印结果验证
// for (Map.Entry<String, DesignPartBean> 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());
// }
// }
}
Loading…
Cancel
Save