Java入門開發POI讀取導入Excel文件


Apache POI是Apache開發的開源的跨平台的 Java API,提供API給Java程序對Microsoft Office格式檔案進行各種操作。

POI中Excel操作很簡單,主要類有

  • HSSFWorkbook:Excel文件
  • HSSFSheet:Excel文件內的分頁sheet
  • HSSHRow:行
  • HSSFCell:單元格

我們想導入讀取並驗證單元格的數據,如下:

excel內容:

 

開發實例:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts.upload.FormFile;

public class POIImport {

    // 導入並驗證文件File_Import.xls

    public static void main(String[] args) {

        // 用Struts導入文件:
        // FormFile formFile = batchChangeAGForm.getXlsfile();
        //        
        // if (0 == formFile.getFileSize()) {
        // this.setPromptMessage(request, "選擇的文件有誤!");
        // return mapping.findForward("batchChangeAGImport");
        // }
        // InputStream is = formFile.getInputStream();

        // 直接讀取文件:
        String filePath = "D:\\File_Import.xls";
        File file = new File(filePath);
        InputStream is;
        HSSFSheet sheetMain;

        try {
            is = new FileInputStream(file);
            POIFSFileSystem fs = new POIFSFileSystem(is);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            // 讀取第一個Sheet
            sheetMain = wb.getSheetAt(0);
            is.close();

            // 總共的行數
            int rowLens = sheetMain.getLastRowNum();
            int colLens = 8;
            int errCnt = 0;
            HSSFRow row = null;
            HSSFCell cell = null;
            String content = "";

            for (int rowCount = 1; rowCount <= rowLens; rowCount++) {
                System.out.println("讀取行:" + rowCount);
                row = sheetMain.getRow(rowCount);
                if (row != null) {

                    for (int colCount = 0; colCount < colLens; colCount++) {
                        System.out.print("行 :" + rowCount + ";列 :" + colCount
                                + "的內容:");
                        cell = row.getCell((short) colCount);
                        content = getCellValue(cell).trim();
                        if (content == "") {
                            System.out.println("### 發現空異常 ###");
                        } else {
                            System.out.println(content);
                        }
                    }
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getCellValue(HSSFCell cell) {
        if (cell != null) {
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_BLANK:
                return "";
            case HSSFCell.CELL_TYPE_NUMERIC:
                String strValue = String.valueOf(cell.getNumericCellValue());
                if (strValue != null && strValue.indexOf(".") != -1
                        && strValue.indexOf("E") != -1) {
                    try {
                        return new DecimalFormat().parse(strValue).toString();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (strValue.endsWith(".0")) {
                        return strValue.substring(0, strValue.indexOf(".0"));
                    } else {
                        return strValue;
                    }
                }
            case HSSFCell.CELL_TYPE_STRING:
                return (cell.getStringCellValue() + "").trim();
            case HSSFCell.CELL_TYPE_FORMULA:
                return (cell.getCellFormula() + "").trim();
            case HSSFCell.CELL_TYPE_BOOLEAN:
                return cell.getBooleanCellValue() + "";
            case HSSFCell.CELL_TYPE_ERROR:
                return cell.getErrorCellValue() + "";
            }
        }
        return "";
    }
}

 

輸出:

讀取行:1
行 :1;列 :0的內容:A000079
行 :1;列 :1的內容:000002017106088
行 :1;列 :2的內容:2
行 :1;列 :3的內容:2
行 :1;列 :4的內容:10000
行 :1;列 :5的內容:1000
行 :1;列 :6的內容:20171020
行 :1;列 :7的內容:已發放
讀取行:2
行 :2;列 :0的內容:A000080
行 :2;列 :1的內容:000002018107088
行 :2;列 :2的內容:1
行 :2;列 :3的內容:1
行 :2;列 :4的內容:20000
行 :2;列 :5的內容:2000
行 :2;列 :6的內容:20181020
行 :2;列 :7的內容:待發
讀取行:3
行 :3;列 :0的內容:A000081
行 :3;列 :1的內容:000002018107099
行 :3;列 :2的內容:1
行 :3;列 :3的內容:1
行 :3;列 :4的內容:### 發現空異常 ###
行 :3;列 :5的內容:3000
行 :3;列 :6的內容:20181020
行 :3;列 :7的內容:待發

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM