diff options
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java')
-rw-r--r-- | src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java | 270 |
1 files changed, 12 insertions, 258 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index a0f30b8294..c453203ac9 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -23,9 +23,9 @@ import java.util.TreeMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.SheetUtil; +import org.apache.poi.util.Internal; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.hssf.util.PaneInformation; @@ -48,14 +48,24 @@ public class SXSSFSheet implements Sheet, Cloneable { _workbook=workbook; _sh=xSheet; - _writer=new SheetDataWriter(); + _writer = workbook.createSheetDataWriter(); setRandomAccessWindowSize(_workbook.getRandomAccessWindowSize()); } + + /** + * for testing purposes only + */ + SheetDataWriter getSheetDataWriter(){ + return _writer; + } + /* Gets "<sheetData>" document fragment*/ public InputStream getWorksheetXMLInputStream() throws IOException { + // flush all remaining data and close the temp file writer flushRows(0); + _writer.close(); return _writer.getWorksheetXMLInputStream(); } @@ -1271,260 +1281,4 @@ public class SXSSFSheet implements Sheet, Cloneable assert false; return -1; } -/*Initially copied from BigGridDemo "SpreadsheetWriter". Unlike the original code which wrote the entire document, this class only writes the "sheetData" document fragment so that it was renamed to "SheetDataWriter"*/ - public class SheetDataWriter - { - private final File _fd; - private final Writer _out; - private int _rownum; - private boolean _rowContainedNullCells=false; - int _numberOfFlushedRows; - int _lowestIndexOfFlushedRows; // meaningful only of _numberOfFlushedRows>0 - int _numberOfCellsOfLastFlushedRow; // meaningful only of _numberOfFlushedRows>0 - - public SheetDataWriter() throws IOException - { - _fd = File.createTempFile("poi-sxssf-sheet", ".xml"); - _fd.deleteOnExit(); - _out = new BufferedWriter(new FileWriter(_fd)); - } - public int getNumberOfFlushedRows() - { - return _numberOfFlushedRows; - } - public int getNumberOfCellsOfLastFlushedRow() - { - return _numberOfCellsOfLastFlushedRow; - } - public int getLowestIndexOfFlushedRows() - { - return _lowestIndexOfFlushedRows; - } - protected void finalize() throws Throwable - { - _fd.delete(); - } - public InputStream getWorksheetXMLInputStream() throws IOException - { - _out.flush(); - _out.close(); - return new FileInputStream(_fd); - } - - /** - * Write a row to the file - * - * @param rownum 0-based row number - * @param row a row - */ - public void writeRow(int rownum,SXSSFRow row) throws IOException - { - if(_numberOfFlushedRows==0) - _lowestIndexOfFlushedRows=rownum; - _numberOfCellsOfLastFlushedRow=row.getLastCellNum(); - _numberOfFlushedRows++; - beginRow(rownum,row); - Iterator<Cell> cells=row.allCellsIterator(); - int columnIndex=0; - while(cells.hasNext()) - { - writeCell(columnIndex++,cells.next()); - } - endRow(); - } - void beginRow(int rownum,SXSSFRow row) throws IOException - { - _out.write("<row r=\""+(rownum+1)+"\""); - if(row.hasCustomHeight()) - _out.write(" customHeight=\"true\" ht=\""+row.getHeightInPoints()+"\""); - if(row.getZeroHeight()) - _out.write(" hidden=\"true\""); - if(row.isFormatted()) { - _out.write(" s=\"" + row._style + "\""); - _out.write(" customFormat=\"1\""); - } - _out.write(">\n"); - this._rownum = rownum; - _rowContainedNullCells=false; - } - - void endRow() throws IOException - { - _out.write("</row>\n"); - } - - public void writeCell(int columnIndex,Cell cell) throws IOException - { - if(cell==null) - { - _rowContainedNullCells=true; - return; - } - String ref = new CellReference(_rownum, columnIndex).formatAsString(); - _out.write("<c r=\""+ref+"\""); - CellStyle cellStyle=cell.getCellStyle(); - if(cellStyle.getIndex() != 0) _out.write(" s=\""+cellStyle.getIndex()+"\""); - int cellType=cell.getCellType(); - switch(cellType) - { - case Cell.CELL_TYPE_BLANK: - { - _out.write(">"); - break; - } - case Cell.CELL_TYPE_FORMULA: - { - _out.write(">"); - _out.write("<f>"); - outputQuotedString(cell.getCellFormula()); - _out.write("</f>"); - switch (cell.getCachedFormulaResultType()){ - case Cell.CELL_TYPE_NUMERIC: - double nval = cell.getNumericCellValue(); - if(!Double.isNaN(nval)){ - _out.write("<v>"+nval+"</v>"); - } - break; - } - break; - } - case Cell.CELL_TYPE_STRING: - { - _out.write(" t=\"inlineStr\">"); - _out.write("<is><t>"); - outputQuotedString(cell.getStringCellValue()); - _out.write("</t></is>"); - break; - } - case Cell.CELL_TYPE_NUMERIC: - { - _out.write(" t=\"n\">"); - _out.write("<v>"+cell.getNumericCellValue()+"</v>"); - break; - } - case Cell.CELL_TYPE_BOOLEAN: - { - _out.write(" t=\"b\">"); - _out.write("<v>"+(cell.getBooleanCellValue()?"1":"0")+"</v>"); - break; - } - case Cell.CELL_TYPE_ERROR: - { - FormulaError error = FormulaError.forInt(cell.getErrorCellValue()); - - _out.write(" t=\"e\">"); - _out.write("<v>" + error.getString() +"</v>"); - break; - } - default: - { - assert false; - throw new RuntimeException("Huh?"); - } - } - _out.write("</c>"); - } -//Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java - protected void outputQuotedString(String s) throws IOException - { - if(s == null || s.length() == 0) { - return; - } - - char[] chars=s.toCharArray(); - int last = 0; - int length=s.length(); - for(int counter = 0; counter < length; counter++) - { - char c = chars[counter]; - switch(c) - { - case '<': - if(counter>last) - { - _out.write(chars,last,counter-last); - } - last=counter+1; - _out.write("<"); - break; - case '>': - if(counter > last) - { - _out.write(chars,last,counter-last); - } - last=counter+1; - _out.write(">"); - break; - case '&': - if(counter>last) - { - _out.write(chars,last,counter-last); - } - last=counter+1; - _out.write("&"); - break; - case '"': - if (counter>last) - { - _out.write(chars,last,counter-last); - } - last=counter+1; - _out.write("""); - break; - // Special characters - case '\n': - if(counter>last) - { - _out.write(chars,last,counter-last); - } - _out.write("
"); - last=counter+1; - break; - case '\t': - if(counter>last) - { - _out.write(chars,last,counter-last); - } - _out.write("	"); - last=counter+1; - break; - case '\r': - if(counter>last) - { - _out.write(chars,last,counter-last); - } - _out.write("
"); - last=counter+1; - break; - case 0xa0: - if(counter>last) - { - _out.write(chars,last,counter-last); - } - _out.write(" "); - last=counter+1; - break; - default: - if(c<' '||c>127) - { - if(counter>last) - { - _out.write(chars,last,counter-last); - } - last=counter+1; - // If the character is outside of ascii, write the - // numeric value. - _out.write("&#"); - _out.write(String.valueOf((int)c)); - _out.write(";"); - } - break; - } - } - if (last<length) - { - _out.write(chars,last,length-last); - } - } - } } |