2015-05-20 37 views
0

我的代碼解析XLSX到XML是這樣的:如何追加SharedStringsTable同時使用DOM解析器和Apache POI

public class ReadXlsx { 
public static void processDoc(String path)throws Exception 
{ 
    OPCPackage pkg = OPCPackage.open(path); 
    XSSFReader r = new XSSFReader(pkg); 
    SharedStringsTable sst = r.getSharedStringsTable();  
    DOMParser parser = new DOMParser(); 
    InputStream inp = r.getSheet("rId1"); 
    InputSource inpSource = new InputSource(inp); 
    inpSource.setEncoding("UTF-8"); 
    parser.parse(inpSource); 


    Document doc = parser.getDocument(); 
    inp.close(); 

    OutputStream writer = new FileOutputStream(System.getProperty("user.home") + "//Desktop"+"//file.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); 
    XmlToJson.Convert(System.getProperty("user.home") + "\\Desktop"+"\\file.xml",System.getProperty("user.home") + "\\Desktop"+"\\Json4.json"); 
}} 

問題是,當我轉換.xlsx文件以.XML文件中每片將字符串被變換到這樣的事情

<c r="A1" s="43" t="s"><v>93</v></c> 

這意味着在A1字符串在sharedStrings.xml 第94串(T = SV = 93表示細胞是一個字符串數組值是93)

我可以得到SharedStringsTable,但我不知道如何將它包含了 XML文件中,以便它會顯示字符串,而不是T = S V = 93像

<c r="A1" s="43"><t>This is String.</t></c> 

感謝。

回答

0

您需要使用getEntryAt(INT)函數從SharedStringsTable對象如下:

SharedStringsTable sst = r1.getSharedStringsTable();//r1 being your XSSFReader 
CTRst st = sst.getEntryAt(k);//k is the index that you have in <v> tag 
st.getT();//getT() gets your value 

的SharedStringsTable在同級車保持爲一個ArrayList和一個HashMap,他們兩人是私有類成員。我寫的上面的代碼是從ArrayList成員訪問它。當您創建SharedStringsTable對象時,整個SharedStrings xml將分別作爲HashMap和ArrayList加載。因此,流式傳輸xlsx文件確實存在此限制。如果有解決方法,我想知道,因爲我們不能覆蓋SharedStringsTable類的私有成員(HashMap和ArrayList)。