1. 程式人生 > >java完美解析excel

java完美解析excel

java 程式碼完美解析excel表格,2007與2003版。2003及以上版本均可使用2007版 package com.elensdata;

import org.apache.poi.hssf.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File; import java.io.FileInputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; public class ExcelUtil { //預設單元格內容為數字時格式 private static DecimalFormat df = new DecimalFormat(“0”); // 預設單元格格式化日期字串 private static SimpleDateFormat sdf = new SimpleDateFormat( “yyyy-MM-dd HH:mm:ss”); // 格式化數字 private static DecimalFormat nf = new DecimalFormat(“0.00”); public static ArrayList<ArrayList> readExcel(File file){ if(file == null){ return null; } if(file.getName().endsWith(“xlsx”)){ //處理ecxel2007 return readExcel2007(file); }else{ //處理ecxel2003 return readExcel2003(file); } } /* * @return 將返回結果儲存在ArrayList內,儲存結構與二位陣列類似 * lists.get(0).get(0)表示過去Excel中0行0列單元格 */ public static ArrayList<ArrayList> readExcel2003(File file){ try{ ArrayList<ArrayList> rowList = new ArrayList<ArrayList>(); ArrayList colList; HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row; HSSFCell cell; Object value; for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){ row = sheet.getRow(i); colList = new ArrayList(); if(row == null){ //當讀取行為空時 if(i != sheet.getPhysicalNumberOfRows()){//判斷是否是最後一行 rowList.add(colList); } continue; }else{ rowCount++; } for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){ cell = row.getCell(j); if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){ //當該單元格為空 if(j != row.getLastCellNum()){//判斷是否是該行中最後一個單元格 colList.add(""); } continue; } switch(cell.getCellType()){ case XSSFCell.CELL_TYPE_STRING: // System.out.println(i + “行” + j + " 列 is String type"); value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if (“General”.equals(cell.getCellStyle() .getDataFormatString())) { value = nf.format(cell.getNumericCellValue()); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } // System.out.println(i + “行” + j // + " 列 is Number type ; DateFormt:" // + value.toString()); break; case XSSFCell.CELL_TYPE_BOOLEAN: // System.out.println(i + “行” + j + " 列 is Boolean type"); value = Boolean.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: // System.out.println(i + “行” + j + " 列 is Blank type"); value = “”; break; default: // System.out.println(i + “行” + j + " 列 is default type"); value = cell.toString(); }// end switch colList.add(value); }//end for j rowList.add(colList); }//end for i

        return rowList;
    }catch(Exception e){
        return null;
    }
}

public static ArrayList<ArrayList<Object>> readExcel2007(File file){
    try{
        ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
        ArrayList<Object> colList;
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        Object value;
        for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){
            row = sheet.getRow(i);
            colList = new ArrayList<Object>();
            if(row == null){
                //當讀取行為空時
                if(i != sheet.getPhysicalNumberOfRows()){//判斷是否是最後一行
                    rowList.add(colList);
                }
                continue;
            }else{
                rowCount++;
            }
            for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){
                cell = row.getCell(j);
                if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){
                    //當該單元格為空
                    if(j != row.getLastCellNum()){//判斷是否是該行中最後一個單元格
                        colList.add("");
                    }
                    continue;
                }
                switch(cell.getCellType()){
                    case XSSFCell.CELL_TYPE_STRING:

// System.out.println(i + “行” + j + " 列 is String type"); value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if (“General”.equals(cell.getCellStyle() .getDataFormatString())) { value = nf.format(cell.getNumericCellValue()); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } // System.out.println(i + “行” + j // + " 列 is Number type ; DateFormt:" // + value.toString()); break; case XSSFCell.CELL_TYPE_BOOLEAN: // System.out.println(i + “行” + j + " 列 is Boolean type"); value = Boolean.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: // System.out.println(i + “行” + j + " 列 is Blank type"); value = “”; break; default: // System.out.println(i + “行” + j + " 列 is default type"); value = cell.toString(); }// end switch colList.add(value); }//end for j rowList.add(colList); }//end for i

        return rowList;
    }catch(Exception e){
        System.out.println("exception");
        return null;
    }
}


public static DecimalFormat getDf() {
    return df;
}
public static void setDf(DecimalFormat df) {
    ExcelUtil.df = df;
}
public static SimpleDateFormat getSdf() {
    return sdf;
}
public static void setSdf(SimpleDateFormat sdf) {
    ExcelUtil.sdf = sdf;
}
public static DecimalFormat getNf() {
    return nf;
}
public static void setNf(DecimalFormat nf) {
    ExcelUtil.nf = nf;
}

}`