[英]Exception in thread “main” java.lang.IllegalArgumentException: Invalid row number (-1) outside allowable range (0..1048575)
我正在嘗試合並輸入文件夾中可用的 2 個 xlsx 文件。 我的兩個文件都有數據。 仍然 newRownumber 被賦值為-1,我猜會出現這個錯誤。 當文件為空時,將分配 -1。 Exception in thread "main" java.lang.IllegalArgumentException: Invalid row number (-1) outside allowable range (0..1048575) at org.apache.poi.xssf.usermodel.XSSFRow.setRowNum(XSSFRow.java:423) at org.apache.poi.xssf.usermodel.XSSFSheet.createRow(XSSFSheet.java:788) at MergeMultipleXlsFilesInDifferentSheet.copySheets(MergeMultipleXlsFilesInDifferentSheet.java:58) at MergeMultipleXlsFilesInDifferentSheet.copySheets(MergeMultipleXlsFilesInDifferentSheet.java:48) at MergeMultipleXlsFilesInDifferentSheet.mergeExcelFiles(MergeMultipleXlsFilesInDifferentSheet.java :31) 在 MergeMultipleXlsFilesInDifferentSheet.main(MergeMultipleXlsFilesInDifferentSheet.java:122)
請讓我知道下面有什么問題是我的代碼
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class MergeMultipleXlsFilesInDifferentSheet{
public static void mergeExcelFiles(File file) throws IOException {
XSSFWorkbook book = new XSSFWorkbook();
System.out.println(file.getName());
String directoryName = "C:\\input";
File directory = new File(directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
for (File file1 : fList){
if (file1.isFile()){
String ParticularFile = file1.getName();
FileInputStream fin = new FileInputStream(new File(directoryName+"\\"+ParticularFile));
XSSFWorkbook b = new XSSFWorkbook(fin);
for (int i = 0; i < b.getNumberOfSheets(); i++) {
XSSFSheet sheet = book.createSheet(b.getSheetName(i));
copySheets(book, sheet, b.getSheetAt(i));
System.out.println("Copying..");
}
}
try {
writeFile(book, file);
}catch(Exception e) {
e.printStackTrace();
}
}
}
protected static void writeFile(XSSFWorkbook book, File file) throws Exception {
FileOutputStream out = new FileOutputStream(file);
book.write(out);
out.close();
}
private static void copySheets(XSSFWorkbook newWorkbook, XSSFSheet newSheet, XSSFSheet sheet){
copySheets(newWorkbook, newSheet, sheet, true);
}
private static void copySheets(XSSFWorkbook newWorkbook, XSSFSheet newSheet, XSSFSheet sheet, boolean copyStyle){
int newRownumber = newSheet.getLastRowNum();
int maxColumnNum = 0;
Map<Integer, XSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, XSSFCellStyle>() : null;
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
XSSFRow srcRow = sheet.getRow(i);
XSSFRow destRow = newSheet.createRow(i + newRownumber);
if (srcRow != null) {
copyRow(newWorkbook, sheet, newSheet, srcRow, destRow, styleMap);
if (srcRow.getLastCellNum() > maxColumnNum) {
maxColumnNum = srcRow.getLastCellNum();
}
}
}
for (int i = 0; i <= maxColumnNum; i++) {
newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
}
}
public static void copyRow(XSSFWorkbook newWorkbook, XSSFSheet srcSheet, XSSFSheet destSheet, XSSFRow srcRow, XSSFRow destRow, Map<Integer, XSSFCellStyle> styleMap) {
destRow.setHeight(srcRow.getHeight());
for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
XSSFCell oldCell = srcRow.getCell(j);
XSSFCell newCell = destRow.getCell(j);
if (oldCell != null) {
if (newCell == null) {
newCell = destRow.createCell(j);
}
copyCell(newWorkbook, oldCell, newCell, styleMap);
}
}
}
public static void copyCell(XSSFWorkbook newWorkbook, XSSFCell oldCell, XSSFCell newCell, Map<Integer, XSSFCellStyle> styleMap) {
if(styleMap != null) {
int stHashCode = oldCell.getCellStyle().hashCode();
XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
if(newCellStyle == null){
newCellStyle = newWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
styleMap.put(stHashCode, newCellStyle);
}
newCell.setCellStyle(newCellStyle);
}
switch(oldCell.getCellType()) {
case STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
case NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case BLANK:
//return null;
newCell.setCellType(null);
break;
case BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
default:
break;
}
}
public static void main(String[] args) {
try {
mergeExcelFiles(new File("C:\\DataWillBeMerged.xlsx"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
歡迎來到論壇。
你得到了例外,因為newSheet.getLastRowNum();
為沒有行的newSheet
調用。 也許您想在此處將newSheet
更改為sheet
,以便從源工作表中獲取最后一個行號。
嘗試使用 apache poi 3.16 而不是您使用的最新版本。 一些功能通過更新進行了修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.