Fixed compiler error in HSSFReadWrite. Fixed deprecated calls and simplified example code. (follow-on from r829758)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@829897 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-10-26 18:55:51 +00:00
parent 8d7f1bb83a
commit eb4b1d0aac

View File

@ -15,8 +15,7 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
package org.apache.poi.hssf.examples.usermodel;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -30,69 +29,35 @@ import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/** /**
* File for HSSF testing/examples * File for HSSF testing/examples
* *
* THIS IS NOT THE MAIN HSSF FILE!! This is a util for testing functionality. * THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API users. * It does contain sample API usage that may be educational to regular API
* users.
* *
* @see #main * @see #main
* @author Andrew Oliver (acoliver at apache dot org) * @author Andrew Oliver (acoliver at apache dot org)
*/ */
public final class HSSFReadWrite {
public class HSSFReadWrite
{
private String filename = null;
protected HSSFWorkbook hssfworkbook = null;
/** /**
* Constructor HSSFReadWrite - creates an HSSFStream from an InputStream. The HSSFStream * creates an {@link HSSFWorkbook} the specified OS filename.
* reads in the records allowing modification.
*
*
* @param filename
*
* @exception IOException
*
*/ */
private static HSSFWorkbook readFile(String filename) throws IOException {
public HSSFReadWrite(String filename) return new HSSFWorkbook(new FileInputStream(filename));
throws IOException
{
this.filename = filename;
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
hssfworkbook = new HSSFWorkbook(fs);
// records = RecordFactory.createRecords(stream);
} }
/** /**
* Constructor HSSFReadWrite - given a filename this outputs a sample sheet with just * given a filename this outputs a sample sheet with just a set of
* a set of rows/cells. * rows/cells.
*
*
* @param filename
* @param write
*
* @exception IOException
*
*/ */
private static void testCreateSampleSheet(String outputFilename) throws IOException {
public HSSFReadWrite(String filename, boolean write) int rownum;
throws IOException
{
short rownum = 0;
FileOutputStream out = new FileOutputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet(); HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle();
@ -112,46 +77,36 @@ public class HSSFReadWrite
cs2.setFillForegroundColor((short) 0xA); cs2.setFillForegroundColor((short) 0xA);
cs2.setFont(f2); cs2.setFont(f2);
wb.setSheetName(0, "HSSF Test"); wb.setSheetName(0, "HSSF Test");
for (rownum = ( short ) 0; rownum < 300; rownum++) for (rownum = 0; rownum < 300; rownum++) {
{ HSSFRow r = s.createRow(rownum);
r = s.createRow(rownum); if ((rownum % 2) == 0) {
if ((rownum % 2) == 0)
{
r.setHeight((short) 0x249); r.setHeight((short) 0x249);
} }
// r.setRowNum(( short ) rownum); for (int cellnum = 0; cellnum < 50; cellnum += 2) {
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2) HSSFCell c = r.createCell(cellnum);
{
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(rownum * 10000 + cellnum c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000) + (((double) rownum / 1000) + ((double) cellnum / 10000)));
+ (( double ) cellnum / 10000))); if ((rownum % 2) == 0) {
if ((rownum % 2) == 0)
{
c.setCellStyle(cs); c.setCellStyle(cs);
} }
c = r.createCell(cellnum + 1, c = r.createCell(cellnum + 1);
HSSFCell.CELL_TYPE_STRING);
c.setCellValue(new HSSFRichTextString("TEST")); c.setCellValue(new HSSFRichTextString("TEST"));
// 50 characters divided by 1/20th of a point
s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05)); s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
if ((rownum % 2) == 0) if ((rownum % 2) == 0) {
{
c.setCellStyle(cs2); 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 // draw a thick black border on the row at the bottom using BLANKS
rownum++; rownum++;
rownum++; rownum++;
r = s.createRow(rownum); HSSFRow r = s.createRow(rownum);
cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK); cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
for (short cellnum = ( short ) 0; cellnum < 50; cellnum++) for (int cellnum = 0; cellnum < 50; cellnum++) {
{ HSSFCell c = r.createCell(cellnum);
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_BLANK);
// c.setCellValue(0);
c.setCellStyle(cs3); c.setCellStyle(cs3);
} }
s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3)); s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
@ -164,208 +119,126 @@ public class HSSFReadWrite
wb.removeSheetAt(1); wb.removeSheetAt(1);
// end deleted sheet // end deleted sheet
FileOutputStream out = new FileOutputStream(outputFilename);
wb.write(out); wb.write(out);
out.close(); out.close();
} }
/**
* Constructor HSSF - takes in file - attempts to read it then reconstruct it
*
*
* @param infile
* @param outfile
* @param write
*
* @exception IOException
*
*/
public HSSFReadWrite(String infile, String outfile, boolean write)
throws IOException
{
this.filename = infile;
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
hssfworkbook = new HSSFWorkbook(fs);
// HSSFWorkbook book = hssfstream.getWorkbook();
}
/** /**
* Method main * Method main
* *
* Given 1 argument takes that as the filename, inputs it and dumps the * Given 1 argument takes that as the filename, inputs it and dumps the
* cell values/types out to sys.out * cell values/types out to sys.out.<br/>
* *
* given 2 arguments where the second argument is the word "write" and the * given 2 arguments where the second argument is the word "write" and the
* first is the filename - writes out a sample (test) spreadsheet (see * first is the filename - writes out a sample (test) spreadsheet
* public HSSF(String filename, boolean write)). * see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
* *
* given 2 arguments where the first is an input filename and the second * given 2 arguments where the first is an input filename and the second
* an output filename (not write), attempts to fully read in the * an output filename (not write), attempts to fully read in the
* spreadsheet and fully write it out. * spreadsheet and fully write it out.<br/>
* *
* given 3 arguments where the first is an input filename and the second an * 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 * 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 * 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 * "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. * take the output from the write test, you'll have a valid scenario.
*
* @param args
*
*/ */
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("At least one argument expected");
return;
}
public static void main(String [] args) String fileName = args[0];
{ try {
if (args.length < 2) if (args.length < 2) {
{
try HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
{
HSSFReadWrite hssf = new HSSFReadWrite(args[ 0 ]);
System.out.println("Data dump:\n"); System.out.println("Data dump:\n");
HSSFWorkbook wb = hssf.hssfworkbook;
for (int k = 0; k < wb.getNumberOfSheets(); k++) for (int k = 0; k < wb.getNumberOfSheets(); k++) {
{
HSSFSheet sheet = wb.getSheetAt(k); HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows(); int rows = sheet.getPhysicalNumberOfRows();
System.out.println("Sheet " + k + " \"" System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
+ wb.getSheetName(k) + "\" has " + " row(s).");
+ rows + " row(s)."); for (int r = 0; r < rows; r++) {
for (int r = 0; r < rows; r++)
{
HSSFRow row = sheet.getRow(r); HSSFRow row = sheet.getRow(r);
int cells = (row != null) ? row.getPhysicalNumberOfCells() : 0; if (row == null) {
if (row != null) { continue;
System.out.println("\nROW " + row.getRowNum()
+ " has " + cells + " cell(s).");
} }
for (int c = 0; c < cells; c++)
{ 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); HSSFCell cell = row.getCell(c);
String value = null; String value = null;
switch (cell.getCellType()) switch (cell.getCellType()) {
{
case HSSFCell.CELL_TYPE_FORMULA: case HSSFCell.CELL_TYPE_FORMULA:
value = "FORMULA value=" value = "FORMULA value=" + cell.getCellFormula();
+ cell.getCellFormula();
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" value = "NUMERIC value=" + cell.getNumericCellValue();
+ cell.getNumericCellValue();
break; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
value = "STRING value=" value = "STRING value=" + cell.getStringCellValue();
+ cell.getStringCellValue();
break; break;
default: default:
} }
System.out.println("CELL col=" System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
+ cell.getCellNum() + value);
+ " VALUE=" + value);
} }
} }
} }
} } else if (args.length == 2) {
catch (Exception e) if (args[1].toLowerCase().equals("write")) {
{
e.printStackTrace();
}
}
else if (args.length == 2)
{
if (args[ 1 ].toLowerCase().equals("write"))
{
System.out.println("Write mode"); System.out.println("Write mode");
try
{
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
HSSFReadWrite hssf = new HSSFReadWrite(args[ 0 ], true); HSSFReadWrite.testCreateSampleSheet(fileName);
System.out System.out.println("" + (System.currentTimeMillis() - time)
.println("" + (System.currentTimeMillis() - time)
+ " ms generation time"); + " ms generation time");
} } else {
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
System.out.println("readwrite test"); System.out.println("readwrite test");
try HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
{
HSSFReadWrite hssf = new HSSFReadWrite(args[ 0 ]);
// HSSFStream hssfstream = hssf.hssfstream;
HSSFWorkbook wb = hssf.hssfworkbook;
FileOutputStream stream = new FileOutputStream(args[1]); 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); wb.write(stream);
stream.close(); stream.close();
} }
catch (Exception e) } else if (args.length == 3 && args[2].toLowerCase().equals("modify1")) {
{ // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
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!!"
{
HSSFReadWrite hssf = new HSSFReadWrite(args[ 0 ]);
// HSSFStream hssfstream = hssf.hssfstream; HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
HSSFWorkbook wb = hssf.hssfworkbook;
FileOutputStream stream = new FileOutputStream(args[1]); FileOutputStream stream = new FileOutputStream(args[1]);
HSSFSheet sheet = wb.getSheetAt(0); HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++) for (int k = 0; k < 25; k++) {
{
HSSFRow row = sheet.getRow(k); HSSFRow row = sheet.getRow(k);
sheet.removeRow(row); sheet.removeRow(row);
} }
for (int k = 74; k < 100; k++) for (int k = 74; k < 100; k++) {
{
HSSFRow row = sheet.getRow(k); HSSFRow row = sheet.getRow(k);
sheet.removeRow(row); sheet.removeRow(row);
} }
HSSFRow row = sheet.getRow(39); HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.getCell(3); HSSFCell cell = row.getCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("MODIFIED CELL!!!!!"); 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); wb.write(stream);
stream.close(); stream.close();
} }
catch (Exception e) } catch (Exception e) {
{
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
}