java.lang.IllegalArgumentException:尝试写入已写入磁盘的[0,1]范围内的行[1]

时间:2019-05-31 08:04:42

标签: java apache-poi

在ubuntu中使用apache poi 3.15版下载xlsx时,它却给了我

java.lang.IllegalArgumentException: Attempting to write a row[1] in the range [0,1] that is already written to disk at org.apache.poi.xssf.streaming.SXSSFSheet.createRow(SXSSFSheet.java:133),
at org.apache.poi.xssf.streaming.SXSSFSheet.createRow(SXSSFSheet.java:62)
String fileName = "myDownloads"+".xlsx";
String sourceFolderPath = "/home/user/sampleFile/";
FileInputStream fileInputStream = new 
FileInputStream(sourceFolderPath+"SampleFile.xlsx");
XSSFWorkbook wb_template = new XSSFWorkbook(fileInputStream);
fileInputStream.close();
String destinationFolderPath = "/home/user/downloads";
File dir = new File(destinationFolderPath);
if (!dir.exists()) {
    dir.mkdirs();
}
SXSSFWorkbook workbook = new SXSSFWorkbook(wb_template);
workbook.setCompressTempFiles(true);
SXSSFSheet workSheet = (SXSSFSheet) workbook.getSheetAt(0);
workSheet.setRandomAccessWindowSize(100000);
SXSSFSheet workSheet1 = (SXSSFSheet) workbook.getSheetAt(1);
workSheet1.setRandomAccessWindowSize(100000);
List<Student> studentList = studnetDao.getStudentListByName("kumar");
if(CollectionUtils.isNotEmpty(studentList)) {
    Integer rowIndex = 1;
    for(Student s : studentList) {
        Row row = workSheet.getRow(rowIndex);
        if (row == null) {
            row = workSheet.createRow(rowIndex); 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请看一下这个构造函数参数 enter image description here

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(exportExcelTo));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook, -1, Boolean.FALSE, Boolean.TRUE);

这个可能有帮助 Apache POI getRow() returns null and .createRow fails