2016-10-27 35 views
1

非常に大きな(150Mb)xlsxファイルを読んでいます。私は興味のあるデータを抽出するために私自身のXSSFSheetXMLHandler.SheetContentsHandlerを持っています。 (それは喜んですばやく実行されます)。ただし、各セルについては、オーバーライドされたメソッドXSSFSheetXMLHandlerを使用してスタイル情報を取得する

cell(String cellReference, String formattedValue, XSSFComment comment) 

のみが私にセル参照と値を取得します。

このセルに適用されるスタイル(したがって前景の塗りつぶし色)を取得するにはどうすればよいですか? XSSFSheetXMLHandlerは私にStylesTableデータを与えます。スタイルの存在を知っています。私は持っていないものは、各セルからStylesTableへのポインタです。唯一の解決策は、XSSFSheetXMLHandlerを拡張し、SAX解析のリバースエンジニアリングを開始することです。

これは唯一のアプローチですか?

答えて

1

Apache POIソースからXSSFSheetXMLHandlerのソースコードを取得します。それはかなり簡単です。

このクラスを複製して、SheetHandlerを渡すよりも必要なものをすべて行うことが理にかなっています。私は大規模なXLSXファイルを処理するメモリの問題に遭遇する前に、ユーザーAPIで行ったことのすべて(スタイル、色、枠線、結合されたセルなど)を達成することができました。

たとえば、startElementの下の "c"ハンドラを少し変更しました。 XSSFCellStyleをクラスのプロパティとして保存してから、cellHandlerで使用できます。

// c => cell 
     else if ("c".equals(localName)) { 
      // Set up defaults. 
      this.nextDataType = xssfDataType.NUMBER; 
      this.formatIndex = -1; 
      this.formatString = null; 
      cellRef = attributes.getValue("r"); 
      String cellType = attributes.getValue("t"); 
      String cellStyleStr = attributes.getValue("s"); 
      if (stylesTable != null) { 
       if (cellStyleStr != null) { 
        int styleIndex = Integer.parseInt(cellStyleStr); 
        this.cellStyle = stylesTable.getStyleAt(styleIndex); 
       } else if (stylesTable.getNumCellStyles() > 0) { 
        this.cellStyle = stylesTable.getStyleAt(0); 
       } 
      } 

と、(例えばcellHandlerで)後で使用:

XSSFFont cellFont = cellStyle.getFont(); 
     if(cellFont.getXSSFColor() != null) { 
      // set hex colour in style. drop first 2 hex characters since they represent alpha 
      styles.put(CSS_COLOR, "#"+cellFont.getXSSFColor().getARGBHex().substring(2)); 
     } 

結合されたセル:

else if ("mergeCell".equals(localName)) { 
      if (attributes.getValue("ref") != null) { 
       CellRangeAddress cra = CellRangeAddress.valueOf(attributes.getValue("ref")); 
       // store merged cell ranges in hashmap?  
      } 
     } 
関連する問題