首页 > 代码库 > 问题:Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容?【原创】

问题:Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容?【原创】

现象:

技术分享

点"是(Y)"

技术分享

提示信息中提到的error242440_02.xml文件:

技术分享

 

问题重现:

技术分享

package poi;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class FoundUnreadRecord {    public static void main(String[] args) throws IOException {        int rowNum = 1;        createExcel_Reference(rowNum);        createExcel_WithTips(rowNum);                rowNum = 0;        createExcel_Reference(rowNum);        createExcel_WithTips(rowNum);    }    private static void createExcel_Reference(int rowNum) throws FileNotFoundException,            IOException {        String fileName = "UnreadRecordTips_Reference"+rowNum+".xlsx";                XSSFWorkbook wb = new XSSFWorkbook();                Sheet sheet = wb.createSheet("UnreadRecordTips");        for (int i = 0; i <= rowNum; i++) {            Row row = sheet.createRow(i);            Cell cell = row.createCell(0);            cell.setCellValue("test" + i);        }        write(fileName, wb);    }    private static void createExcel_WithTips(int rowNum) throws FileNotFoundException,            IOException {        String fileName = "UnreadRecordTips"+rowNum+".xlsx";;        XSSFWorkbook wb = new XSSFWorkbook();        Sheet sheet = wb.createSheet("UnreadRecordTips");        for (int i = 0; i <= rowNum; i++) {            Row row = sheet.createRow(i);            Cell cell = row.createCell(0);            cell.setCellValue("test" + i);            sheet.addMergedRegion(new CellRangeAddress(0, rowNum, 0, 0));        }        write(fileName, wb);    }    private static void write(String fileName, XSSFWorkbook wb)            throws FileNotFoundException, IOException {        OutputStream stream = new FileOutputStream(fileName);        wb.write(stream);        stream.close();    }}
分析及原因:
 
org.apache.poi.xssf.usermodel.XSSFWorkbook下进行合并单元格操作,

org.apache.poi.ss.util.CellRangeAddress.CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
中涉及的单元格,如果对其中任何一个单元格进行超过一次addMergedRegion操作,则生成的excel打开时,会出现以上提示。
TIPS:
(1)org.apache.poi.hssf.usermodel.HSSFWorkbook.HSSFWorkbook()无此问题。可能是因为XSSFWorkbook是基于OOXML(.xlsx) file format
(2)POI进行一次addMergedRegion操作

 

问题:Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容?【原创】