Java 使用ApachePOI读取Excel、XML映射元素名称
我有一个业务需求,其中提供了一个excel文档,该文档具有xml映射(基本上使用excel菜单选项Developer->Source,然后选择一个xml文件并将xml元素映射到excel单元格)。例如:单元格A2中的值映射到xml元素“文档标题”,B2映射到“文档编号” 要求以编程方式读取excel文档,搜索XML元素列表,并查找映射的单元格和单元格内容。例如:搜索xml元素“document_title”,找到该元素映射到的单元格(在上面提到的示例中,这是A2),并读取单元格的内容 我曾尝试使用ApachePOI的OPCP包和XSSFReader类,并尝试使用DOMParser对其进行解析,但未能实现这一点 下面是一个精简版本的源代码,有人可以帮助我在正确的方向Java 使用ApachePOI读取Excel、XML映射元素名称,java,xml,excel,apache-poi,Java,Xml,Excel,Apache Poi,我有一个业务需求,其中提供了一个excel文档,该文档具有xml映射(基本上使用excel菜单选项Developer->Source,然后选择一个xml文件并将xml元素映射到excel单元格)。例如:单元格A2中的值映射到xml元素“文档标题”,B2映射到“文档编号” 要求以编程方式读取excel文档,搜索XML元素列表,并查找映射的单元格和单元格内容。例如:搜索xml元素“document_title”,找到该元素映射到的单元格(在上面提到的示例中,这是A2),并读取单元格的内容 我曾尝试使
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类中的一个bug。我根据自己的需要调整了该示例中的代码,得到了所需的内容 总之,下面的代码读取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();
}
希望这对别人有帮助。请随时询问是否有任何疑问
谢谢,请包括导入内容。我不明白你从哪里弄来的东西!