입력 폴더에서 사용할 수있는 2 개의 xlsx 파일을 병합하려고합니다. 두 파일 모두 데이터가 있습니다. 여전히 newRownumber가 값 -1로 할당되고 있으며이 오류가 발생하는 것 같습니다. 파일이 비어 있으면 -1이 할당됩니다. 스레드 "main"의 예외 java.lang.IllegalArgumentException : 허용 범위 (0..1048575)를 벗어난 잘못된 행 번호 (-1) org.apache.poi.xssf.usermodel.XSSFRow.setRowNum (XSSFRow.java:423) org.apache.poi.xssf.usermodel.XSSFSheet.createRow (XSSFSheet.java:788) MergeMultipleXlsFilesInDifferentSheet.copySheets (MergeMultipleXlsFilesInDifferentSheet.java:58) MergeMultipleXlsFilesInDifferentSheet.copySheets (MergeMultipleXlsFilesInDifferentSheet.java:48) 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();
}
}
}
- 답변 # 1
- 답변 # 2
사용하던 최신 버전 대신 Apache poi 3.16을 사용해보십시오. 일부 기능은 업데이트로 수정됩니다.
관련 자료
- c# - XML 예외 처리 중에 예외를 발생시키는 XML 파일의 줄 번호를 어떻게 얻습니까?
- java - 이진 문자열을 정수로 구문 분석 할 때 숫자 형식 예외
- 스레드 "main"scalamatcherror - map ()의 예외 (클래스 orgapachesparksqlcatalystutilcaseinsensitivemap)
- arrays - 스레드"main"의 예외 javalangarrayindexoutofboundsexception - 3
- sql - ora-00909 - 유효하지 않은 수의 인수 00909 직원으로부터 "직원 및 직책"으로 concat (last_name, '', job_id)를 선택하십시오
- phpunit - 잘못된 인수 예외 - 라 라벨 유닛 테스트
- multithreading - C ++ 멀티 스레드 애플리케이션에서 최대 동시 스레드 수를 설정하십시오
- c# - 명령 줄 인수 수 + 예외 처리 방법
- "스레드"main"javalangExceptionInInitializerError"오류 발생
- php - PDO에서 잘못된 매개 변수 번호를 수정하는 방법
- java의 '메인 "메인"javalangnoclassdeffounderror - org/eclipse/swt/swt에서 예외를 수정하는 방법
- java - 스레드 "main"의 예외 - nullpointerexception
- android - 스레드 "main"javalangnoclassdeffounderror의 예외 - javax/xml/bind/annotation/xmlschema
- django 예외 값 - 잘못된 필터 오류 :
- 멀티 스레딩 중 스레드 "main"javautilConcurrentModificationException의 예외
- sql - ora-01481 - to_date의 잘못된 숫자 형식 모델
- php - Yii2 select2 데이터베이스 예외 바인딩 된 변수 수가 토큰 수와 일치하지 않습니다
- swing - 스레드 "awt-eventqueue-0"예외 javalangnumberformatexception - 입력 문자열의 경우 : "달 선택"
- java - 스레드 "main"orgopenqaseleniumelementnotinteractableexception의 예외 - 상호 작용할 수없는 요소
포럼에 오신 것을 환영합니다.
예외가 발생합니다.
newSheet.getLastRowNum();
요구된다newSheet
행이 없습니다. 아마도 당신은 바꾸고 싶어newSheet
...에sheet
여기서 마지막 행 번호는 소스 시트에서 가져옵니다.