여러개의 Excel 워크북을 병합할 때 전화번호가 과학적 표기법으로 표시될 수 있습니다. 만약 문자열 방식으로 처리하고 싶다면 아래의 완전한 코드를 사용하세요.
public static void mergeWorkBook() throws Exception {
long startTime = System.currentTimeMillis(); // 시작 시간 측정
String fromPath = "exceldoc\\cases"; // 엑셀 파일存放 경로
String toPath = "exceldoc/result/"; // 결과 엑셀 파일 저장 경로
String excelName = "합계"; // 새로운 엑셀 파일명
HSSFWorkbook wbCreate = new HSSFWorkbook(); // 새로운 엑셀创建工作북 생성
File file = new File(fromPath);
for (File excel : file.listFiles()) {
String strExcelPath = fromPath + "/" + excel.getName();
InputStream in = new FileInputStream(strExcelPath);
HSSFWorkbook wb = new HSSFWorkbook(in);
String bookName = excel.getName().substring(0, excel.getName().lastIndexOf("."));
int sheetNum = wb.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++) {
HSSFSheet sheet = wb.getSheetAt(i);
String sheetName = sheet.getSheetName();
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
System.out.println("워크북명: " + bookName);
System.out.println("시트명: " + sheetName);
if (lastRow == 0) {
continue;
}
HSSFSheet sheetCreate = wbCreate.createSheet(bookName + "_" + sheetName);
for (int j = firstRow; j <= lastRow; j++) {
HSSFRow rowCreate = sheetCreate.createRow(j);
HSSFRow row = sheet.getRow(j);
if (row == null) {
continue;
}
System.out.println("bookName: " + bookName + "|sheetName: " + sheetName + "|row: " + j);
int lastCell = row.getLastCellNum();
int firstCell = row.getFirstCellNum();
for (int k = firstCell; k < lastCell && lastCell > 0; k++) {
String strVal = "";
if (row.getCell(k) != null) {
switch (row.getCell(k).getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
DecimalFormat df = new DecimalFormat("#");
strVal = df.format(row.getCell(k).getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
strVal = row.getCell(k).getStringCellValue() + "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strVal = row.getCell(k).getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
strVal = row.getCell(k).getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK:
strVal = "";
break;
case HSSFCell.CELL_TYPE_ERROR:
System.out.println(i + j + "CELL_TYPE_ERROR");
break;
default:
System.out.print(i + j + "未知类型 ");
break;
}
rowCreate.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);
rowCreate.getCell(k).setCellValue(strVal + "");
}
}
System.out.println("한 행 처리 완료");
}
System.out.println("여러 행 처리 완료");
}
System.out.println("여러 시트 처리 완료");
}
System.out.println("모든 파일 처리 완료");
System.out.println(toPath + excelName + ".xls");
FileOutputStream fileOut = new FileOutputStream(toPath + excelName + ".xls");
wbCreate.write(fileOut);
fileOut.close();
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(toPath + excelName + ".xls"));
System.out.println(workbook.getNumberOfSheets());
long endTime = System.currentTimeMillis();
System.out.println("프로그램 실행 시간: " + (endTime - startTime) + "ms");
}
특히 원본 파일을 읽을 때 셀 유형을 판별하는 것이 중요합니다.
if (row.getCell(k) != null) {
switch (row.getCell(k).getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
DecimalFormat df = new DecimalFormat("#");
strVal = df.format(row.getCell(k).getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
strVal = row.getCell(k).getStringCellValue() + "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strVal = row.getCell(k).getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
strVal = row.getCell(k).getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK:
strVal = "";
break;
case HSSFCell.CELL_TYPE_ERROR:
System.out.println(i + j + "CELL_TYPE_ERROR");
break;
default:
System.out.print(i + j + "未知类型 ");
break;
}
rowCreate.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);
rowCreate.getCell(k).setCellValue(strVal + "");
}
또한 쓰기 전에 셀 유형을 설정합니다:
rowCreate.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);