Java中如何使用Apache Poi读取Excel文件
时间:2020-02-23 14:34:22 来源:igfitidea点击:
在本教程中,我们将看到Java中如何使用Apache Poi示例读取Excel。
关于Apache Poi项目:
Apache Poi项目的使命是根据Office打开XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)来创建和维护Java API以便操纵各种文件格式。
简而言之,我们可以使用Java读取和写入MS Excel文件。
关于Apache Poi的一些基础知识:
在Java中读取/写作Excel时,我们遇到了两个前缀
HSSF:用于处理文件Excel 2003或者更早版本(.xls)。
HSSF前缀的一些类是HSSFWorkbook,HSSFSheet,HSSFrow和HSSFCell。
XSSF:用于处理文件Excel 2007或者更高版本(.xlsx)。
具有XSSF前缀的一些类是XSSFWorkbook,XSSFSheet,XSSFrow和XSSFCell。
以下是我们需要了解的程序。
- 工作簿:这是代表Excel工作簿的高级等级。
- 表:这是表示Excel表的高级等级。
- 行:这是表示Excel行的高级类。它有与行有关的方法。
- 单元:这是表示单个Excel单元的高级等级。它具有与单元格相关的方法,例如:getDatatype()。
依赖性:
如果我们使用的是maven,则需要在pom.xml中添加以下依赖项。
org.apache.poi poi 3.13 org.apache.poi poi-ooxml 3.13
如果我们不使用Maven,则需要在ClassPath中添加以下JAR。
- poi-3.13.jar.
- Commons-codec-1.9.jar
- poi-ooxml-3.13.jar
- Poi-oOxML-Schemas-3.13.jar
- XMLBeans-2.6.0.jar.
- stax-api-1.0.1.jar
使用POI读取Excel文件:
Java程序:
我们将读取Countrations.xlsx。
它的内容是:
创建ReadWriiteexcelMain.java如下
package org.igi.theitroad;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelMain {
public static void main(String[] args) throws IOException {
readFileUsingPOI();
}
public static void readFileUsingPOI() throws IOException
{
ClassLoader classLoader = ReadWriteExcelMain.class.getClassLoader();
String excelFilePath = "Countries.xlsx";
FileInputStream inputStream = new FileInputStream(new File(classLoader.getResource(excelFilePath).getFile()));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator iterator = sheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
}
System.out.print(" | ");
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
运行上面的程序时,我们将获取以下输出:
Country | Capital | Population | Netherlands | Delhi | 10000.0 | France | Paris | 40000.0 | Germany | Berlin | 20000.0 | England | London | 30000.0 |
让我们使用面向对象方法。
我们将阅读每行并创建国家对象。
显然我们将跳过标题行。
在package com.igi.theitroad.model中创建一个名为country.java的类.model
package com.igi.theitroad.model;
public class Country {
String name;
String capital;
double population;
//getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPopulation() {
return population;
}
public void setPopulation(double population) {
this.population = population;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public String toString()
{
return name+" | "+capital+" | "+population ;
}
}
readexcelwithcounrymain.java.
package org.igi.theitroad;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.igi.theitroad.model.Country;
public class ReadExcelWithCountryMain {
public static void main(String[] args) throws IOException {
List countries=readFileUsingPOI();
for(Country country:countries)
{
System.out.println(country.toString());
}
}
public static List readFileUsingPOI() throws IOException
{
List countries=new ArrayList();
ClassLoader classLoader = ReadExcelWithCountryMain.class.getClassLoader();
String excelFilePath = "Countries.xlsx";
FileInputStream inputStream = new FileInputStream(new File(classLoader.getResource(excelFilePath).getFile()));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator iterator = sheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
//Not creating country object for header
if(nextRow.getRowNum()==0)
continue;
Country countryObj=new Country();
Iterator cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex=cell.getColumnIndex();
switch (columnIndex+1) {
case 1:
countryObj.setName(cell.getStringCellValue());
break;
case 2:
countryObj.setCapital(cell.getStringCellValue());
break;
case 3:
countryObj.setPopulation(cell.getNumericCellValue());
break;
}
}
countries.add(countryObj);
}
workbook.close();
inputStream.close();
return countries;
}
}
运行上面的程序时,我们将获取以下输出:
Netherlands | Delhi | 10000.0 France | Paris | 40000.0 Germany | Berlin | 20000.0 England | London | 30000.0

