快速掌握并使用Apache POI

Apache POI

Apache POl是一个处理Miscrosoft Ofice各种文件格式的开源项目。简单来说就是,我们可以使用 POI在 Java 程序中对Miscrosoft Office各种文件进行读写操作。


简单使用

快速入门

第一步:导入Apache POI的maven坐标:

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

如何使用POI操作Excel文件

我们先在一个目录下创建一个测试的test.xlsx文件
通过POI将数据写入到文件中

java 复制代码
@SpringBootTest
//使用POI操作Excel文件
public class POITest {
    /**
     * 通过POI创建excel文件并写入内容
     */
    public static void write() throws Exception{
        //在内存中创建一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel文件中创建一个Sheet页
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet页中创建行,参数行号下表是从0开始的
        XSSFRow row = sheet.createRow(1);
        //在指定行上创建单元格
        XSSFCell cell = row.createCell(1);
        //向单元格中写入内容
        cell.setCellValue("帅");
        //向第三个单元格写入数据
        row.createCell(2).setCellValue("真的帅");
        //通过输出流将内容写入到文件中,创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\test.xlsx"));
        //通过Excel文件对象将输出流写入到文件中
        excel.write(fileOutputStream);
        //关闭资源
        fileOutputStream.close();
        excel.close(); 
    }
    public static void main(String[] args) throws Exception{
        write();
    }
}

这里创建Excel文件对象是在内存中创建的文件,最后结合输出流对象将文件写入到Excel文件当中

根据POI从文件中读取到内容

java 复制代码
    public static void read() throws Exception{
        //创建输入流,读取磁盘上已经存在的Excel文件
        FileInputStream fileInputStream = new FileInputStream(new File("F:\\test.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
        //读取Excel文件中的第一个sheet页
        XSSFSheet sheet = excel.getSheet("info");
        //读取sheet页中的内容,首先应该得到有文字的最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        for (int i=1;i<=lastRowNum;i++)
        {
            XSSFRow row = sheet.getRow(i);
            String stringCellValue = row.getCell(1).getStringCellValue();
            String stringCellValue1 = row.getCell(2).getStringCellValue();
            System.out.println(stringCellValue+":"+stringCellValue1);
        }
        //关闭资源
        excel.close();
        fileInputStream.close();
    }

从文件中读就不能在内存中操作,需要将磁盘上的文件通过输入流的方式加载到Excel对象当中

相关推荐
零星_AagT16 小时前
Apache-CC6链审计笔记
java·笔记·apache·代码审计
花千树-0102 天前
Java中的自然语言处理(NLP)工具:Stanford NLP、Apache OpenNLP、DL4J
java·自然语言处理·nlp·aigc·apache
Bai_Yin2 天前
Debezium 与 Apache Kafka 的集成方式
分布式·kafka·apache·debezium
xing.yu.CTF2 天前
Web入侵实战分析-常见web攻击类应急处置实验2
运维·服务器·windows·web安全·apache·php漏洞·phpstudy后门漏洞
10km2 天前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
Apache IoTDB4 天前
Apache IoTDB v2.0.1-beta 发布|树、表双模型支持,更灵活更全面!
apache·iotdb
垚垚 Securify 前沿站4 天前
Apache Logic4j 库反序列化漏洞复现与深度剖析
linux·网络·安全·web安全·系统安全·apache
undo_try4 天前
大数据组件(四)快速入门实时数据湖存储系统Apache Paimon(1)
大数据·flink·apache
花千树-0106 天前
使用 Apache PDFBox 提取 PDF 中的文本和图像
java·pdf·apache·ai编程
百事可乐☆6 天前
uniapp 支付宝小程序自定义导航栏
小程序·uni-app·apache