Apache POI

POI介绍

复制代码
Apache POI是用Java编写的免费开源的跨平台的Java API,
Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,
其中使用最多的就是使用POI操作Excel文件。
XML 复制代码
maven坐标:
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.14</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.14</version>
</dependency>

POI结构:

XML 复制代码
HSSF - 提供读写Microsoft Excel XLS格式档案的功能
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能
HWPF - 提供读写Microsoft Word DOC格式档案的功能
HSLF - 提供读写Microsoft PowerPoint格式档案的功能
HDGF - 提供读Microsoft Visio格式档案的功能
HPBF - 提供读Microsoft Publisher格式档案的功能
HSMF - 提供读Microsoft Outlook格式档案的功能

入门案例

从Excel文件读取数据

使用POI可以从一个已经存在的Excel文件中读取数据

java 复制代码
public class POITest {
    //使用POI读取Excel中的数据
    @Test
    public void test1() throws Exception {
        //加载指定文件,创建一个Excel(工作簿)
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\poitest.xlsx")));
        //读取Excel文件中第一个sheet标签项
        XSSFSheet sheet = excel.getSheetAt(0);
        //遍历sheet标签项,获取每一行数据
        for (Row row : sheet) {
            //遍历行,获取每个单元对象
            for (Cell cell : row) {
                System.out.println(cell.getStringCellValue());
            }
        }
        //关闭资源
        excel.close();
    }

    @Test
    public void test2() throws Exception {
        //加载指定文件,创建一个Excel(工作簿)
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\poitest.xlsx")));
        //读取Excel文件中第一个sheet标签项
        XSSFSheet sheet = excel.getSheetAt(0);
        //获取当前工作表最后一行的行号,行号从0开始
        int lastRowNum = sheet.getLastRowNum();
        for(int i=0;i<=lastRowNum;i++){
            //根据行号获取行对象
            XSSFRow row = sheet.getRow(i);
            //获取当前行的最后一个单元格索引
            short lastCellNum = row.getLastCellNum();
            for(short j=0;j<lastCellNum;j++){
                //根据单元格索引获得单元格对象
                XSSFCell cell = row.getCell(j);
                System.out.println(cell.getStringCellValue());
            }
        }
        //关闭资源
        excel.close();
    }
}
java 复制代码
XSSFWorkbook:工作簿
XSSFSheet:工作表
Row:行
Cell:单元格

向Excel文件写入数据

java 复制代码
public class POITest {
    //使用POI向Excel文件写入数据,并且通过输出流将创建的Excel文件保存到本地磁盘
    @Test
    public void test3() throws Exception{
        //在内存中创建一个Excel文件(工作簿)
        XSSFWorkbook excel = new XSSFWorkbook();
        //创建一个工作表对象
        XSSFSheet sheet = excel.createSheet("POI写入数据");
        //在工作表中创建行对象
        XSSFRow title = sheet.createRow(0);
        //在行中创建单元格对象
        title.createCell(0).setCellValue("姓名");
        title.createCell(1).setCellValue("地址");
        title.createCell(2).setCellValue("年龄");

        XSSFRow dataRow = sheet.createRow(1);
        //在行中创建单元格对象
        dataRow.createCell(0).setCellValue("小明");
        dataRow.createCell(1).setCellValue("广州");
        dataRow.createCell(2).setCellValue("20");

        //创建一个输出流,通过输出流将内存中的Excel文件写入本地磁盘
        FileOutputStream outputStream = new FileOutputStream("C:\\hello1.xlsx");
        excel.write(outputStream);
        outputStream.flush();
        excel.close();
    }
}
相关推荐
智_永无止境10 小时前
Apache Commons Math3 使用指南:强大的Java数学库
apache·math
一只 Lemon19 小时前
jquery 文件上传 (CVE-2018-9207)漏洞复现
apache
喜欢你,还有大家2 天前
Apache服务——搭建实验
apache
一休哥助手2 天前
Apache Thrift:跨语言服务开发的高性能RPC框架指南
网络协议·rpc·apache
Apache IoTDB2 天前
Apache IoTDB V1.3.5 发布|优化加密算法,优化内核稳定性,修复社区反馈问题
apache·iotdb
悠悠~飘2 天前
php学习(第四天)
php·apache
白鹭3 天前
apache实现LAMP+apache(URL重定向)
linux·运维·apache·url重定向·apache实现lamp架构
aramae3 天前
终端之外:解锁Linux命令行的魔法与力量
linux·服务器·apache
lifallen3 天前
Hadoop MapOutputBuffer:Map高性能核心揭秘
java·大数据·数据结构·hadoop·算法·apache
acaad3 天前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel