ASF Bugzilla – Attachment 30753 Details for
Bug 53130
[PATCH] SXSSF Shared Strings option support, to make generated xlsx files compatible with Google Docs or iPad
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch to facilitate optional use of SharedStringsTable in SXSSFWorkbook with correction of minor mistake
53150_r1.patch (text/plain), 10.83 KB, created by
Rahul mondal
on 2013-08-23 06:27:41 UTC
(
hide
)
Description:
Patch to facilitate optional use of SharedStringsTable in SXSSFWorkbook with correction of minor mistake
Filename:
MIME Type:
Creator:
Rahul mondal
Created:
2013-08-23 06:27:41 UTC
Size:
10.83 KB
patch
obsolete
>Index: src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java (revision 1516092) >+++ src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java (working copy) >@@ -22,14 +22,15 @@ > import java.io.*; > import java.util.zip.GZIPInputStream; > import java.util.zip.GZIPOutputStream; >+import org.apache.poi.xssf.model.SharedStringsTable; > > /** > * Sheet writer that supports gzip compression of the temp files. > */ > public class GZIPSheetDataWriter extends SheetDataWriter { > >- public GZIPSheetDataWriter() throws IOException { >- super(); >+ public GZIPSheetDataWriter(SharedStringsTable sharedStringsTable) throws IOException { >+ super(sharedStringsTable); > } > > /** >Index: src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (revision 1516092) >+++ src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (working copy) >@@ -23,12 +23,11 @@ > import org.apache.poi.ss.usermodel.CellStyle; > import org.apache.poi.ss.usermodel.FormulaError; > import org.apache.poi.ss.util.CellReference; >-import org.apache.xmlbeans.XmlCursor; >-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring; >- >-import javax.xml.namespace.QName; > import java.io.*; > import java.util.Iterator; >+import org.apache.poi.xssf.model.SharedStringsTable; >+import org.apache.poi.xssf.usermodel.XSSFRichTextString; >+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; > > /** > * Initially copied from BigGridDemo "SpreadsheetWriter". >@@ -46,11 +45,21 @@ > int _numberOfCellsOfLastFlushedRow; // meaningful only of _numberOfFlushedRows>0 > int _numberLastFlushedRow = -1; // meaningful only of _numberOfFlushedRows>0 > >+ /** >+ * Table of strings shared across this workbook. >+ * If two cells contain the same string, then the cell value is the same index into SharedStringsTable >+ */ >+ private SharedStringsTable _sharedStringSource; >+ > public SheetDataWriter() throws IOException { > _fd = createTempFile(); > _out = createWriter(_fd); > } > >+ public SheetDataWriter(SharedStringsTable sharedStringsTable) throws IOException{ >+ this(); >+ this._sharedStringSource = sharedStringsTable; >+ } > /** > * Create a temp file to write sheet data. > * By default, temp files are created in the default temporary-file directory >@@ -188,14 +197,24 @@ > break; > } > case Cell.CELL_TYPE_STRING: { >- _out.write(" t=\"inlineStr\">"); >- _out.write("<is><t"); >- if(hasLeadingTrailingSpaces(cell.getStringCellValue())) { >- _out.write(" xml:space=\"preserve\""); >+ if (_sharedStringSource != null) { >+ XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue()); >+ int sRef = _sharedStringSource.addEntry(rt.getCTRst()); >+ >+ _out.write(" t=\"" + STCellType.S.toString() + "\">"); >+ _out.write("<v>"); >+ _out.write(String.valueOf(sRef)); >+ _out.write("</v>"); >+ } else { >+ _out.write(" t=\"inlineStr\">"); >+ _out.write("<is><t"); >+ if (hasLeadingTrailingSpaces(cell.getStringCellValue())) { >+ _out.write(" xml:space=\"preserve\""); >+ } >+ _out.write(">"); >+ outputQuotedString(cell.getStringCellValue()); >+ _out.write("</t></is>"); > } >- _out.write(">"); >- outputQuotedString(cell.getStringCellValue()); >- _out.write("</t></is>"); > break; > } > case Cell.CELL_TYPE_NUMERIC: { >@@ -237,7 +256,7 @@ > } > > //Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java >- protected void outputQuotedString(String s) throws IOException { >+ protected void outputQuotedString(String s) throws IOException { > if (s == null || s.length() == 0) { > return; > } >Index: src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (revision 1516092) >+++ src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (working copy) >@@ -43,6 +43,7 @@ > import org.apache.poi.ss.formula.udf.UDFFinder; > import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; > import org.apache.poi.ss.util.CellRangeAddress; >+import org.apache.poi.xssf.model.SharedStringsTable; > > /** > * Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. >@@ -73,6 +74,11 @@ > private boolean _compressTmpFiles = false; > > /** >+ * shared string table - a cache of strings in this workbook >+ */ >+ private SharedStringsTable _sharedStringSource = null; >+ >+ /** > * Construct a new workbook > */ > public SXSSFWorkbook(){ >@@ -80,6 +86,17 @@ > } > > /** >+ * Construct a new workbook >+ * <p></p> >+ * Whether to use shared string table or not >+ * </p> >+ * @param useSharedStringsTable >+ */ >+ public SXSSFWorkbook(boolean useSharedStringsTable){ >+ this(null /*workbook*/, useSharedStringsTable); >+ } >+ >+ /** > * Construct a workbook from a template. > * <p> > * There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) : >@@ -115,11 +132,53 @@ > * @param workbook the template workbook > */ > public SXSSFWorkbook(XSSFWorkbook workbook){ >- this(workbook, DEFAULT_WINDOW_SIZE); >+ this(workbook, DEFAULT_WINDOW_SIZE, false); > } >- > > /** >+ * Construct a workbook from a template. >+ * <p> >+ * There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) : >+ * <ol> >+ * <li> >+ * Append new sheets to existing workbooks. You can open existing >+ * workbook from a file or create on the fly with XSSF. >+ * </li> >+ * <li> >+ * Append rows to existing sheets. The row number MUST be greater >+ * than max(rownum) in the template sheet. >+ * </li> >+ * <li> >+ * Use existing workbook as a template and re-use global objects such >+ * as cell styles, formats, images, etc. >+ * </li> >+ * </ol> >+ * All three use cases can work in a combination. >+ * </p> >+ * What is not supported: >+ * <ul> >+ * <li> >+ * Access initial cells and rows in the template. After constructing >+ * SXSSFWorkbook(XSSFWorkbook) all internal windows are empty and >+ * SXSSFSheet@getRow and SXSSFRow#getCell return null. >+ * </li> >+ * <li> >+ * Override existing cells and rows. The API silently allows that but >+ * the output file is invalid and Excel cannot read it. >+ * </li> >+ * </ul> >+ * >+ * @param workbook the template workbook >+ * <p></p> >+ * Whether to use shared string table or not >+ * </p> >+ * @param useSharedStringsTable >+ */ >+ public SXSSFWorkbook(XSSFWorkbook workbook, boolean useSharedStringsTable){ >+ this(workbook, DEFAULT_WINDOW_SIZE, useSharedStringsTable); >+ } >+ >+ /** > * Constructs an workbook from an existing workbook. > * <p> > * When a new node is created via createRow() and the total number >@@ -138,9 +197,13 @@ > * </p> > * > * @param rowAccessWindowSize >+ * <p></p> >+ * Whether to use shared string table or not >+ * </p> >+ * @param useSharedStringsTable > */ >- public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize){ >- this(workbook,rowAccessWindowSize, false); >+ public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean useSharedStringsTable){ >+ this(workbook,rowAccessWindowSize, false, useSharedStringsTable); > } > > /** >@@ -164,16 +227,22 @@ > * @param rowAccessWindowSize > * @param compressTmpFiles whether to use gzip compression for temporary files > */ >- public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean compressTmpFiles){ >+ public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean compressTmpFiles, boolean useSharedStringsTable){ > setRandomAccessWindowSize(rowAccessWindowSize); > setCompressTempFiles(compressTmpFiles); > if (workbook == null) > { > _wb=new XSSFWorkbook(); >+ if(useSharedStringsTable){ >+ _sharedStringSource = _wb.getSharedStringSource(); >+ } > } > else > { > _wb=workbook; >+ if(useSharedStringsTable){ >+ _sharedStringSource = _wb.getSharedStringSource(); >+ } > for ( int i = 0; i < _wb.getNumberOfSheets(); i++ ) > { > XSSFSheet sheet = _wb.getSheetAt( i ); >@@ -202,8 +271,38 @@ > * @param rowAccessWindowSize > */ > public SXSSFWorkbook(int rowAccessWindowSize){ >- this(null /*workbook*/, rowAccessWindowSize); >+ this(null /*workbook*/, rowAccessWindowSize, false); > } >+ >+ /** >+ * Construct an empty workbook and specify the window for row access. >+ * <p> >+ * When a new node is created via createRow() and the total number >+ * of unflushed records would exceed the specified value, then the >+ * row with the lowest index value is flushed and cannot be accessed >+ * via getRow() anymore. >+ * </p> >+ * <p> >+ * A value of -1 indicates unlimited access. In this case all >+ * records that have not been flushed by a call to flush() are available >+ * for random access. >+ * <p> >+ * <p></p> >+ * A value of 0 is not allowed because it would flush any newly created row >+ * without having a chance to specify any cells. >+ * </p> >+ * >+ * @param rowAccessWindowSize >+ * <p></p> >+ * Whether to use shared string table or not >+ * </p> >+ * @param useSharedStringsTable >+ * >+ * >+ */ >+ public SXSSFWorkbook(int rowAccessWindowSize, boolean useSharedStringsTable){ >+ this(null /*workbook*/, rowAccessWindowSize, useSharedStringsTable); >+ } > > public int getRandomAccessWindowSize() > { >@@ -236,9 +335,9 @@ > > SheetDataWriter createSheetDataWriter() throws IOException { > if(_compressTmpFiles) { >- return new GZIPSheetDataWriter(); >+ return new GZIPSheetDataWriter(_sharedStringSource); > } else { >- return new SheetDataWriter(); >+ return new SheetDataWriter(_sharedStringSource); > } > } >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 53130
:
28655
|
28656
|
30750
| 30753 |
31031
|
31032