在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
Microsoft Excel 工作簿是一款基于电子表格的软件,允许用户以高效的方式组织和存储数据。 它以表格形式存储和组织数据,即使用行和列。 excel 电子表格中的单元格是存放数据的方框,可以使用不同的样式、格式和公式进行操作。
阅读 Excel 文件有时很复杂。 由于 Excel 的单元格,用 Java 阅读 Excel 文件与用 Java 阅读 Word 文件也有些不同。 JDK 不提供读取或写入 Microsoft Excel 文档的直接 API。 但我们必须依赖第三方库 Apache POI。
POI 是 "Poor Obfuscation Implementation "的缩写。 Apache POI 是一个开源 Java 库,用于读写微软文档。 它提供了一种基于 Microsoft Office 创建和处理各种文件格式的方法。使用 Apache POI,人们应该能够对一系列 Microsoft Office 文件格式执行创建、修改和显示/读取操作。
首先,我们将下载最新版本的 POI JAR。 导航至 http://poi.apache.org/download.html 并下载最新的 ZIP 文件,其中将包含在 Java 中读取 Excel 文件的 Java API。
下载 Apache POI JAR 文件
下载 ZIP 文件后,需要解压缩并将以下 JAR 文件添加到项目的类路径中。 下文从 Excel 文件中对此进行了解释。
将 Apache POI Jar 文件添加到 Java classpath 中
注意: 也要将 lib 和 ooxml-lib 中的文件与其他文件一起复制。
以下是可使用的支持 XLS 和 XLSX 文件格式的类:
阿帕奇 POI 类
下面列出了 POI 中不同的 Java 接口和类,用于在 Java 中读取 XLS 和 XLSX 文件:
工作簿接口由 HSSFWorkbook
和 XSSFWorkbook
类实现。
XSSF工作簿:这是 XLSX 文件的类表示。
Sheet 接口由 HSSFSheet
和 XSSFSheet
类实现。
XSSFSheet:这是一个表示 XLSX 文件中工作表的类。
行接口由 HSSFRow
和 XSSFRow
类实现。
XSSFRow:这是一个表示 XLSX 文件工作表中一行的类。
单元格接口由 HSSFCell
和 XSSFCell
类实现。
在我们的示例中,我们将用 Java 阅读以下 Excel 文件:
将读取的 Excel 文件
使用任何 Java IDE 创建 Java 项目。本项目将使用 Netbeans。
接下来,在项目中创建 lib 文件夹。
然后,将下载的 JAR 文件添加到上一步创建的 lib 文件夹中。
右键单击 "项目文件夹">"构建路径">"添加外部 JAR 文件">"选择上述所有 JAR 文件">"应用并关闭",设置 "类路径"。
现在,让我们创建一个名称为 ReadExcelFileDemo 的类文件。
创建一个名为 "studentdata.xls "的 Excel 文件,并将上述数据或任何数据写入其中。
以下是用 Java 阅读 Excel 文件的代码示例:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
public class ReadExcelFileDemo {
public static void main(String args []) throws IOException {
//obtaining input bytes from a file
FileInputStream fis=new FileInputStream(new File("C:\\demo\\studentdata.xls"));
//creating workbook instance that refers to .xls file
HSSFWorkbook wb=new HSSFWorkbook(fis);
//creating a Sheet object to retrieve the object
HSSFSheet sheet=wb.getSheetAt(0);
//evaluating cell type
FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();
for(Row row: sheet) {
for(Cell cell: row) {
switch(formulaEvaluator.evaluateInCell(cell).getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //field that represents numeric cell type
//getting the value of the cell as a number
System.out.print(cell.getNumericCellValue()+ "\t\t");
break;
case Cell.CELL_TYPE_STRING: //field that represents string cell type
//getting the value of the cell as a string
System.out.print(cell.getStringCellValue()+ "\t\t");
break;
}
}
System.out.println();
}
}
}
输出:
IdNames
1Zeeshan
2Shoaib
3Umar
4Rizwan
5Ahsan
除以下两个要点外,阅读 xlsx 文件的步骤相同:
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XLSXReaderExample {
public static void main(String [] args) {
try {
File file = new File("C:\\demo\\studentdata.xlsx");
FileInputStream fis = new FileInputStream(file);
//creating Workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //field that represents numeric cell type
//getting the value of the cell as a number
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING: //field that represents string cell type
//getting the value of the cell as a string
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
输出:
IdNames
1Zeeshan
2Shoaib
3Umar
4Rizwan
5Ahsan
IronXLMicrosoft Excel 是一个独立的.NET 库,便于使用 C# 阅读和编辑 Microsoft Excel 文档。 译文既不要求安装 Microsoft Excel,也不依赖 Interop。
使用 IronXL.Excel,开发人员只需编写几行代码,就能毫不费力地执行所有与 Excel 相关的计算,而且性能飞快。 翻译的任务包括添加两个单元格、计算一列的总计、向 Excel 表格中添加一整列、向 Excel 表格中添加一整行、单列和多列求和,或通过 IronXL.Excel 轻松完成的许多其他任务。有用的功能.
IronXL 完全支持 .NET Framework、.NET Core、Mobile、Xamarin、Azure Linux 和 MacOS。
样式 - 视觉样式、字体和字体大小、背景图案、边框、对齐方式和数字格式。
以下是使用 IronXL.Excel 在 C# 中读取 Excel 文件的代码示例:
using IronXL;
using System.Linq;
//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();
//Select cells easily in Excel notation and return the calculated value
int cellValue = sheet ["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in sheet ["A2:A10"]) {
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
using IronXL;
using System.Linq;
//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();
//Select cells easily in Excel notation and return the calculated value
int cellValue = sheet ["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in sheet ["A2:A10"]) {
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
Imports IronXL
Imports System.Linq
'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private cellValue As Integer = sheet ("A2").IntValue
' Read from Ranges of cells elegantly.
For Each cell In sheet ("A2:A10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
与 IronXL 合作,开发人员的工作将变得更加轻松。 其代码简单易用,使软件在处理 Excel 文件时不易出现错误。
下载 IronXL并将其用于您的项目中。