合并多个Excel工作簿,会出现电话号码以科学计数法显示,如果想要以字符串方式处理,要按如下完整代码
public static void mergeWorkBook() throws Exception { long startTime = System.currentTimeMillis(); //获取开始时间
String fromPath = "exceldoc\\cases";// excel存放路径
String toPath = "exceldoc/result/";// 保存新EXCEL路径
// 新的excel 文件名
String excelName = "汇总";
// 创建新的excel
HSSFWorkbook wbCreat = new HSSFWorkbook();
File file = new File(fromPath);
for (File excel : file.listFiles()) {
// 打开已有的excel
String strExcelPath = fromPath + "/" + excel.getName();
// input 来源 ,output 目的地
InputStream in = new FileInputStream(strExcelPath);
HSSFWorkbook wb = new HSSFWorkbook(in);
//获取工作簿的名称,不带前缀
String bookName = excel.getName().substring(0,excel.getName().lastIndexOf("."));
// i表示源工作簿 sheet 数量
int sheetnum = wb.getNumberOfSheets();
for (int i = 0; i < sheetnum; i++) {
//开始每个表的处理
HSSFSheet sheet = wb.getSheetAt(i);
// 目标工作簿创建同样的sheet名
String sheetName = sheet.getSheetName(); int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum(); System.out.println("workbook:"+bookName);
System.out.println("sheetName:"+sheetName);
//如果sheet的lastrow 是0 表示这个表没有内容,这张sheet就不用处理,继续处理下张sheet处理,用continue
if(lastRow == 0 ){
continue;
} HSSFSheet sheetCreat = wbCreat.createSheet(bookName +"_"+ sheet.getSheetName()); //开始多行的处理
for (int j = firstRow; j <= lastRow ; j++) {
// 创建新建excel Sheet的行
HSSFRow rowCreat = sheetCreat.createRow(j);
// 取得源有excel Sheet的行
HSSFRow row = sheet.getRow(j);
// 单元格式样
//如果行的内容为null ,说明行为空行,此行就跳过,继续下行的处理,用continue
if(null == row){
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 (null != row.getCell(k)) {
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: // 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;
}
//System.out.println(strVal);
rowCreat.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);;
rowCreat.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"); wbCreat.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 (null != row.getCell(k)) {
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: // 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;
}
//System.out.println(strVal);
rowCreat.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);;
rowCreat.getCell(k).setCellValue(strVal+"");
}
再在写之前进行写单元格处理:
rowCreat.createCell(k).setCellType(HSSFCell.CELL_TYPE_STRING);;