Java工具类:将Excel文件转成字符串数组

pom.xml

<!--POI Start-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-excelant</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>com.github.virtuald</groupId>
    <artifactId>curvesapi</artifactId>
    <version>1.04</version>
</dependency>
<!--POI End-->

ExcelUtils.java

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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.ss.usermodel.WorkbookFactory;

import java.io.*;
import java.util.Iterator;

/**
 * Excel工具类
 * @author jiabochao@live.com
 * @version 2018-8-9 20:26:30
 */
public class ExcelUtils {

    /**
     * 将Excel文件转成字符串数组。支持xls、xlsx
     * @param in Excel文件输入流
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static String[][] copyData(InputStream in) throws IOException, InvalidFormatException {
        Workbook wb = WorkbookFactory.create(in);
        Sheet sheet = wb.getSheetAt(0);
        int rowNumber = sheet.getPhysicalNumberOfRows() - 1;
        Iterator<Row> rowIterator = sheet.rowIterator();
        Row titleRow = rowIterator.next();
        int columnNumber = titleRow.getLastCellNum();
        String[][] table = new String[rowNumber][columnNumber];
        for (int i = 0; i < rowNumber && rowIterator.hasNext(); i++) {
            Row row = rowIterator.next();
            for (int j = 0; j < columnNumber; j++) {
                Cell cell = row.getCell(j);
                if (cell != null) {
                    cell.setCellType(CellType.STRING);
                    table[i][j] = cell.getStringCellValue().trim();
                } else {
                    table[i][j] = "";
                }
            }
        }
        return table;
    }

    /**
     * 将Excel文件转成字符串数组。支持xls、xlsx
     * @param bytes Excel文件字节数组
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static String[][] copyData(byte[] bytes) throws IOException, InvalidFormatException {
        return copyData(new ByteArrayInputStream(bytes));
    }

    /**
     * 将Excel文件转成字符串数组。支持xls、xlsx
     * @param file Excel文件
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static String[][] copyData(File file) throws IOException, InvalidFormatException {
        return copyData(new FileInputStream(file));
    }

}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值