View | Details | Raw Unified | Return to bug 53130
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java (-2 / +3 lines)
Lines 22-35 Link Here
22
import java.io.*;
22
import java.io.*;
23
import java.util.zip.GZIPInputStream;
23
import java.util.zip.GZIPInputStream;
24
import java.util.zip.GZIPOutputStream;
24
import java.util.zip.GZIPOutputStream;
25
import org.apache.poi.xssf.model.SharedStringsTable;
25
26
26
/**
27
/**
27
 * Sheet writer that supports gzip compression of the temp files.
28
 * Sheet writer that supports gzip compression of the temp files.
28
 */
29
 */
29
public class GZIPSheetDataWriter extends SheetDataWriter {
30
public class GZIPSheetDataWriter extends SheetDataWriter {
30
31
31
    public GZIPSheetDataWriter() throws IOException {
32
    public GZIPSheetDataWriter(SharedStringsTable sharedStringsTable) throws IOException {
32
        super();
33
        super(sharedStringsTable);
33
    }
34
    }
34
35
35
    /**
36
    /**
(-)src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (-12 / +31 lines)
Lines 23-34 Link Here
23
import org.apache.poi.ss.usermodel.CellStyle;
23
import org.apache.poi.ss.usermodel.CellStyle;
24
import org.apache.poi.ss.usermodel.FormulaError;
24
import org.apache.poi.ss.usermodel.FormulaError;
25
import org.apache.poi.ss.util.CellReference;
25
import org.apache.poi.ss.util.CellReference;
26
import org.apache.xmlbeans.XmlCursor;
27
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;
28
29
import javax.xml.namespace.QName;
30
import java.io.*;
26
import java.io.*;
31
import java.util.Iterator;
27
import java.util.Iterator;
28
import org.apache.poi.xssf.model.SharedStringsTable;
29
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
30
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
32
31
33
/**
32
/**
34
 * Initially copied from BigGridDemo "SpreadsheetWriter".
33
 * Initially copied from BigGridDemo "SpreadsheetWriter".
Lines 46-56 Link Here
46
    int _numberOfCellsOfLastFlushedRow; // meaningful only of _numberOfFlushedRows>0
45
    int _numberOfCellsOfLastFlushedRow; // meaningful only of _numberOfFlushedRows>0
47
    int _numberLastFlushedRow = -1; // meaningful only of _numberOfFlushedRows>0
46
    int _numberLastFlushedRow = -1; // meaningful only of _numberOfFlushedRows>0
48
47
48
    /**
49
     * Table of strings shared across this workbook.
50
     * If two cells contain the same string, then the cell value is the same index into SharedStringsTable
51
     */
52
    private SharedStringsTable _sharedStringSource;
53
49
    public SheetDataWriter() throws IOException {
54
    public SheetDataWriter() throws IOException {
50
        _fd = createTempFile();
55
        _fd = createTempFile();
51
        _out = createWriter(_fd);
56
        _out = createWriter(_fd);
52
    }
57
    }
53
58
59
    public SheetDataWriter(SharedStringsTable sharedStringsTable) throws IOException{
60
        this();
61
        this._sharedStringSource = sharedStringsTable;
62
    }
54
    /**
63
    /**
55
     * Create a temp file to write sheet data. 
64
     * Create a temp file to write sheet data. 
56
     * By default, temp files are created in the default temporary-file directory
65
     * By default, temp files are created in the default temporary-file directory
Lines 188-201 Link Here
188
                break;
197
                break;
189
            }
198
            }
190
            case Cell.CELL_TYPE_STRING: {
199
            case Cell.CELL_TYPE_STRING: {
191
                _out.write(" t=\"inlineStr\">");
200
                if (_sharedStringSource != null) {
192
                _out.write("<is><t");
201
                    XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
193
                if(hasLeadingTrailingSpaces(cell.getStringCellValue())) {
202
                    int sRef = _sharedStringSource.addEntry(rt.getCTRst());
194
                    _out.write(" xml:space=\"preserve\"");
203
204
                    _out.write(" t=\"" + STCellType.S.toString() + "\">");
205
                    _out.write("<v>");
206
                    _out.write(String.valueOf(sRef));
207
                    _out.write("</v>");
208
                } else {
209
                    _out.write(" t=\"inlineStr\">");
210
                    _out.write("<is><t");
211
                    if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
212
                        _out.write(" xml:space=\"preserve\"");
213
                    }
214
                    _out.write(">");
215
                    outputQuotedString(cell.getStringCellValue());
216
                    _out.write("</t></is>");
195
                }
217
                }
196
                _out.write(">");
197
                outputQuotedString(cell.getStringCellValue());
198
                _out.write("</t></is>");
199
                break;
218
                break;
200
            }
219
            }
201
            case Cell.CELL_TYPE_NUMERIC: {
220
            case Cell.CELL_TYPE_NUMERIC: {
Lines 237-243 Link Here
237
    }
256
    }
238
257
239
    //Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java
258
    //Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java
240
    protected void outputQuotedString(String s) throws IOException {
259
     protected void outputQuotedString(String s) throws IOException {
241
        if (s == null || s.length() == 0) {
260
        if (s == null || s.length() == 0) {
242
            return;
261
            return;
243
        }
262
        }
(-)src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (-8 / +107 lines)
Lines 43-48 Link Here
43
import org.apache.poi.ss.formula.udf.UDFFinder;
43
import org.apache.poi.ss.formula.udf.UDFFinder;
44
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
44
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
45
import org.apache.poi.ss.util.CellRangeAddress;
45
import org.apache.poi.ss.util.CellRangeAddress;
46
import org.apache.poi.xssf.model.SharedStringsTable;
46
47
47
/**
48
/**
48
 * Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy.
49
 * Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy.
Lines 73-78 Link Here
73
    private boolean _compressTmpFiles = false;
74
    private boolean _compressTmpFiles = false;
74
75
75
    /**
76
    /**
77
     * shared string table - a cache of strings in this workbook
78
     */
79
    private SharedStringsTable _sharedStringSource = null;
80
81
    /**
76
     * Construct a new workbook
82
     * Construct a new workbook
77
     */
83
     */
78
    public SXSSFWorkbook(){
84
    public SXSSFWorkbook(){
Lines 80-85 Link Here
80
    }
86
    }
81
87
82
    /**
88
    /**
89
     * Construct a new workbook
90
     * <p></p>
91
     * Whether to use shared string table or not
92
     * </p>
93
     * @param useSharedStringsTable
94
     */
95
    public SXSSFWorkbook(boolean useSharedStringsTable){
96
    	this(null /*workbook*/, useSharedStringsTable);
97
    }
98
99
    /**
83
     * Construct a workbook from a template.
100
     * Construct a workbook from a template.
84
     * <p>
101
     * <p>
85
     * There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) :
102
     * There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) :
Lines 115-125 Link Here
115
     * @param workbook  the template workbook
132
     * @param workbook  the template workbook
116
     */
133
     */
117
    public SXSSFWorkbook(XSSFWorkbook workbook){
134
    public SXSSFWorkbook(XSSFWorkbook workbook){
118
    	this(workbook, DEFAULT_WINDOW_SIZE);
135
    	this(workbook, DEFAULT_WINDOW_SIZE, false);
119
    }
136
    }
120
    
121
137
122
    /**
138
    /**
139
     * Construct a workbook from a template.
140
     * <p>
141
     * There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) :
142
     * <ol>
143
     *   <li>
144
     *       Append new sheets to existing workbooks. You can open existing
145
     *       workbook from a file or create on the fly with XSSF.
146
     *   </li>
147
     *   <li>
148
     *       Append rows to existing sheets. The row number MUST be greater
149
     *       than max(rownum) in the template sheet.
150
     *   </li>
151
     *   <li>
152
     *       Use existing workbook as a template and re-use global objects such
153
     *       as cell styles, formats, images, etc.
154
     *   </li>
155
     * </ol>
156
     * All three use cases can work in a combination.
157
     * </p>
158
     * What is not supported:
159
     * <ul>
160
     *   <li>
161
     *   Access initial cells and rows in the template. After constructing
162
     *   SXSSFWorkbook(XSSFWorkbook) all internal windows are empty and
163
     *   SXSSFSheet@getRow and SXSSFRow#getCell return null.
164
     *   </li>
165
     *   <li>
166
     *    Override existing cells and rows. The API silently allows that but
167
     *    the output file is invalid and Excel cannot read it.
168
     *   </li>
169
     * </ul>
170
     *
171
     * @param workbook  the template workbook
172
     * <p></p>
173
     * Whether to use shared string table or not
174
     * </p>
175
     * @param useSharedStringsTable
176
     */
177
    public SXSSFWorkbook(XSSFWorkbook workbook, boolean useSharedStringsTable){
178
    	this(workbook, DEFAULT_WINDOW_SIZE, useSharedStringsTable);
179
    }
180
181
    /**
123
     * Constructs an workbook from an existing workbook.
182
     * Constructs an workbook from an existing workbook.
124
     * <p>
183
     * <p>
125
     * When a new node is created via createRow() and the total number
184
     * When a new node is created via createRow() and the total number
Lines 138-146 Link Here
138
     * </p>
197
     * </p>
139
     *
198
     *
140
     * @param rowAccessWindowSize
199
     * @param rowAccessWindowSize
200
     * <p></p>
201
     * Whether to use shared string table or not
202
     * </p>
203
     * @param useSharedStringsTable
141
     */
204
     */
142
    public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize){
205
    public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean useSharedStringsTable){
143
    	this(workbook,rowAccessWindowSize, false);
206
    	this(workbook,rowAccessWindowSize, false, useSharedStringsTable);
144
    }
207
    }
145
208
146
    /**
209
    /**
Lines 164-179 Link Here
164
     * @param rowAccessWindowSize
227
     * @param rowAccessWindowSize
165
     * @param compressTmpFiles whether to use gzip compression for temporary files
228
     * @param compressTmpFiles whether to use gzip compression for temporary files
166
     */
229
     */
167
    public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean compressTmpFiles){
230
    public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean compressTmpFiles, boolean useSharedStringsTable){
168
    	setRandomAccessWindowSize(rowAccessWindowSize);
231
    	setRandomAccessWindowSize(rowAccessWindowSize);
169
    	setCompressTempFiles(compressTmpFiles);
232
    	setCompressTempFiles(compressTmpFiles);
170
    	if (workbook == null)
233
    	if (workbook == null)
171
    	{
234
    	{
172
    		_wb=new XSSFWorkbook();
235
    		_wb=new XSSFWorkbook();
236
                if(useSharedStringsTable){
237
                    _sharedStringSource = _wb.getSharedStringSource();
238
                }
173
    	}
239
    	}
174
    	else
240
    	else
175
    	{
241
    	{
176
    		_wb=workbook;
242
    		_wb=workbook;
243
                if(useSharedStringsTable){
244
                    _sharedStringSource = _wb.getSharedStringSource();
245
                }
177
            for ( int i = 0; i < _wb.getNumberOfSheets(); i++ )
246
            for ( int i = 0; i < _wb.getNumberOfSheets(); i++ )
178
            {
247
            {
179
                XSSFSheet sheet = _wb.getSheetAt( i );
248
                XSSFSheet sheet = _wb.getSheetAt( i );
Lines 202-209 Link Here
202
     * @param rowAccessWindowSize
271
     * @param rowAccessWindowSize
203
     */
272
     */
204
    public SXSSFWorkbook(int rowAccessWindowSize){
273
    public SXSSFWorkbook(int rowAccessWindowSize){
205
    	this(null /*workbook*/, rowAccessWindowSize);
274
    	this(null /*workbook*/, rowAccessWindowSize, false);
206
    }
275
    }
276
277
    /**
278
     * Construct an empty workbook and specify the window for row access.
279
     * <p>
280
     * When a new node is created via createRow() and the total number
281
     * of unflushed records would exceed the specified value, then the
282
     * row with the lowest index value is flushed and cannot be accessed
283
     * via getRow() anymore.
284
     * </p>
285
     * <p>
286
     * A value of -1 indicates unlimited access. In this case all
287
     * records that have not been flushed by a call to flush() are available
288
     * for random access.
289
     * <p>
290
     * <p></p>
291
     * A value of 0 is not allowed because it would flush any newly created row
292
     * without having a chance to specify any cells.
293
     * </p>
294
     *
295
     * @param rowAccessWindowSize
296
     * <p></p>
297
     * Whether to use shared string table or not
298
     * </p>
299
     * @param useSharedStringsTable
300
     *
301
     *
302
     */
303
    public SXSSFWorkbook(int rowAccessWindowSize, boolean useSharedStringsTable){
304
    	this(null /*workbook*/, rowAccessWindowSize, useSharedStringsTable);
305
    }
207
    
306
    
208
    public int getRandomAccessWindowSize()
307
    public int getRandomAccessWindowSize()
209
    {
308
    {
Lines 236-244 Link Here
236
335
237
    SheetDataWriter createSheetDataWriter() throws IOException {
336
    SheetDataWriter createSheetDataWriter() throws IOException {
238
        if(_compressTmpFiles) {
337
        if(_compressTmpFiles) {
239
            return new GZIPSheetDataWriter();
338
            return new GZIPSheetDataWriter(_sharedStringSource);
240
        } else {
339
        } else {
241
            return new SheetDataWriter();
340
            return new SheetDataWriter(_sharedStringSource);
242
        }
341
        }
243
    }
342
    }
244
343

Return to bug 53130