使用Apache POI读取Excel,XML MAP元素名称

我有一个业务需求,我已经提供了与Excel映射到位的Excel文档(基本上使用Excel菜单选项开发人员 – >源,然后select一个XML文件和XML元素映射到Excel单元格)。 例如:单元格A2中的值被映射到xml元素“document_title”,B2被映射到“document_number”。

要求是以编程方式读取excel文档并searchXML元素的列表,并find映射的单元格和单元格的内容。 例如:searchxml元素“document_title”并查找该元素映射到的单元格(在上面提到的例子中,这是A2)并读取单元格的内容。

我已经尝试使用Apache POI的OPCP包和XSSFReader类,并尝试使用DOMParserparsing它,但无法实现这一点。

以下是源代码的修剪版本,有人可以帮助我在正确的方向。

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(); } 

如有任何疑问/build议,请告诉我。 任何帮助将非常感激

在通过互联网爬行之后,我find一个例子来解决一个POI类中的错误。 我已经调整了这个例子中的代码来满足我的需要,并得到了所需的东西。

因此,总之,下面的代码读取一个xlsx文件,检索任何关系(在这种情况下,我感兴趣的关系是tableSingleCells,因为这包含xml地图数据)。 代码然后parsing这个文档的所有映射的XML元素和相关联的单元格引用。

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

 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(); } 

希望这有助于某人。 随意询问是否有任何疑问。

谢谢,