ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java学习之poi

2021-10-02 15:02:10  阅读:192  来源: 互联网

标签:Cell java row1 创建 单元格 学习 poi sheet


遇到的问题

java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils

需要添加commons-math3依赖

 

对07版的excel进行处理时

java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

遇到过上面这个错误,无法排除,可能是英文依赖导入太乱七八糟了

重新开一个新的项目只导入以下依赖

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

程序正常执行没有出现任何错误,原因就是poi的版本和poi-ooxml版本不一致导致了诸多错误,poi和poi-ooxm的版本要一致!!!!!poi和poi-ooxm的版本要一致!!!!!poi和poi-ooxm的版本要一致!!!!!

要使用的依赖如下

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

 

 HSSFWorkbook

HSSFWorkbook用于处理03版的excel

创建03版的excel

 //创建03版的excel
    @Test
    public void a03() throws IOException {
        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("学习人数统计");
        //创建行,行数从0开始
        Row row1 = sheet.createRow(0);
        //创建单元格,单元格也是从0开始
        Cell cell = row1.createCell(0);
        //设置内容
        cell.setCellValue("学习人数");
        //创建第二个单元格
        Cell cell1 = row1.createCell(1);
        cell1.setCellValue(888);

        //创建第二行
        Row row2 = sheet.createRow(1);
        //创建第二行的第一个单元格
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("回复人数");
        //创建第二个单元格
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(555);

        //创建文件
        FileOutputStream  fo = new FileOutputStream("B:\\maven项目\\dianmingqi\\poi.xls");
        //输出
        workbook.write(fo);
        //关闭流
        fo.close();
        System.out.println("完毕");
    }

读取03版的excel

//读取03版的excel
    @Test
    public void r03() throws IOException {
        //创建读的文件流
        FileInputStream fi = new FileInputStream("B:\\maven项目\\dianmingqi\\poi.xls");

        //创建一个工作簿,使用excel能操作的使用workbook对象都能进行操作
        Workbook workbook = new HSSFWorkbook(fi);
        //得到表,表的下标从0开始
        Sheet sheet = workbook.getSheetAt(0);
        //得到行
        Row row1 = sheet.getRow(0);
        //得到第一个单元格
        Cell cell1 = row1.getCell(0);
        //得到列的数据,数据类型要对应
        String str = cell1.getStringCellValue();
        System.out.println(str);
        //关闭流
        fi.close();
    }

XSSFWorkbook

XSSFWorkbook用于处理07版的excel

创建07版的excel

@Test
    public  void a07() throws IOException {
        //创建工作簿
        Workbook wk = new XSSFWorkbook();
        //创建表
        Sheet sheet = wk.createSheet("poi07");
        //创建第一行
        Row row1 = sheet.createRow(0);
        //创建第一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("班级名称:");
        //创建第二个单元格
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("计8");
        //创建第二行
        Row row2 = sheet.createRow(1);
        //创建第一个单元格
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("班级人数");
        //创建第二个单元格
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue("56");

        //创建文件
        FileOutputStream fo = new FileOutputStream("B:\\maven项目\\dianmingqi\\poi07.xlsx");
        wk.write(fo);
        wk.close();
        System.out.println("完成");
    }

读取07版的excel文件

@Test
    public void r07() throws IOException {
        //流
        FileInputStream fi = new FileInputStream("B:\\maven项目\\dianmingqi\\poi07.xlsx");
        //获得工作簿
        Workbook wk = new XSSFWorkbook(fi);
        //获得工作表
        Sheet sheet = wk.getSheetAt(0);
        //获得行
        Row row1 = sheet.getRow(0);
        //获得第一个单元格
        Cell cell1 = row1.getCell(0);
        String str = cell1.getStringCellValue();
        System.out.println(str);
        fi.close();

    }

 

标签:Cell,java,row1,创建,单元格,学习,poi,sheet
来源: https://www.cnblogs.com/lizifashe/p/15359525.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有