I believe the issue here is that XSSF classes are for XML based (Excel 2007)
spreadsheets. For Excel 2003 the HSSF classes should be used.
You could try:
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Workbook wb;
Sheet sheet;
Row row;
Cell cell;
String excel2007File = "newwork_workbook.xlsx";
wb = new XSSFWorkbook(excel2007File);
sheet = wb.getSheetAt(0);
row = sheet.getRow(2);
cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("2007excel.xlsx");
wb.write(fileOut);
fileOut.close();
String excel2003File = "newwork_workbook.xls";
wb = new HSSFWorkbook(excel2003File);
sheet = wb.getSheetAt(0);
row = sheet.getRow(2);
cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("2003excel.xls");
wb.write(fileOut);
fileOut.close();
Good luck,
Bobby
-----Original Message-----
From: MaYong [mailto:[email protected]]
Sent: Wednesday, October 28, 2009 05:56
To: "[email protected]"@athena.apache.org;
"[email protected]"@athena.apache.org;
"[email protected]"@athena.apache.org;
[email protected]
Subject: org.apache.poi.openxml4j.exceptions.InvalidOperationException
Dear All:
when I used XSSF read Excel 2003 file, had a Exception.read Excel 2007,ok.
source:
String excelFile = "newwork_workbook.xls";
// XSSFWorkbook wb=new XSSFWorkbook();
XSSFWorkbook wb = wb = new XSSFWorkbook(excelFile);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(2);
XSSFCell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new XSSFRichTextString("a test"));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("2007excel.xls");
wb.write(fileOut);
fileOut.close();
Exction:
Exception in thread "main"
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open
the specified file: 'newwork_workbook.xls'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:62)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:167)
at PoiTest.readingAndRewritingWorkbooks(PoiTest.java:179)
at PoiTest.main(PoiTest.java:57)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]