gpt4 book ai didi

java - 如何摆脱使用 Apache POI XSSF 创建的 xlsx 文件上的 "Save changes?"提示

转载 作者:行者123 更新时间:2023-11-30 06:09:29 24 4
gpt4 key购买 nike

打开并立即关闭使用 Apache POI XSSF 创建的 xlsx 文件后,系统提示我保存未保存的更改。据我所知,这是因为我在 xlsx 文件中使用了公式。

根据 javadoc,这应该通过设置 XSSFWorkbook.setForceFormulaRecalculation(true) 来绕过但是,这并不能解决问题。

我还尝试在保存文件之前手动重新计算公式,但没有成功。

中南合作:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSFExample {

public static void main(String[] args) {
// Create workbook and sheet
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet 1");

// Create a row and put some cells in it.
Row row = sheet.createRow((short) 0);
row.createCell(0).setCellValue(5.0);
row.createCell(1).setCellValue(5.0);
row.createCell(2).setCellFormula("A1/B1");


// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("XSSFExample.xlsx")) {
wb.setForceFormulaRecalculation(false);
System.out.println(wb.getForceFormulaRecalculation()); // prints "false"
XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) wb); // this doesn't seem to make any difference
wb.write(fileOut);
} catch (IOException ex) {
Logger.getLogger(XSSFExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

我该怎么做才能创建文件并且在我第一次打开它后没有收到保存它的提示?

更新:
如前所述 here (https://poi.apache.org/spreadsheet/eval.html#recalculation)我还尝试了另一种方法来手动重新计算但没有成功。即使在保存、重新计算和另存为第二个文件后重新读取文件也不起作用。

更新 2:
考虑到已接受的答案,我能够通过向上述 SSCCE 添加以下代码行来解决问题:
(请注意,这只是解决问题的“快速而肮脏”的尝试。可能有很多改进的可能)。

ZipFile zipFile = new ZipFile("XSSFExample.xlsx");
final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("XSSFExample_NoSave.xlsx"));
for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
ZipEntry entryIn = (ZipEntry) e.nextElement();
if (!entryIn.getName().equalsIgnoreCase("xl/workbook.xml")) {
zos.putNextEntry(entryIn);
InputStream is = zipFile.getInputStream(entryIn);
byte[] buf = new byte[1024];
int len;
while ((len = (is.read(buf))) > 0) {
zos.write(buf, 0, len);
}
} else {
zos.putNextEntry(new ZipEntry("xl/workbook.xml"));
InputStream is = zipFile.getInputStream(entryIn);
byte[] buf = new byte[1024];
int len;
while (is.read(buf) > 0) {
String s = new String(buf);
String searchFileVersion = "/relationships\"><workbookPr";
String replaceFileVersion = "/relationships\"><fileVersion appName=\"xl\" lastEdited=\"5\" lowestEdited=\"5\" rupBuild=\"9303\"/><workbookPr";
String searchCalcId = "<calcPr calcId=\"0\"/>";
String replaceCalcId = "<calcPr calcId=\"" + String.valueOf(Integer.MAX_VALUE) + "\"/>";
if (s.contains(searchFileVersion)) {
s = s.replaceAll(searchFileVersion, replaceFileVersion);
}
if (s.contains(searchCalcId)) {
s = s.replaceAll(searchCalcId, replaceCalcId);
}
len = s.trim().length();
buf = s.getBytes();
zos.write(buf, 0, (len < buf.length) ? len : buf.length);
}
}
zos.closeEntry();
}
zos.close();

最佳答案

即使我也面临同样的问题,但在添加以下行后,问题已得到解决。

wb.getCreationHelper().createFormulaEvaluator().evaluateAll();

关于java - 如何摆脱使用 Apache POI XSSF 创建的 xlsx 文件上的 "Save changes?"提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com