JAVA解析Excel需要的包及代码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013146766/article/details/78367951
需要的包: 
commons-collections4-4.1.jar 
poi-3.17.jar 
poi-ooxml-3.17.jar 
xmlbeans-2.6.0.jar 
poi-ooxml-schemas-3.17.jar

maven只需要写:

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

以下为解析的代码:

package util;

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.junit.Test;

public class ExcelReader {
    //需要解析的是xlsx文件
    public List<List<String>> readXlsx(String path) throws Exception{
        //获取文件输入流
        InputStream is = new FileInputStream(path);
        //获取读取的实例
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook(is);
        //记录Excel结果的变量
        List<List<String>> result = new ArrayList<List<String>>();
        //遍历每一页
        //for(int index=0;index<hssfWorkbook.getNumberOfSheets();index++)
        //HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(index);
        for(int index=0;index<xssfWorkbook.getNumberOfSheets();index++) {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(index);
            if(xssfSheet == null)
                continue;
            //遍历每一行
            for(int row=1;row<=xssfSheet.getLastRowNum();row++) {
                XSSFRow xssfRow = xssfSheet.getRow(row);

                int minCol = xssfRow.getFirstCellNum();
                int maxCol = xssfRow.getLastCellNum();
                List<String> rowList = new ArrayList<String>();
                //遍历该行,获取每个celll元素
                for(int col = minCol;col<maxCol;col++) {
                    XSSFCell cell = xssfRow.getCell(col);
                    if(cell==null) {
                        continue;
                    }
                    rowList.add(cell.toString());
                }
                result.add(rowList);
            }
        }
        xssfWorkbook.close();
        return result;
    }

    //需要解析的是xls文件
    public List<List<String>> readXls(String path) throws Exception{
        InputStream is = new FileInputStream(path);
        HSSFWorkbook hssfWorkbook=new HSSFWorkbook(is);
        List<List<String>> result = new ArrayList<List<String>>();
        for(int index=0;index<hssfWorkbook.getNumberOfSheets();index++) {
            HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(index);
            if(hssfSheet == null)
                continue;
            for(int row=1;row<=hssfSheet.getLastRowNum();row++) {
                HSSFRow hssfRow = hssfSheet.getRow(row);

                int minCol = hssfRow.getFirstCellNum();
                int maxCol = hssfRow.getLastCellNum();
                List<String> rowList = new ArrayList<String>();
                //遍历该行,获取每个celll元素
                for(int col = minCol;col<maxCol;col++) {
                    HSSFCell cell = hssfRow.getCell(col);
                    if(cell==null) {
                        continue;
                    }
                    rowList.add(cell.toString());
                }
                result.add(rowList);
            }
        }
        hssfWorkbook.close();
        return result;
    }

    @Test
    public void test() throws Exception {
        ExcelReader excelReader = new ExcelReader();
        List<List<String>> result = excelReader.readXlsx("D:\\code\\worksp\\oxygen\\onmyoji_card\\src\\util\\test.xlsx");
        System.out.println(result);
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值