Java通过poi读取excel中文件

Posted 振宇要低调

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java通过poi读取excel中文件相关的知识,希望对你有一定的参考价值。

maven依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.12</version>
        </dependency>

 

代码示例

package com.**.**.**.common.utils;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @author zyydd
 * @date 2019/3/15 15:00
 */
public class ExcelUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);

    public static void main(String[] args) {
        String[][] rowArray = getArrayFromXLSX("D://test.xlsx", 0);
        for (String[] row : rowArray) {
            for (String cell : row) {
                System.out.print(cell + "\\t");
            }
            System.out.println();
        }
    }

    /**
     * 获取xlsx文件内容某个sheet(从0开始)的内容,以二维数组形式返回
     *
     * @param fileAbsolutePath xlsx文件的绝对路径
     * @return xlsx文件的文本内容
     */
    public static String[][] getArrayFromXLSX(String fileAbsolutePath, int bookIndex) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(fileAbsolutePath));
            XSSFWorkbook book = new XSSFWorkbook(inputStream);
            if (bookIndex >= book.getNumberOfSheets()) {
                LOGGER.error("getArrayFromXLSX error: bookIndex={} is to large! ", bookIndex);
                return null;
            }
            XSSFSheet sheet = book.getSheetAt(bookIndex);
            int rowNum = sheet.getLastRowNum() + 1;
            int coloumNum = sheet.getRow(0).getPhysicalNumberOfCells();
            String[][] contents = new String[rowNum][coloumNum];
            for (int j = 0; j < rowNum; j++) {
                XSSFRow row = sheet.getRow(j);
                if (row != null) {
                    for (int k = 0; k < row.getLastCellNum(); k++) {
                        contents[j][k] = getXCellFormatValue(row.getCell(k));
                    }
                }
            }
            return contents;
        } catch (FileNotFoundException fe) {
            LOGGER.error("getArrayFromXLSX error: FileNotFoundException", fe);
        } catch (IOException ie) {
            LOGGER.error("getArrayFromXLSX error: IOException", ie);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    LOGGER.error("getArrayFromXLSX close Exception", e);
                }
            }
        }
        return null;
    }

    private static String getXCellFormatValue(XSSFCell cell) {
        String cellValue = "";
        if (null != cell) {
            switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_STRING:
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                case XSSFCell.CELL_TYPE_NUMERIC:
                    cellValue = (new Double(cell.getNumericCellValue())).intValue() + "";
                    break;
                default:
                    cellValue = " ";
            }
        } else {
            cellValue = "";
        }
        return cellValue;
    }

}

 

excel数据(test.xlsx)

 

 

执行结果

 

以上是关于Java通过poi读取excel中文件的主要内容,如果未能解决你的问题,请参考以下文章

java通过apache poi框架读取2007版Excel文件

java使用poi读取excel时,电话号码变成了科学计数法,整数变成double,怎么改过来

Java实现通过POI读取Excel文件提取手机号码并去重

JAVA:通过poi读取excel

Java-poi-excel-对空值单元格的读取

java poi 读取excel 数字类型