You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2014/10/29 18:17:27 UTC

[Bug 57165] New: Failed to clone a sheet from an Excel 2010

https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

            Bug ID: 57165
           Summary: Failed to clone a sheet from an Excel 2010
           Product: POI
           Version: 3.11-dev
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: PhiLho@GMX.net

Created attachment 32165
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=32165&action=edit
The Excel file

I created an Excel 2010 file (XLS first, then saved to XLSX format).
Later, I re-ordered the tabs.

I have code that copies this file, opens it, removes all sheets except the
selected one, then clone this sheet n times before filling them.
Ie. the document is a template with several models.

I found out that in some cases, the clone operation fails.

Note: I have to use a convoluted method to delete the sheets, because of the
bug #57163.

My method to isolate a sheet:

  void removeAllSheetsBut(int sheetIndex, Workbook wb)
  {
    int sheetNb = wb.getNumberOfSheets();
    // Move this sheet at the first position
    wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
    // Must make this sheet active (otherwise, for XLSX, Excel might protest
that active sheet no longer exists)
    // I think POI should automatically handle this case when deleting
sheets...
    wb.setActiveSheet(0);
    for (int sn = sheetNb - 1; sn > 0; sn--)
    {
      wb.removeSheetAt(sn);
    }
  }

(I will open a new bug about the comment, once I will be able to reproduce it.)

Then I just do:

Workbook wb = readWorkbook(fileName);
removeAllSheetsBut(3, wb);
wb.cloneSheet(0);
wb.setSheetName(1, "New Sheet");
saveWorkbook(wb, fileName + " Updated");

On the cloneSheet line, I get an exception:

Exception in thread "main"
org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException: A part with the
name '/xl/worksheets/sheet4.xml' already exists : Packages shall not contain
equivalent part names and package implementers shall neither create nor
recognize packages with equivalent part names. [M1.12]
    at org.apache.poi.openxml4j.opc.OPCPackage.createPart(OPCPackage.java:785)
    at org.apache.poi.openxml4j.opc.OPCPackage.createPart(OPCPackage.java:749)
    at
org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:374)
    at
org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:358)
    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:728)
    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.cloneSheet(XSSFWorkbook.java:503)
    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.cloneSheet(XSSFWorkbook.java:102)

>From my investigation, I would say the culprit is the line:

for(XSSFSheet sh : sheets) sheetNumber = (int)Math.max(sh.sheet.getSheetId() +
1, sheetNumber);

in XSSFSheet createSheet(String sheetname) (in XSSFWorkbook).

This sheetNumber is used to name the sheet<n>.xml file, but apparently, after
moving around the tabs (I suppose) in Excel, they are no longer in synch, hence
the conflict.

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 57165] Failed to clone a sheet from an Excel 2010

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

--- Comment #4 from Dominik Stadler <do...@gmx.at> ---
FYI, there is now a test in class org.apache.poi.xssf.usermodel.TestUnfixedBugs
which reproduces this, a fix is still pending, though...

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 57165] Failed to clone a sheet from an Excel 2010

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

Nick Burch <ap...@gagravarr.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |NEEDINFO
                 OS|                            |All

--- Comment #1 from Nick Burch <ap...@gagravarr.org> ---
Can you create a small junit unit test which triggers the problem? We can then
use that to test a fix, and to ensure it stays fixed going forward?

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 57165] Failed to clone a sheet from an Excel 2010

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

--- Comment #3 from Philippe Lhoste <Ph...@GMX.net> ---
Not really a JUnit test, but it is a standalone class that loads the sample
Excel file and reproduce the bugs (three bugs are shown, I numbered them with
the Bugzilla id).
You might need to adjust the path to the sample.

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 57165] Failed to clone a sheet from an Excel 2010

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

--- Comment #2 from Philippe Lhoste <Ph...@GMX.net> ---
Created attachment 32174
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=32174&action=edit
Show bugs

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 57165] Failed to clone a sheet from an Excel 2010

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=57165

Dominik Stadler <do...@gmx.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEEDINFO                    |NEW

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org