使用 MS-Excel 打开 .xlsx 时遇到问题,其中 APACHE POI XSSFDataValidationHelper 用于数字和日期验证
Facing an issue while opening .xlsx using MS-Excel in which APACHE POI XSSFDataValidationHelper is used for numeric and date validation
我在 java 中使用 Apache POI 3.17、XSSFDataValidationHelper 为 .xlsx 文件中的日期和数值等数据添加验证,但每当我使用 MS-Excel 打开文件时,它都会显示
“我们发现 .您是否希望我们尽可能多地尝试恢复文件?如果您信任此工作簿的来源,请单击“是”
当我检查这个错误时,我发现,当文件部分损坏或完全损坏时会发生这种情况。
这个问题只出现在那些我使用了上面提到的任何类型的数据验证的 .xlsx 文件上,但是如果我在 linux 环境中打开那个 .xlsx 文件也不会发生这个问题。
P.S :我在最后关闭工作簿和 FileOutputStream,在工作簿下至少创建了一个 sheet(显然)
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_VALUE
和 Double.MAX_VALUE
。 Excel
对数字单元格值有更严格的限制。您不能在 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,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,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 中使用 Apache POI 3.17、XSSFDataValidationHelper 为 .xlsx 文件中的日期和数值等数据添加验证,但每当我使用 MS-Excel 打开文件时,它都会显示
“我们发现 .您是否希望我们尽可能多地尝试恢复文件?如果您信任此工作簿的来源,请单击“是”
当我检查这个错误时,我发现,当文件部分损坏或完全损坏时会发生这种情况。
这个问题只出现在那些我使用了上面提到的任何类型的数据验证的 .xlsx 文件上,但是如果我在 linux 环境中打开那个 .xlsx 文件也不会发生这个问题。
P.S :我在最后关闭工作簿和 FileOutputStream,在工作簿下至少创建了一个 sheet(显然)
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_VALUE
和 Double.MAX_VALUE
。 Excel
对数字单元格值有更严格的限制。您不能在 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,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,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();
}
}