我想在JAVA中使用POI编写一个大的.xlsx
文件(5万行)。我希望每一行都包含多个下拉单元格。当行数少于30K时,代码可以正常工作;但如果行数超过35K,则写入损坏的文件。
我尝试了SXSSFWorkbook
和XSSFWorkbook
,但是没有一个对我有用。
这是代码:
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
SXSSFSheet sheet = workbook.createSheet();
String[] optionsArray = new String[] {"1000.00","2000.00"};
int no_of_rows = 35000;
for(int i=0;i<=no_of_rows;i++) {
SXSSFRow row1 = sheet.createRow(i);
SXSSFCell r1c1 = row1.createCell(0);
DataValidationConstraint constraint1 = sheet.getDataValidationHelper().createExplicitListConstraint(optionsArray);
CellRangeAddressList addressList1 = new CellRangeAddressList(i, i, 0, 0);
DataValidation dataValidation1 = sheet.getDataValidationHelper().createValidation(constraint1, addressList1);
sheet.addValidationData(dataValidation1);
r1c1.setCellValue("1000.00");
SXSSFCell r1c2 = row1.createCell(1);
DataValidationConstraint constraint2 = sheet.getDataValidationHelper().createExplicitListConstraint(optionsArray);
CellRangeAddressList addressList2 = new CellRangeAddressList(i, i, 1, 1);
DataValidation dataValidation2 = sheet.getDataValidationHelper().createValidation(constraint2, addressList2);
sheet.addValidationData(dataValidation2);
r1c2.setCellValue("2000.00");
}
FileOutputStream fos =new FileOutputStream(new File("c:\\data\\testout.xlsx"));
workbook.write(fos);
workbook.close();
fos.close();
我希望POI至少可以处理5万行此类数据。
最佳答案
不要为每个单元格创建一个DataValidation
。而是仅创建两个所需的数据验证,一个验证为A1:A50001
,第二个验证为B1:B50001
。为此,CellRangeAddressList
可以包含整个列范围:
CellRangeAddressList addressList1 = new CellRangeAddressList(0, no_of_rows, 0, 0);
和
CellRangeAddressList addressList1 = new CellRangeAddressList(0, no_of_rows, 1, 1);
你的例子:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.*;
class CreateSXSSFWorkbookDataValidations {
public static void main(String[] args) throws Exception {
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
SXSSFSheet sheet = workbook.createSheet();
String[] optionsArray = new String[] {"1000.00","2000.00"};
int no_of_rows = 50000;
for(int i=0;i<=no_of_rows;i++) {
SXSSFRow row1 = sheet.createRow(i);
SXSSFCell r1c1 = row1.createCell(0);
r1c1.setCellValue("1000.00");
SXSSFCell r1c2 = row1.createCell(1);
r1c2.setCellValue("2000.00");
}
DataValidationConstraint constraint1 = sheet.getDataValidationHelper().createExplicitListConstraint(optionsArray);
CellRangeAddressList addressList1 = new CellRangeAddressList(0, no_of_rows, 0, 0);
DataValidation dataValidation1 = sheet.getDataValidationHelper().createValidation(constraint1, addressList1);
sheet.addValidationData(dataValidation1);
DataValidationConstraint constraint2 = sheet.getDataValidationHelper().createExplicitListConstraint(optionsArray);
CellRangeAddressList addressList2 = new CellRangeAddressList(0, no_of_rows, 1, 1);
DataValidation dataValidation2 = sheet.getDataValidationHelper().createValidation(constraint2, addressList2);
sheet.addValidationData(dataValidation2);
FileOutputStream fos = new FileOutputStream(new File("testout.xlsx"));
workbook.write(fos);
workbook.close();
fos.close();
}
}
当不同唯一数据验证的计数超过限制时,不可能对每个单电池进行不同的唯一数据验证。此限制是
Excel
限制,而不是apache poi
限制。Excel specifications and limits中没有记录此数据验证的特殊限制。但这可能与唯一单元格格式/单元格样式的限制(64,000)或工作表中超链接的限制(66,530)有关。
关于java - 在工作表中添加大量ValidationData时,POI写入损坏的.xlsx,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56865788/