Apache POI处理Miscrosoft Office 各种文件格式的开源项目

介绍:

应用场景

maven 坐标

复制代码
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.16</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.16</version>
            </dependency>

方法及其作用

创建/写入

复制代码
//创建Excel文件 并且写入文件内容(在内存中创建)
XSSFWorkbook excel = new XSSFWorkbook();

用XSSFWorkbook()创建的excel表默认没有sheet页 需要手动创建

复制代码
XSSFSheet sheet = excel.createSheet();//创建一个sheet页
复制代码
复制代码
        XSSFWorkbook excel = new XSSFWorkbook();//新建excel表
        XSSFSheet sheet = excel.createSheet("info");//创建一个sheet页
                                            //输入sheet名字
        XSSFRow row = sheet.createRow(1);//创建第二行(从0开始)
        row.createCell(1).setCellValue("姓名");//创建单元格也是从0开始 这里表示创建第二个单元格
                                      //在单元格中写入内容
        row.createCell(2).setCellValue("年龄");

        XSSFRow row1 = sheet.createRow(2);
        row1.createCell(1).setCellValue("小李");
        row1.createCell(2).setCellValue("18");

通过excel在磁盘创建

复制代码
        //通过输出流将excel写入磁盘
        FileOutputStream info = new FileOutputStream(new File("D://info.xlsx"));
        excel.write(info);

        info.close();
        excel.close();//关闭资源

执行之后

读取

复制代码
        //通过输入流获取表格
        FileInputStream in = new FileInputStream(new File("D://info.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(in);//接收表格
        XSSFSheet sheet = excel.getSheetAt(0);//通过索引获取sheet页
        //XSSFSheet sheet1 = excel.getSheet("sheet");//通过名字获取sheet页
        int lastRowNum = sheet.getLastRowNum();//获取表中有内容的 最后一行的行号
        for (int i = 1; i <= lastRowNum; i++) {
            //已知有内容的最后一行 循环读取sheet页中内容
            XSSFRow row = sheet.getRow(i);
            XSSFCell cell = row.getCell(1);
            XSSFCell cell1 = row.getCell(2);
            System.out.println(cell + " " + cell1);
        }
        in.close();
        excel.close();

执行结果 记得关流

相关推荐
SuperherRo19 小时前
服务攻防-中间件安全&Apache&Tomcat&Jetty&Weblogic&AJP协议&反序列化&CVE漏洞
中间件·tomcat·apache·jetty·weblogic
回忆2012初秋1 天前
时序库.net平台下的推荐 SonnetDB,一文分析清除他与Apache IoTDB的区同
apache·iotdb
家有娇妻张兔兔2 天前
Apache POI 导出 Word 踩坑实录:Word 分栏为什么做不好左右平铺
c#·word·apache·poi·分栏
HashData酷克数据2 天前
官宣:Apache Cloudberry (Incubating) 2.1.0 正式发布!
apache
weixin_394758032 天前
直播间小程序码生成问题修复代码清单
android·小程序·apache
YaBingSec3 天前
玄机靶场—Apache-druid(CVE-2021-25646) WP
java·开发语言·笔记·安全·php·apache
回忆2012初秋5 天前
.NET 时序数据操作实战:Apache IoTDB连接与 CRUD 完全指南
.net·apache·iotdb
weixin_430750935 天前
部署FreeRadius+php+apache+mariaDB+daloradius 实现认证计费功能
php·apache·mariadb·daloradius·freeradius
还在忙碌的吴小二6 天前
Apache HertzBeat 安装使用完整指南
apache
web前端神器6 天前
记录uniapp小程序的报错
小程序·uni-app·apache