Java 带有未保存更改的Apache poi xlsx文件

Java 带有未保存更改的Apache poi xlsx文件,java,excel,apache,apache-poi,xlsx,Java,Excel,Apache,Apache Poi,Xlsx,在使用poi创建excel电子表格时,我面临着一种异常的行为。我可以保存内容,但当我在excel上打开它并尝试在不触摸它的情况下关闭文档时,我会收到一个消息框,其中有一行消息“您想保存您的修改吗?” 代码: protected static Map< String ,CellStyle> makeCellStyles(XSSFWorkbook workbook) { Map<String , CellStyle> styles = new HashMap&

在使用poi创建excel电子表格时,我面临着一种异常的行为。我可以保存内容,但当我在excel上打开它并尝试在不触摸它的情况下关闭文档时,我会收到一个消息框,其中有一行消息“您想保存您的修改吗?”

代码:

protected static Map< String ,CellStyle> makeCellStyles(XSSFWorkbook workbook) {
        Map<String , CellStyle> styles = new HashMap<String , CellStyle>();
        Font font = workbook.createFont();
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        font.setBold(true);
        style.setFont(font);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setBorderTop(CellStyle.BORDER_MEDIUM);
        style.setBorderLeft(CellStyle.BORDER_MEDIUM);
        style.setBorderRight(CellStyle.BORDER_MEDIUM);
        style.setBorderBottom(CellStyle.BORDER_MEDIUM);
        styles.put("groupHeader", style);
        style = workbook.createCellStyle();
        font = workbook.createFont();
        font.setColor(HSSFColor.WHITE.index);
        font.setBold(true);
        style.setBorderTop(CellStyle.BORDER_MEDIUM);
        style.setBorderLeft(CellStyle.BORDER_MEDIUM);
        style.setBorderRight(CellStyle.BORDER_MEDIUM);
        style.setBorderBottom(CellStyle.BORDER_MEDIUM);
        style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFont(font);
        styles.put("rowHeader", style);
        DataFormat format = workbook.createDataFormat();
        style = workbook.createCellStyle();
        style.setBorderRight(CellStyle.BORDER_MEDIUM);
        style.setDataFormat(format.getFormat("# ##0"));
        style.setAlignment(CellStyle.ALIGN_CENTER);
        styles.put("borderData", style);
        style = workbook.createCellStyle();
        //style.setBorderBottom(CellStyle.BORDER_MEDIUM);

        style.setBorderRight(CellStyle.BORDER_MEDIUM);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setDataFormat(format.getFormat("# ##0"));
        styles.put("data", style);
        return styles;
    }

    static private void writeXls() {
        // Using XSSF for xlsx format, for xls use HSSF
        //final String FILE_PATH = "F:/lesStats.xlsx";
        try {

            FileOutputStream fos = new FileOutputStream(statfile);
            XSSFWorkbook workbook = new XSSFWorkbook();
            DataFormat format = workbook.createDataFormat();
            String si = "Fichier;total documents; documents corrects;"
                + "documents erronnees; erreurs formats ; "
                + "erreur dictionnaire ; erreurs index manquants ; erreurs chemin";
            String[] entetes = si.split(";");
            Sheet globalStat = workbook.createSheet("statistiques gobales");
            setXlsProperties(workbook);
            int cellIndex = 0;
            int rowIndex = 0;
            Row row = globalStat.createRow(rowIndex++);
            row = globalStat.createRow(rowIndex++);
            row = globalStat.createRow(rowIndex++);
            Map<String ,CellStyle> styles = makeCellStyles(workbook);


            for (int i = 0; i < 9; i++) {
                if (i != 4)
                    row.createCell(i).setCellValue("");
                else {
                    row.createCell(i).setCellValue("Type Erreurs");
                }
            }

            for (int i = 4; i < 8; i++) {
                row.getCell(i).setCellStyle(styles.get("groupHeader"));
                // row.getCell(i).getCellStyle().setBorderTop(CellStyle.BORDER_MEDIUM);

            }
            row.getCell(7).getCellStyle().setBorderRight(CellStyle.BORDER_MEDIUM);
                // (groupHeaderStyle);
            globalStat.addMergedRegion(new CellRangeAddress(2, 2, 4, 7));
            // row.getCell(4).setCellStyle(groupHeaderStyle);
            row = globalStat.createRow(rowIndex++);
            cellIndex = 0;

            for (String val : entetes) {

                row.createCell(cellIndex++).setCellValue(val);
                row.getCell(cellIndex - 1).setCellStyle(styles.get("rowHeader"));
            }

            int fileIndex = 0;

            for (Integer[] info : lesstats) {
                row = globalStat.createRow(rowIndex++);
                cellIndex = 0;
                row.createCell(cellIndex++).setCellValue(filescecked.get(fileIndex++));
                row.getCell(0).setCellStyle(styles.get("rowHeader"));
                for (Integer i : info) {

                    row.createCell(cellIndex).setCellValue(i);
                    row.getCell(cellIndex).setCellStyle(styles.get("data"));
                    cellIndex++;
                }

            }
            System.out.println(lesstats.size());
            for (int i = 1; i <= lesstats.get(0).length; i++) {
                row.getCell(i).setCellStyle(styles.get("borderData"));
            }

            row = globalStat.createRow(rowIndex);
            row.createCell(0);
            for (int i = 1; i < 8; i++) {
                row.createCell(i);

                Integer limit = 3 + lesstats.size();
                String col = "";
                col += (char) (65 + i);
                col += "3";
                col += ":";
                col += (char) (65 + i);
                col += limit.toString() + ")";
                System.out.println("\tSUM(" + col);

                row.getCell(i).setCellFormula("SUM(" + col);
                row.getCell(i).setCellStyle(styles.get("rowHeader"));
                row.getCell(i).getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);
                row.getCell(i).getCellStyle().setDataFormat(format.getFormat("# ##0"));
            }
            for (int i = 0; i < 9; i++)
                globalStat.autoSizeColumn(i);
            //workbook.close();
            workbook.write(fos);

            fos.close();
            workbook.close();
            System.out.println(statfile + " is successfully written");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
protectedstaticmapmakeCellStyles(XSSF工作簿工作簿){
映射样式=新HashMap();
Font=workbook.createFont();
CellStyle style=workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);
font.setBold(true);
style.setFont(字体);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setBorderTop(CellStyle.BORDER_-MEDIUM);
style.setboorderleft(CellStyle.BORDER_-MEDIUM);
style.setboordright(CellStyle.BORDER_-MEDIUM);
样式.底部(CellStyle.BORDER_-MEDIUM);
样式。放置(“groupHeader”,样式);
style=workbook.createCellStyle();
font=workbook.createFont();
font.setColor(HSSFColor.WHITE.index);
font.setBold(true);
style.setBorderTop(CellStyle.BORDER_-MEDIUM);
style.setboorderleft(CellStyle.BORDER_-MEDIUM);
style.setboordright(CellStyle.BORDER_-MEDIUM);
样式.底部(CellStyle.BORDER_-MEDIUM);
style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);
style.setFont(字体);
样式。放置(“行标题”,样式);
DataFormat=workbook.createDataFormat();
style=workbook.createCellStyle();
style.setboordright(CellStyle.BORDER_-MEDIUM);
style.setDataFormat(format.getFormat(“####0”);
style.setAlignment(CellStyle.ALIGN_CENTER);
样式。放置(“边框数据”,样式);
style=workbook.createCellStyle();
//样式.底部(CellStyle.BORDER_-MEDIUM);
style.setboordright(CellStyle.BORDER_-MEDIUM);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setDataFormat(format.getFormat(“####0”);
样式。放置(“数据”,样式);
返回样式;
}
静态私有void writeXls(){
//对于xlsx格式使用XSSF,对于xls使用HSSF
//最终字符串文件_PATH=“F:/lesStats.xlsx”;
试一试{
FileOutputStream fos=新的FileOutputStream(statfile);
XSSFWorkbook工作簿=新XSSFWorkbook();
DataFormat=workbook.createDataFormat();
String si=“Fichier;文档总数;文档更正;”
+“文档错误;错误格式;”
+“语言错误;数量错误指数;化学错误”;
字符串[]entetes=si.split(“;”);
Sheet globalStat=workbook.createSheet(“statistiques gobales”);
setXlsProperties(工作簿);
int cellIndex=0;
int rowIndex=0;
Row-Row=globalStat.createRow(rowIndex++);
row=globalStat.createRow(rowIndex++);
row=globalStat.createRow(rowIndex++);
地图样式=makeCellStyles(工作簿);
对于(int i=0;i<9;i++){
如果(i!=4)
row.createCell(i).setCellValue(“”);
否则{
row.createCell(i).setCellValue(“类型错误”);
}
}
对于(int i=4;i<8;i++){
row.getCell(i).setCellStyle(styles.get(“groupHeader”));
//row.getCell(i.getCellStyle().setBorderTop(CellStyle.BORDER_MEDIUM);
}
row.getCell(7.getCellStyle().setBorderRight(CellStyle.BORDER_-MEDIUM);
//(团长式);
globalStat.addMergedRegion(新的CellRangeAddress(2,2,4,7));
//row.getCell(4)、setCellStyle(groupHeaderStyle);
row=globalStat.createRow(rowIndex++);
细胞指数=0;
for(字符串值:entetes){
row.createCell(cellIndex++).setCellValue(val);
row.getCell(cellIndex-1).setCellStyle(styles.get(“rowHeader”));
}
int fileIndex=0;
对于(整数[]信息:lesstats){
row=globalStat.createRow(rowIndex++);
细胞指数=0;
row.createCell(cellIndex++)、setCellValue(filescecked.get(fileIndex++));
row.getCell(0.setCellStyle(styles.get(“rowHeader”));
用于(整数i:info){
row.createCell(cellIndex).setCellValue(i);
row.getCell(cellIndex.setCellStyle(style.get(“数据”));
cellIndex++;
}
}
System.out.println(lesstats.size());

对于(int i=1;我认为您实际上不需要调用workbook.close()。。。