使用Apache POI在Java中打开现有Excel工作表

阿帕奇波伊 是一个强大的API,通过它可以读取、写入和修改任何Microsoft文档,如powerpoint、world或excel。 ApachePOI有不同的类和方法来处理不同的MS Office文档。

null
  • POIFS—— 它代表 “文件系统的模糊处理实现较差” .该组件是所有其他POI元素的基本要素。它用于显式读取不同的文件。
  • HSSF 它代表 “可怕的电子表格格式” .用于读取和写入MS Excel文件的xls格式。
  • XSSF 它代表 “XML电子表格格式” .用于MS Excel的xlsx文件格式。
  • HPSF 它代表 “可怕的属性集格式” 。它用于提取MS Office文件的属性集。
  • HWPF 它代表 “可怕的文字处理器格式” .用于读写MS Word的文档扩展文件。
  • XWPF 它代表 “XML字处理器格式” .用于读写MS Word的docx扩展文件。
  • HSLF 它代表 “可怕的幻灯片布局格式” 。它用于阅读、创建和编辑PowerPoint演示文稿。
  • HDGF 它代表 “可怕的图表格式” 。它包含MS Visio二进制文件的类和方法。
  • HPBF 它代表 “可怕的出版商格式” 。它用于读取和写入MS Publisher文件。

在eclipse中用Java打开现有Excel工作表的步骤

  • 创建一个JAVA Maven项目
  • 在pom中添加依赖项。xml文件

XML

< dependency >
< groupId >org.apache.poi</ groupId >
< artifactId >poi</ artifactId >
< version >3.12</ version >
</ dependency >
< dependency >
< groupId >org.apache.poi</ groupId >
< artifactId >poi-ooxml</ artifactId >
< version >3.12</ version >
</ dependency >


  • 在javaResource文件夹中创建一个类

JAVA

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class GFG {
public static void main(String args[]) throws Exception
{
// Create a file object
// for the path of existing Excel file
// Give the path of the file as parameter
// from where file is to be read
File file = new File( "Geeks.xlsx" );
// Create a FileInputStream object
// for getting the information of the file
FileInputStream fip = new FileInputStream(file);
// Getting the workbook instance for XLSX file
XSSFWorkbook workbook = new XSSFWorkbook(fip);
// Ensure if file exist or not
if (file.isFile() && file.exists()) {
System.out.println( "Geeks.xlsx open" );
}
else {
System.out.println( "Geeks.xlsx either not exist"
+ " or can't open" );
}
}
}


  • 将代码作为java应用程序运行

输出:

极客。xlsx开放

eclipse中的文件位置

图片[1]-使用Apache POI在Java中打开现有Excel工作表-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享