简体   繁体   中英

How to copy entire sheets from large excel files without parsing them using Apache POI?

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.

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){

 }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM