1. 程式人生 > >POI讀取excel簡單例子

POI讀取excel簡單例子

maven需要的依賴:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version
>
3.9</version> </dependency>
package com.da.test;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel
.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.da.util.PoiUtil; public class ExcelPoi { public static void main(String[] args) { String fileToBeRead = "C:\\aaa.xlsx"; Workbook workbook; try { if (fileToBeRead.indexOf(".xlsx") > -1
) { workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); } else { workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); } // HSSFWorkbook workbook = new HSSFWorkbook(new // FileInputStream(fileToBeRead)); //2003 建立對Excel工作簿檔案的引用 // XSSFWorkbook workbook = new XSSFWorkbook(new // FileInputStream(fileToBeRead)); //2007,2010 建立對Excel工作簿檔案的引用 Sheet sheet = workbook.getSheet("Sheet1"); // 建立對工作表的引用 int rows = sheet.getPhysicalNumberOfRows();// 獲取表格的 int columns = 0; for (int r = 0; r < rows; r++) { // 迴圈遍歷表格的行 if (r == 0) { // 在第一行標題行計算出列寬度,因為資料行中可能會有空值 columns = sheet.getRow(r).getLastCellNum(); continue; } // String value = ""; StringBuilder sb = new StringBuilder(); Row row = sheet.getRow(r); // 獲取單元格中指定的行物件 if (row != null) { // int cells = row.getPhysicalNumberOfCells();// 獲取一行中的單元格數 // int cells = row.getLastCellNum();// 獲取一行中最後單元格的編號(從1開始) for (short c = 0; c < columns; c++) { // 迴圈遍歷行中的單元格 Cell cell = row.getCell((short) c); if (cell != null) { // value += getCellValue(cell) + ","; sb.append(PoiUtil.getCellValue(cell)).append(" "); } } } // String[] str = value.split(","); System.out.println(sb.toString()); } } catch (Exception e) { e.printStackTrace(); } } }

工具類:

package com.da.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;

public class PoiUtil {
    // 得到單元格的字串內容
    public static String getCellValue(Cell cell) {
        DecimalFormat df = new DecimalFormat("#");
        if (cell == null)
            return "";
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(cell.getDateCellValue()).toString();
                // return
                // sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue())).toString();
            }
            return df.format(cell.getNumericCellValue());
        case Cell.CELL_TYPE_STRING:
            // System.out.println(cell.getStringCellValue());
            return cell.getStringCellValue();
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue() + "";
        case Cell.CELL_TYPE_ERROR:
            return cell.getErrorCellValue() + "";
        }
        return "";
    }
}