当前位置:首页 > 编程笔记 > 正文
已解决

Apache POI

来自网友在路上 152852提问 提问时间:2023-11-06 06:10:58阅读次数: 52

最佳答案 问答题库528位专家为你答疑解惑

文章目录

  • 一、Apache POI介绍
  • 二、应用场景
  • 三、使用步骤
    • 1.导入maven坐标
    • 2.写入代码讲解
    • 3.读取代码讲解


一、Apache POI介绍

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

一般情况下,POI 都是用于操作 Excel 文件。
在这里插入图片描述

二、应用场景

Apache POI 的应用场景:

  • 银行网银系统导出交易明细
    在这里插入图片描述

  • 各种业务系统导出Excel报表
    在这里插入图片描述

  • 批量导入业务数据
    在这里插入图片描述

三、使用步骤

1.导入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>

2.写入代码讲解

在内存中创建一个Excel文件:

XSSFWorkbook excel=new XSSFWorkbook();

在Excel文件中创建一个Sheet页:

XSSFSheet sheet=excel.createSheet("info");

在Sheet中创建行对象,rownum编号从0开始:

XSSFRow row=sheet.createRow(1);

创建单元格并且写入文件内容:

row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("城市");

//创建一个新行:

 row=sheet.createRow(2);row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("北京");

通过输出流将内存中的Excel文件写入到磁盘:

FileOutputStream out=new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(out);

关闭资源:

out.close();
excel.close();

全部代码如下:

package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileOutputStream;public class POITest {/*** 通过POI创建Excel文件并且写入文件内容*/public static void write() throws Exception{//在内存中创建一个Excel文件XSSFWorkbook excel=new XSSFWorkbook();//在Excel文件中创建一个Sheet页XSSFSheet sheet=excel.createSheet("info");//在Sheet中创建行对象,rownum编号从0开始XSSFRow row=sheet.createRow(1);//创建单元格并且写入文件内容row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("城市");//创建一个新行row=sheet.createRow(2);row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("北京");//创建一个新行row=sheet.createRow(3);row.createCell(1).setCellValue("李四");row.createCell(2).setCellValue("南京");//通过输出流将内存中的Excel文件写入到磁盘FileOutputStream out=new FileOutputStream(new File("D:\\info.xlsx"));excel.write(out);//关闭资源out.close();excel.close();}public static void main(String[] args) throws Exception{write();}}

效果如下:
在这里插入图片描述

3.读取代码讲解

创建文件读取流:

 InputStream in =new FileInputStream(("D:\\info.xlsx"));

读取磁盘上已经存在的Excel文件:

XSSFWorkbook excel=new XSSFWorkbook(in);

读取Excel文件中的第一个Sheet页:

XSSFSheet sheet=excel.getSheetAt(0);

获取Sheet中最后一行的行号:

int lastRowNum = sheet.getLastRowNum();

输出:

for(int i = 1; i <= lastRowNum ; i++){//获得某一行XSSFRow row = sheet.getRow(i);//获得单元格对象String cellValue1 = row.getCell(1).getStringCellValue();String cellValue2 = row.getCell(2).getStringCellValue();System.out.println(cellValue1+"  "+cellValue2);}

关闭资源:

in.close();
excel.close();

完整代码如下:

package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;public class POITest {/*** 通过POI创建Excel文件并且写入文件内容*/public static void write() throws Exception{//在内存中创建一个Excel文件XSSFWorkbook excel=new XSSFWorkbook();//在Excel文件中创建一个Sheet页XSSFSheet sheet=excel.createSheet("info");//在Sheet中创建行对象,rownum编号从0开始XSSFRow row=sheet.createRow(1);//创建单元格并且写入文件内容row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("城市");//创建一个新行row=sheet.createRow(2);row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("北京");//创建一个新行row=sheet.createRow(3);row.createCell(1).setCellValue("李四");row.createCell(2).setCellValue("南京");//通过输出流将内存中的Excel文件写入到磁盘FileOutputStream out=new FileOutputStream(new File("D:\\info.xlsx"));excel.write(out);//关闭资源out.close();excel.close();}/*** 通过POI读取Excel文件中的内容* @throws Exception*/public static void read() throws  Exception{//创建文件读取流InputStream in =new FileInputStream(("D:\\info.xlsx"));//读取磁盘上已经存在的Excel文件XSSFWorkbook excel=new XSSFWorkbook(in);//读取Excel文件中的第一个Sheet页XSSFSheet sheet=excel.getSheetAt(0);//获取Sheet中最后一行的行号int lastRowNum = sheet.getLastRowNum();for(int i = 1; i <= lastRowNum ; i++){//获得某一行XSSFRow row = sheet.getRow(i);//获得单元格对象String cellValue1 = row.getCell(1).getStringCellValue();String cellValue2 = row.getCell(2).getStringCellValue();System.out.println(cellValue1+"  "+cellValue2);}//关闭资源in.close();excel.close();}public static void main(String[] args) throws Exception{//write();read();}}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Apache POI":http://eshow365.cn/6-33392-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!