DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30978>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30978

removeSheetAt() method corrupts workbook

           Summary: removeSheetAt() method corrupts workbook
           Product: POI
           Version: 2.5
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: Major
          Priority: Other
         Component: HSSF
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


I have an excel workbook A with 3 worksheets. I need to create 3 additional  
workbooks each contains a worksheet from workbook A. Because currently HSSF 
model doesn't allow copy sheets, I thought I could workaround by openning the 
workbook A, remove 2 worksheets, write it back out as workbook A1, (and keep 
doing the samething until all workbooks have been produced). Here is my code:

public void testRemoveSheets() {
  String savePath = "C:/temp/Doc/";
  String sheetNames[] = {"Sheet1", "Sheet2", "Sheet3"};
  try {
     for (int i = 0; i < sheetNames.length; i++) {
       String sn  = sheetNames[i];
       POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(savePath 
+ "TestRemove2.xls"));
       HSSFWorkbook wb = new HSSFWorkbook(fs);
       FileOutputStream fos = new FileOutputStream(savePath + "TestRemove2-" + 
sn + ".xls");
                        
       for (int j = 0; j < sheetNames.length; j++) {
         String s = sheetNames[j];
         if (!s.equals(sn)) wb.removeSheetAt(wb.getSheetIndex(s));
       }
                                
       wb.write(fos);
       fos.close();
    }
  }
  catch (Exception e) {System.out.println(e.toString());}
}

Everything seamed to be OK because I received no error. However, when I tried 
to open the newly generated workbooks, MS Excel (ver 2002) gave me an error. 
Here is the error log
**************
Microsoft Excel File Repair Log

Errors were detected in file 'C:\temp\Doc\TestRemove2-Sheet1.xls'
The following is a list of repairs:

Removed one or more invalid names.
**************

Data seems to be OK but the error is annoying/scary to users. And the file 
size is always bigger than it would be if generated individually.

This bug is similar to bug # 29619 - Excel file becomes corrupt after sheet 
deletion.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to