On 4/21/2010 6:27 PM, Ramon F Herrera wrote:
Hello all,
I am using the example provided with the POI distribution
(ReadWriteWorkBook.java, below) as a reference. It works fine, but when
I try to port it to XSSF, the final write() statement fails.
The file is created, but seems to be corrupted.
In my next post, I will include the program as modified by me.
TIA,
-Ramon
--------------------------------------------------------
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This example demonstrates opening a workbook, modifying
* it and writing the results back out.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class ReadWriteWorkbook {
public static void main(String[] args) throws IOException {
FileInputStream fileIn = null;
FileOutputStream fileOut = null;
try
{
fileIn = new FileInputStream("workbook.xls");
POIFSFileSystem fs = new POIFSFileSystem(fileIn);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
if (row == null)
row = sheet.createRow(2);
HSSFCell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
fileOut = new FileOutputStream("workbookout.xls");
wb.write(fileOut);
} finally {
if (fileOut != null)
fileOut.close();
if (fileIn != null)
fileIn.close();
}
}
}
This is the modified version:
------------------------------------
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This example demonstrates opening a workbook, modifying
* it and writing the results back out.
* @author Glen Stampoultzis (glens at apache.org)
*/
public class ReadWriteWorkbook {
public static void main(String[] args) throws IOException {
FileInputStream fileIn = null;
FileOutputStream fileOut = null;
XSSFWorkbook wb;
try {
fileIn = new FileInputStream("1-Tab.xlsx");
wb = new XSSFWorkbook(fileIn);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
if (row == null)
row = sheet.createRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
fileOut = new FileOutputStream("workbookout.xlsx");
wb.write(fileOut); // Crashes here, creating an empty file
} finally {
if (fileOut != null)
fileOut.close();
if (fileIn != null)
fileIn.close();
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]