How to copy entire sheets from large excel files w

2019-07-17 04:07发布

问题:

I am new as an official user, I always found my answers here but now I have got to ask.

I am working with the last apache-poi 3.8 release (from 2012/03/26) and I have to manipulate a single file with multiple sheets in which only one contains a big amount of data (over 1000 columns and 10 000 rows).

I only need to had more columns to the big sheet. Therefore, with the current tools that exist I should use SAX to read it and SXSSF to rewrite it.

The Excel file is already preformatted with different styles and images in every sheet therefore it will be helpful to be able to make a copy of the file without the big sheet.

There goes my question: How can I make a copy of sheet with SAX (from the input stream in ) without parsing it? I tried to do like in here but the field sheets in XSSFWorkbook has a visibility set to private.

The awesome thing would be to have something like a SXSSFWriter.SheetIterator if it is in future plans for POI Developers.

Thanks for reading,

Arthur

**Update**

The file is too big to be able to open it as a common XSSFWorkbook (OutOfMemoryException). Could it be possible to create and XSSFSheet from an InputStream? Like in the following:

  XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader
            .getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        String sheetName = iter.getSheetName();
        if (!sheetName.equalsIgnoreCase("BigSheetThatIDontWant")) {
            Sheet newSheet = new XSSFSheet(stream);
            stream.close();
        }
        ++index;
    }

Thanks a lot for your answers.

回答1:

You will have to read the file.

Regarding second question see public java.util.Iterator<XSSFSheet> XSSFWorkbook.iterator()

Allows foreach loops:

 XSSFWorkbook wb = new XSSFWorkbook(package);
 for(XSSFSheet sheet : wb){

 }


回答2:

I came to realize throughout my experience with POI and copy operations that, as far as performance is not critical, in order to copy one or more sheet it's safer and easier to load the whole workbook, delete the unnecessary sheets and then save the result in another file.

And +1 for Andy for the iterator.