在使用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();
}
}
我刚刚发现,如果我删除对setCellFormula的调用,问题就会消失,这没有任何意义。