• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Workbook读取Excel数据

//讀取Excel
/*
*startRow 开始列
*endRow  结束列
* startCell 开始行
* endCell 结束行
 */

    public static Object[] [] readExcel(String  url,int startRow,int endRow,int startCell,int endCell){

        Object[] [] datas = new Object[endRow-startRow+1][endCell-startCell+1];
        //获取Workbook对象
        try {
            File file = new File(url);
            Workbook workBook = WorkbookFactory.create(file);
            //获取sheet 对象
            Sheet sheet =  workBook.getSheet("Sheet1");
            DataFormatter  formatter = new DataFormatter();
            //获取行
            for(int i = startRow;i <= endRow;i++){
                Row row = sheet.getRow(i);
                for(int j = startCell;j <= endCell;j++){
                    Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    String value = formatter.formatCellValue(cell);
                    System.out.print(value+",");
                    datas[i-startRow][j-startCell] = value;
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return datas;
    }

需要jar

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-math3</artifactId>
        <version>3.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.14.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.14.1</version>
    </dependency>

 

posted on 2021-11-27 17:04  yxchun  阅读(1590)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3