1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.dev;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.Region;
/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a util for testing functionality.
* It does contain sample API usage that may be educational to regular API users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
*/
public class HSSF
{
private String _filename = null;
protected HSSFWorkbook _hssfworkbook = null;
/**
* Constructor HSSF - creates an HSSFStream from an InputStream. The HSSFStream
* reads in the records allowing modification.
*
*
* @param filename
*
* @exception IOException
*
*/
public HSSF(String filename) throws IOException {
_filename = filename;
_hssfworkbook = new HSSFWorkbook(new FileInputStream(filename));
}
/**
* Constructor HSSF - given a filename this outputs a sample sheet with just
* a set of rows/cells.
*
*
* @param filename
* @param write
*
* @exception IOException
*
*/
public HSSF(String filename, boolean write)
throws IOException
{
int rownum = 0;
FileOutputStream out = new FileOutputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
f.setFontHeightInPoints(( short ) 12);
f.setColor(( short ) 0xA);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
f2.setFontHeightInPoints(( short ) 10);
f2.setColor(( short ) 0xf);
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cs.setFont(f);
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cs2.setFillPattern(( short ) 1); // fill w fg
cs2.setFillForegroundColor(( short ) 0xA);
cs2.setFont(f2);
wb.setSheetName(0, "HSSF Test");
for (rownum = 0; rownum < 300; rownum++)
{
r = s.createRow(rownum);
if ((rownum % 2) == 0)
{
r.setHeight(( short ) 0x249);
}
// r.setRowNum(( short ) rownum);
for (int cellnum = 0; cellnum < 50; cellnum += 2)
{
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));
if ((rownum % 2) == 0)
{
c.setCellStyle(cs);
}
c = r.createCell(cellnum + 1,
HSSFCell.CELL_TYPE_STRING);
c.setCellValue(new HSSFRichTextString("TEST"));
s.setColumnWidth(cellnum + 1, (int)(50 * 8 / 0.05));
if ((rownum % 2) == 0)
{
c.setCellStyle(cs2);
}
} // 50 characters divided by 1/20th of a point
}
// draw a thick black border on the row at the bottom using BLANKS
rownum++;
rownum++;
r = s.createRow(rownum);
cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
for (int cellnum = 0; cellnum < 50; cellnum++)
{
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_BLANK);
// c.setCellValue(0);
c.setCellStyle(cs3);
}
s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
// end draw thick black border
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
// end deleted sheet
wb.write(out);
out.close();
}
/**
* Constructor HSSF - takes in file - attempts to read it then reconstruct it
*
*
* @param infile
* @param outfile
* @param write
*
* @exception IOException
*
*/
public HSSF(String infile, String outfile, boolean write)
throws IOException
{
_filename = infile;
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(_filename));
_hssfworkbook = new HSSFWorkbook(fs);
// HSSFWorkbook book = hssfstream.getWorkbook();
}
/**
* Method main
*
* Given 1 argument takes that as the filename, inputs it and dumps the
* cell values/types out to sys.out
*
* given 2 arguments where the second argument is the word "write" and the
* first is the filename - writes out a sample (test) spreadsheet (see
* public HSSF(String filename, boolean write)).
*
* given 2 arguments where the first is an input filename and the second
* an output filename (not write), attempts to fully read in the
* spreadsheet and fully write it out.
*
* given 3 arguments where the first is an input filename and the second an
* output filename (not write) and the third is "modify1", attempts to read in the
* spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to
* "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you
* take the output from the write test, you'll have a valid scenario.
*
* @param args
*
*/
public static void main(String [] args)
{
if (args.length < 2)
{
try
{
HSSF hssf = new HSSF(args[ 0 ]);
System.out.println("Data dump:\n");
HSSFWorkbook wb = hssf._hssfworkbook;
for (int k = 0; k < wb.getNumberOfSheets(); k++)
{
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("Sheet " + k + " \""
+ wb.getSheetName(k) + "\" has "
+ rows + " row(s).");
for (int r = 0; r < rows; r++)
{
HSSFRow row = sheet.getRow(r);
if (row == null) {
continue;
}
int cells = row.getPhysicalNumberOfCells();
System.out.println("\nROW " + row.getRowNum()
+ " has " + cells + " cell(s).");
for (int c = 0; c < cells; c++)
{
HSSFCell cell = row.getCell(c);
String value = null;
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA :
value = "FORMULA value="
+ cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC :
value = "NUMERIC value="
+ cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING :
value = "STRING value="
+ cell.getStringCellValue();
break;
default :
}
System.out.println("CELL col="
+ cell.getColumnIndex()
+ " VALUE=" + value);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if (args.length == 2)
{
if (args[ 1 ].toLowerCase().equals("write"))
{
System.out.println("Write mode");
try
{
long time = System.currentTimeMillis();
HSSF hssf = new HSSF(args[ 0 ], true);
System.out
.println("" + (System.currentTimeMillis() - time)
+ " ms generation time");
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
System.out.println("readwrite test");
try
{
HSSF hssf = new HSSF(args[ 0 ]);
// HSSFStream hssfstream = hssf.hssfstream;
HSSFWorkbook wb = hssf._hssfworkbook;
FileOutputStream stream = new FileOutputStream(args[ 1 ]);
// HSSFCell cell = new HSSFCell();
// cell.setCellNum((short)3);
// cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
// cell.setCellValue(-8009.999);
// hssfstream.modifyCell(cell,0,(short)6);
wb.write(stream);
stream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
else if ((args.length == 3)
&& args[ 2 ].toLowerCase().equals("modify1"))
{
try // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
{
HSSF hssf = new HSSF(args[ 0 ]);
// HSSFStream hssfstream = hssf.hssfstream;
HSSFWorkbook wb = hssf._hssfworkbook;
FileOutputStream stream = new FileOutputStream(args[ 1 ]);
HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++)
{
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
for (int k = 74; k < 100; k++)
{
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.getCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("MODIFIED CELL!!!!!");
// HSSFCell cell = new HSSFCell();
// cell.setCellNum((short)3);
// cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
// cell.setCellValue(-8009.999);
// hssfstream.modifyCell(cell,0,(short)6);
wb.write(stream);
stream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
|