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的内容:待发
原文地址:https://www.cnblogs.com/starcrm/p/5889198.html