apache POI介绍 在JAVA中操作三大办公软件
Apache POI(Poor Obfuscation Implementation,简称 POI)是一个开源的Java库,它提供了对Microsoft Office格式文件的读写支持。Apache POI使得开发者可以在Java应用程序中操作Microsoft Office文档,如Excel电子表格、Word文档、PowerPoint演示文稿等。
Apache POI(Apache POI 是 Apache 项目的一部分,用于处理 Microsoft Office 格式文件)在许多应用场景中非常有用,以下是四个典型的应用场景:
应用场景
报表自动化生成
在企业中,经常需要生成各种报表,如财务报表、销售报表等。使用 Apache POI,可以自动化地从数据库读取数据,并将其写入 Excel 文件中,从而简化报表生成流程,减少人工操作的错误。数据分析和处理
数据分析是另一个常见的应用场景。Apache POI 可以读取包含数据的 Excel 文件,将数据导入到数据分析工具或应用程序中进行进一步分析和处理。此外,分析结果也可以写回 Excel 文件中,方便分享和报告。文档转换服务
在需要将 Word 文档或 Excel 电子表格转换成其他格式(如 PDF)的场景中,Apache POI 可以读取原始文档的内容,并使用其他库(如 Apache PDFBox)生成所需格式的文件。Web 应用程序中的 Office 文件处理
在 Web 应用程序中,用户可能需要上传、编辑或下载 Office 格式的文件。Apache POI 可以在服务器端处理这些文件,无需安装 Microsoft Office。例如,一个在线协作平台可能允许用户共同编辑一个 Excel 电子表格,而所有处理都在服务器端通过 Apache POI 完成。批量文件处理
在需要处理大量 Office 文件的场景中,如批量更新 Excel 电子表格中的公式或格式,Apache POI 可以自动化这一过程,通过编程方式批量读取、修改和保存文件,大大提高效率。教育和培训材料的自动化生成
在教育领域,可能需要根据学生的成绩或其他数据自动生成个性化的报告或反馈。Apache POI 可以用来读取学生数据,生成定制的 Word 文档或 Excel 电子表格,然后分发给学生或教师。
这些场景展示了 Apache POI 在不同领域的应用潜力,从企业自动化到 Web 服务,再到教育,Apache POI 提供了一个强大的工具来处理 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表中写入数据
package com.sky;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
public class TestPOI {
public static void writer() throws Exception{
//在内存中创建一个excel文件
XSSFWorkbook excel = new XSSFWorkbook();
//在excel文件中创建一个sheet页
XSSFSheet sheetTest = excel.createSheet("sheetTest");
//在excel中创建行对象 和数组一样 下表为0
XSSFRow row = sheetTest.createRow(1);
//创建单元格 并且写入文件内容
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("年龄");
row.createCell(3).setCellValue("性别");
//创建一个新行
row = sheetTest.createRow(2);
row.createCell(1).setCellValue("张三");
row.createCell(2).setCellValue("18");
row.createCell(3).setCellValue("男");
//创建一个新行
row = sheetTest.createRow(3);
row.createCell(1).setCellValue("李四");
row.createCell(2).setCellValue("19");
row.createCell(3).setCellValue("女");
//创建一个新行
row = sheetTest.createRow(4);
row.createCell(1).setCellValue("王五");
row.createCell(2).setCellValue("17");
row.createCell(3).setCellValue("男");
//通过输出流将内存中的excel文件写如到磁盘
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Test.xls");
excel.write(fileOutputStream);
//关闭资源
fileOutputStream.close();
excel.close();
}
public static void main(String[] args) throws Exception {
writer();
}
}
- 读取excel文件数据
package com.sky;
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 TestPOI {
/**
* 通过POI读取excel文件
* @throws Exception
*/
public static void read() throws Exception{
//读取地盘上已经存在的excel文件
XSSFWorkbook excel = new XSSFWorkbook(new File("D:\\Test.xls"));
//读取excel文件中的第一个Sheet页
XSSFSheet sheetAt = excel.getSheetAt(0);
//获取sheet中最后一行的行号
int lastRowNum = sheetAt.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
//获得某一行
XSSFRow row = sheetAt.getRow(i);
//获得单元格元素
String stringCellValue = row.getCell(1).getStringCellValue();
String stringCellValue1 = row.getCell(2).getStringCellValue();
String stringCellValue2 = row.getCell(3).getStringCellValue();
System.out.println(stringCellValue+" "+stringCellValue1+" "+stringCellValue2);
}
//关闭资源
excel.close();
}
public static void main(String[] args) throws Exception {
read();
}
}