我有一個業務需求,其中提供了具有xml映射的excel文檔(基本上使用excel菜單選項Developer-> Source,然後選擇一個xml文件並將XML元素映射到Excel單元格)。例如:單元格A2中的值映射到xml元素「document_title」,B2映射到「document_number」。使用Apache POI讀取Excel,XML MAP元素名稱
要求是以編程方式讀取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();
}
如果有任何疑問/建議,請讓我知道。任何幫助將非常感謝