gpt4 book ai didi

java - 使用 Apache POI 读取 Excel、XML MAP 元素名称

转载 作者:行者123 更新时间:2023-11-30 07:02:17 26 4
gpt4 key购买 nike

我有一个业务需求,其中向我提供了具有 xml 映射的 excel 文档(基本上使用 excel 菜单选项 Developer-> Source,然后选择 xml 文件并将 XML 元素映射到 excel 单元格)。例如:单元格 A2 中的值映射到 xml 元素“document_title”,B2 映射到“document_number”。

要求是以编程方式读取 Excel 文档并搜索 XML 元素列表并找到映射的单元格和单元格的内容。例如:搜索xml元素“document_title”并找到该元素映射到的单元格(在上面提到的示例中,这是A2)并读取该单元格的内容。

我尝试使用 OPCP 包和 apache POI 的 XSSFReader 类,并尝试使用 DOMParser 解析它,但无法实现这一点。

以下是源代码的精简版本,有人可以帮助我找到正确的方向吗?

public static void main( String[] args ) throws IOException
{
System.out.println( "reading excel" );

try {
OPCPackage pkg = OPCPackage.open("D:\\test.xlsx");
XSSFReader r = new XSSFReader( pkg );
SharedStringsTable sst = r.getSharedStringsTable();

InputStream inp = r.getSheet("rId1");

InputSource inpSource = new InputSource(inp);

DOMParser parser = new DOMParser();
parser.parse(inpSource);

Document doc = parser.getDocument();
inp.close(); // dont know yet, how to read each element, and hence trying to write this to a file

OutputStream writer = new FileOutputStream("D:\\outtrId11.xml");
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

//create string from xml tree

StreamResult result = new StreamResult(writer);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);

} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (OpenXML4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

如有任何疑问/建议,请告诉我。任何帮助将不胜感激

最佳答案

在互联网上进行了一些爬行之后,我发现了一个发布的示例,用于解决 POI 类之一中的错误。我已经根据我的需要调整了该示例中的代码,并得到了所需的内容。

总而言之,下面的代码读取 xlsx 文件,检索任何关系(在本例中,我感兴趣的关系是 tableSingleCells,因为它包含 xml 映射数据)。然后,代码解析此文档以获取所有映射的 XML 元素和关联的单元格引用。

最后,我显示 XML 元素、xpath 以及与这些 XML 元素关联的单元格的单元格值。

public static void main(String[] args) throws Exception {

System.out.println( "reading excel" );

File file = new File("D:\\test.xlsx");
// load an XLSX file with mapping informations

XSSFWorkbook wb;
wb = new XSSFWorkbook(file.getAbsolutePath());

for( XSSFSheet sheet : wb ) {

for( POIXMLDocumentPart doc : sheet.getRelations() ) {

final PackagePart part = doc.getPackagePart();
assert null!=part;

if( part==null ) {
System.out.println("part of relation is null. Will be ignored!");
continue;
}

//System.out.println(String.format("contentType [%s]", part.getContentType()));

if(part.getContentType().equalsIgnoreCase("application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml"))
{
System.out.println(String.format("contentType [%s]", part.getContentType()));

SingleXmlCellsDocument singleCellsXml = SingleXmlCellsDocument.Factory.parse( part.getInputStream() );
CTSingleXmlCells scs = singleCellsXml.getSingleXmlCells();

for( CTSingleXmlCell sc : scs.getSingleXmlCellArray() ) {

//get R reference
final String ref = sc.getR();

//get cell reference
final CellReference cellRef = new CellReference( ref );
final CTXmlCellPr cellPr = sc.getXmlCellPr();

//get xml element reference
final CTXmlPr pr = cellPr.getXmlPr();

//get xpath reference
final String xpath = pr.getXpath();

//navigate to the cell by setting row and column
final int rowNum = cellRef.getRow();
XSSFRow row = sheet.getRow(rowNum);

final int colNum = cellRef.getCol();
XSSFCell cell = row.getCell( colNum);


DataFormatter formatter = new DataFormatter();

String cellStrValue="";

cellStrValue=formatter.formatCellValue(cell);


//System.out.println(xpathQuery);
final String xpathQuery = String.format("[Cell Reference: " + ref + "] [Element Name: "+ cellPr.getUniqueName() + "] [Cell Value: " + cellStrValue + "] [Full xpath: " + xpath + "]" );
System.out.println(xpathQuery);


}

}

}

}

wb.close();

}

希望这对某人有帮助。如有任何疑问,请随时询问。

谢谢

关于java - 使用 Apache POI 读取 Excel、XML MAP 元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40718841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com