gpt4 book ai didi

java - 使用 MS-Excel 打开 .xlsx 时遇到问题,其中 APACHE POI XSSFDataValidationHelper 用于数字和日期验证

转载 作者:行者123 更新时间:2023-12-02 08:45:00 24 4
gpt4 key购买 nike

我在 java 中使用 Apache POI 3.17、XSSFDataValidationHelper 来添加对 .xlsx 文件中日期和数值等数据的验证,但每当我使用 MS-Excel 打开文件时,它都会显示

“我们发现 .您希望我们尽可能尝试恢复文件吗?如果您信任此工作簿的来源,请单击"is"”

当我检查此错误时,我发现,当文件部分损坏或完全损坏时会发生这种情况。

此问题仅出现在那些我使用了上面提到的任何类型的数据验证的 .xlsx 文件上,但如果我在 Linux 环境中打开该 .xlsx 文件,也不会发生此问题。

P.S:我最后关闭工作簿和 FileOutputStream,在工作簿下创建了至少一张工作表(显然)

PFB 用于数据验证的代码片段:-

`XSSFDataValidationHelper dataValidationHelper = new XSSFDataValidationHelper(sheet);

XSSFDataValidationConstraint numberValidationConstraint =
(XSSFDataValidationConstraint)
dataValidationHelper.createNumericConstraint(
XSSFDataValidationConstraint.ValidationType.DECIMAL,
XSSFDataValidationConstraint.OperatorType.BETWEEN,
String.valueOf(Double.MIN_VALUE),
String.valueOf(Double.MAX_VALUE)
);

CellRangeAddressList addressList = new CellRangeAddressList(
2, 2000, columnCounter, columnCounter);
XSSFDataValidation numberValidation =(XSSFDataValidation)dataValidationHelper.createValidation(
numberValidationConstraint, addressList);
numberValidation.setSuppressDropDownArrow(false);
numberValidation.setShowErrorBox(true);
numberValidation.createErrorBox("Invalid data","Only numbers are allowed");
sheet.addValidationData(numberValidation);`

最佳答案

您不能在 Excel 数据验证中使用 Double.MIN_VALUEDouble.MAX_VALUEExcel 对数字单元格值有更严格的限制。您无法在 Excel 单元格中存储 1.7976931348623157E308,也不能存储 4.9E-324。在 Excel 单元格中只能存储 15 位有效数字。因此,您可以存储的最小数字是-9.99999999999999E307,您可以存储的最大数字是9.99999999999999E307。因此,您必须使用该数字来限制可能的数字范围。

DataValidationConstraint numberValidationConstraint = dataValidationHelper.createNumericConstraint(
DataValidationConstraint.ValidationType.DECIMAL,
DataValidationConstraint.OperatorType.BETWEEN,
//String.valueOf(Double.MIN_VALUE),
//String.valueOf(Double.MAX_VALUE)
"-9.99999999999999E307",
"9.99999999999999E307"
);

但我怀疑目标是只允许数值。这也可以使用具有公式的自定义公式约束来实现

=ISNUMBER(OFFSET($A$1,ROW()-1,COLUMN()-1))

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;

class CreateExcelDataValidation {

public static void main(String[] args) throws Exception {

//Workbook workbook = new HSSFWorkbook();
Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet();

int columnCounter = 0;
int fromRow = 2;
int toRow = 2000;

DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();

/*
DataValidationConstraint numberValidationConstraint = dataValidationHelper.createNumericConstraint(
DataValidationConstraint.ValidationType.DECIMAL,
DataValidationConstraint.OperatorType.BETWEEN,
//String.valueOf(Double.MIN_VALUE),
//String.valueOf(Double.MAX_VALUE)
"-9.99999999999999E307",
"9.99999999999999E307"
);
*/

DataValidationConstraint numberValidationConstraint = dataValidationHelper.createCustomConstraint(
"ISNUMBER(OFFSET($A$1,ROW()-1,COLUMN()-1))"
);

CellRangeAddressList addressList = new CellRangeAddressList(fromRow, toRow, columnCounter, columnCounter);
DataValidation numberValidation = dataValidationHelper.createValidation(numberValidationConstraint, addressList);
numberValidation.setShowErrorBox(true);
numberValidation.createErrorBox("Invalid data","Only numbers are allowed");
sheet.addValidationData(numberValidation);

FileOutputStream out = null;
if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("CreateExcelDataValidation.xls");
} else if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("CreateExcelDataValidation.xlsx");
}
workbook.write(out);
workbook.close();
out.close();

}
}

关于java - 使用 MS-Excel 打开 .xlsx 时遇到问题,其中 APACHE POI XSSFDataValidationHelper 用于数字和日期验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61138970/

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