excel表格数据读取(2)--POI方式

一、maven依耐

<!-- POI读取excel表格文件 -->
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.14</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>

二、Demo代码

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestPoi {

    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";
    private static Workbook workbook = null;
    private static List<String[]> list = new ArrayList<String[]>();

    public static void main(String[] args) {
        String filePath = "D:\\test.xlsx";
        poiReadExcel(filePath);
        // 遍历获取每一行的内容
        for (int i = 0; i < list.size(); i++) {
            String[] row = (String[]) list.get(i);
            // 遍历获取每一行的每一列的内容
            for (String string : row) {
                System.out.print(string + "\t");
            }
            System.out.println();
        }
    }

    /**
     * @Title: poiReadExcel 
     * @Description: POI读取excel表格(支持xls和xlsx格式)
     * @param filePath
     */
    private static void poiReadExcel(String filePath) {
        try {
            if (filePath.endsWith(EXCEL_XLS)) {
                workbook = new HSSFWorkbook(new FileInputStream(new File(filePath)));
            } else if (filePath.endsWith(EXCEL_XLSX)) {
                workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            }
//          for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {// 循环工作表Sheet
//              if (sheet == null) {
//                  continue;
//              }
//          }
            Sheet sheet = workbook.getSheetAt(0);// 默认读取第一个sheet表
            // 循环所有行
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row xssfRow = sheet.getRow(rowNum);
                if (xssfRow == null) {
                    continue;
                }
                // 创建一个数组 用来存储每一列的值
                String[] str1 = new String[xssfRow.getLastCellNum()];
                // 循环每一行的所有列
                for (int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++) {
                    Cell xssfCell = xssfRow.getCell(cellNum);
                    if (xssfCell == null) {
                        continue;
                    }
                    String value = getValue(xssfCell, workbook);
                    str1[cellNum] = value;
                }
                list.add(str1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getValue(Cell xssfCell, Workbook workbook) {
        String value = "";
        switch (xssfCell.getCellType()) {
        case XSSFCell.CELL_TYPE_NUMERIC: // 数字
            if (DateUtil.isCellDateFormatted(xssfCell)) {
                value = String.valueOf(String.valueOf(xssfCell.getDateCellValue()));
            } else {
                value = String.valueOf(xssfCell.getNumericCellValue());
            }
            break;
        case XSSFCell.CELL_TYPE_STRING: // 字符串
            value = String.valueOf(xssfCell.getStringCellValue());
            break;
        case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean
            value = String.valueOf(xssfCell.getBooleanCellValue());
            break;
        case XSSFCell.CELL_TYPE_FORMULA: // 公式
            value = String.valueOf(xssfCell.getCellFormula());
            break;
        case XSSFCell.CELL_TYPE_BLANK: // 空值
            value = "";
            break;
        case XSSFCell.CELL_TYPE_ERROR: // 故障
            System.out.println("故障");
            break;
        default:
            System.out.print("未知类型   ");
            break;
        }
        return value;
    }
}

猜你喜欢

转载自blog.csdn.net/wgq3773/article/details/81116692
今日推荐