码迷,mamicode.com
首页 > 编程语言 > 详细

Java通过poi读取excel中文件

时间:2019-11-04 13:16:10      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:返回   cells   intval   path   文件的   cep   number   enc   efault   

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中文件

标签:返回   cells   intval   path   文件的   cep   number   enc   efault   

原文地址:https://www.cnblogs.com/zhenyuyaodidiao/p/11791386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!