简体   繁体   中英

excel 2007 unreadable content using apache poi (.xlsx)

I'm having issue using the apache poi for excel 2007 utilizing the XSSF

I have two code snippets below (fat-fingered). The first seems to work, as the workbook opens correctly in excel 2007, and there is some change.

If I run the second code snippet then open in excel 2007, I get an error about unreadable content. I have to click a confirmation box, then excel opens my file.

Why is the second code snippet causing this error? the only difference is in the second one I am trying to shift all rows from row 2(zero indexed) up two rows...

All i really want to do is remove some header rows, and after I get that down some footer rows from a file. I'm not doing anything fancy. I'm guessing I'm misunderstanding the API, but I've been fighting with this for a while now.

Any ideas? Also should I be shifting the rows, or removing them, or some combination of both?

String filename = "C:\\file.xlsx";
FileInputStream file = new FileInputStream( new File(fileName) );

XSSFWorkbook wb = new XSSFWorkbook(file);

XSSFSheet sheet = wb.getSheetAt(0);
int lastRow = sheet.getlastRow();

sheet.shiftRows(1, lastRow, -1);


file.close()
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.flush();
out.close();

String filename = "C:\\file.xlsx";
FileInputStream file = new FileInputStream( new File(fileName) );

XSSFWorkbook wb = new XSSFWorkbook(file);

XSSFSheet sheet = wb.getSheetAt(0);
int lastRow = sheet.getlastRow();

sheet.shiftRows(2, lastRow, -2);


file.close()
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.flush();
out.close();

Edit: Actually it apears that the first snippet is also causing an issue...sometimes???? I'm under the impression I am doing something wrong... Any suggestions?

I have run into a similar problem. I think that the problem might lie with the handling of comments. In my case, the excel file was reported as "damaged" by Excel whenever a shift was done when comments were present. Excel repaired the file and reported that it deleted the comments part and the rest was fine. Excel files without comments but with the same processing code, did not cause any problems.

Sorry that this does not solve the problem, but maybe it provides for a workaround. In my case, I delete all the comments programmatically (ugly).

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