任何人都可以建议我使用 Apache POI 将我的 xlsx 表转换为 java 对象吗?
eq,我的 Excel 工作表包含两列
和我的java对象
Employee{
String empNo;
String empName;
}
现在我想将我的 Excel 工作表转换为 Java 对象。 我在互联网上尝试过,但大多数教程都讨论迭代每一行并为对象中的每个成员分配值。 JAXB xml 解析器中是否有类似 Marshaller 和 UnMarshaller 的功能可以直接转换。
提前致谢。
对于给定的场景,我假设工作表的每一行都代表一名员工,其中第一列保存员工编号,第二列保存员工姓名。所以你可以使用以下内容:
Employee{
String empNo;
String empName;
}
创建一个将员工信息分配为
的方法assignEmployee(Row row){
empNo = row.getCell(0).toString();
empName = row.getCell(1).toString();
}
或者如果您愿意,您可以为其创建一个构造函数。
现在您只需使用上述方法迭代每一行即可获取/使用信息。
Employee emp = new Employee();
Iterator<Row> itr = sheet.iterator();
while(itr.hasNext()){
Row row = itr.next();
emp.assignEmployee(row);
// enter code here for the rest operation
}
在内部使用 Apache POI 尝试此库从 excel 转换为 POJO: 波吉
检查以下存储库。它的开发始终牢记“易用性”。 https://github.com/millij/poi-object-mapper
初始版本已发布到Maven Central。
<dependency>
<groupId>io.github.millij</groupId>
<artifactId>poi-object-mapper</artifactId>
<version>3.0.0</version>
</dependency>
与杰克逊类似。像下面这样注释你的 bean..
@Sheet
public class Employee {
// Pick either field or its accessor methods to apply the Column mapping.
...
@SheetColumn("Age")
private Integer age;
...
@SheetColumn("Name")
public String getName() {
return name;
}
...
}
并阅读..
...
final File xlsxFile = new File("<path_to_file>");
final XlsReader reader = new XlsReader();
List<Employee> employees = reader.read(Employee.class, xlsxFile);
...
就目前而言,所有原始数据类型都受支持。仍在努力添加对
Date
、Formula
等的支持..
希望这有帮助。
我正在使用 POI,并且正在上传一个简单的程序。希望对你有帮助。
注意:记住更改文件路径。
Jars 详细信息:dom4j-1.6.1.jar、poi-3.9.jar、poi-ooxml-3.9.jar、poi-ooxml-schemas-3.11.jar、xmlbeans-2.6.0.jar
我的Excel表格中的数据:
ID NAME LASTNAME
1.0 Ena Rana
2.0 Meena Hanly
3.0 Tina Mounce
4.0 Dina Cobain
模型或 Pojo:NewEmployee.java
public class NewEmployee {
private Double id;
private String firstName;
private String lastName;
public NewEmployee(){}
public NewEmployee(Double id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Double getId() {
return id;
}
public void setId(Double id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
主要方法:ExcelToObject.java
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
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 ExcelToObject {
public static void main(String[] args) {
try
{
FileInputStream file = new FileInputStream(new File("/home/ohelig/eclipse/New Worksheet.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<NewEmployee> employeeList = new ArrayList<>();
//I've Header and I'm ignoring header for that I've +1 in loop
for(int i=sheet.getFirstRowNum()+1;i<=sheet.getLastRowNum();i++){
NewEmployee e= new NewEmployee();
Row ro=sheet.getRow(i);
for(int j=ro.getFirstCellNum();j<=ro.getLastCellNum();j++){
Cell ce = ro.getCell(j);
if(j==0){
//If you have Header in text It'll throw exception because it won't get NumericValue
e.setId(ce.getNumericCellValue());
}
if(j==1){
e.setFirstName(ce.getStringCellValue());
}
if(j==2){
e.setLastName(ce.getStringCellValue());
}
}
employeeList.add(e);
}
for(NewEmployee emp: employeeList){
System.out.println("ID:"+emp.getId()+" firstName:"+emp.getFirstName());
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
您也可以考虑使用这个小库excelorm
刚刚找到两个库:
jxls-reader
(但配置完全在 XML 中完成...呃!!!) http://jxls.sourceforge.net/reference/reader.htmlexcel-object-mapping
这只是注释,但不幸的是它的创建者并没有真正支持,例如仍在“1.0-SNAPSHORT”中或查看唯一的拉取请求https://github.com/jittagornp/excel-object-mapping(编辑:从那时起回购已被删除,我在这里找到了一个克隆:https:/ /github.com/pramoth/excel-object-mapping )希望它能帮助别人。
我也遇到了同样的问题,我知道通过标准(Apache POI)实现为什么要花费这么多时间,所以在搜索和环顾四周之后,我找到了更好的原因(JXLS-Reader)
首先使用/导入/包含库 jxls-reader
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.3</version>
</dependency>
然后创建一个由库用于列和对象属性之间对应关系的 XML 文件,该 XML 将一个初始化列表作为参数,通过从 Excel 文件中提取的数据(Employee 对象)来填充它,在您的示例中,它看起来像:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook>
<worksheet idx="0">
<section startRow="0" endRow="0" />
<loop startRow="1" endRow="1" items="employeeList" var="employee" varType="com.department.Employee">
<section startRow="1" endRow="1">
<mapping row="1" col="0">employee.empNo</mapping>
<mapping row="1" col="1">employee.empName</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0"></cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>
然后在Java中,初始化Employees列表(其中将包含解析结果),然后通过输入Excel文件和XML映射调用JXLS阅读器,它看起来像:
package com.department;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.ReaderConfig;
import org.jxls.reader.XLSReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
public class ExcelProcessor {
private static Logger logger = LoggerFactory.getLogger(ExcelProcessor.class);
public void parseExcelFile(File excelFile) throws Exception{
final List<Employee> employeeList = new ArrayList<Employee>();
InputStream xmlMapping = new BufferedInputStream(ExcelProcessor.class.getClassLoader().getResourceAsStream("proBroMapping.xml"));
ReaderConfig.getInstance().setUseDefaultValuesForPrimitiveTypes(true);
ReaderConfig.getInstance().setSkipErrors(true);
InputStream inputXLS;
try{
XLSReader mainReader = ReaderBuilder.buildFromXML(xmlMapping);
inputXLS = new BufferedInputStream(new FileInputStream(excelFile));
final Map<String, Object> beans = new HashMap<String, Object>();
beans.put("employeeList", employeeList);
mainReader.read(inputXLS, beans);
System.out.println("Employee data are extracted successfully from the Excel file, number of Employees is: "+employeeList.size());
} catch(java.lang.OutOfMemoryError ex){
// Case of a very large file that exceed the capacity of the physical memory
ex.printStackTrace();
throw new Exception(ex.getMessage());
} catch (IOException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} catch (SAXException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} catch (InvalidFormatException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
希望这对遇到此类问题的人有所帮助!
我想找到一种简单的方法将 xls/xlsx 文件解析为 pojo 列表。经过一番搜索后,我没有找到任何方便的东西,而是希望快速开发它。现在我只需拨打电话即可获得 pojo:
InputStream is = this.getClass().getResourceAsStream("/ExcelUtilsTest.xlsx");
List<Pojo> pojos = ExcelToPojoUtils.toPojo(Pojo.class, is);
有兴趣的话可以看看:
查看使用 Apache POI 将 xlsx 工作表绑定到对象列表的 example。
这是一个非常简单的示例,展示如何使用 Apache POI 将 Microsoft Excel (xlsx) 工作表转换为对象列表。
这个想法只是在要将工作表列映射到的字段上定义注释@ExcelCellInfo。然后,工作表单元格将根据注释属性使用反射进行绑定。
使用示例:
ExcelSheetDescriptor<RowClassSample> sheetDescriptor = new ExcelSheetDescriptor<>(RowClassSample.class).setHasHeader();
List<RowClassSample> rows = ExcelUtils.readFirstSheet("pathToFile.xlsx", sheetDescriptor);
以及要绑定到的类:
public class RowClassSample {
@ExcelCellInfo(index = 0)
private long serial;
@ExcelCellInfo(index = 1)
private String name;
@ExcelCellInfo(index = 2, cellParser = CellNumericAsStringParser.class)
private String registrationNumber;
@ExcelCellInfo(index = 3, cellParser = CellPercentageParser.class)
private Double percentage;
@ExcelCellInfo(index = 6)
private String reason;
@ExcelCellInfo(index = 4)
private String notes;
@ExcelCellInfo(index = 5, cellParser = CellBooleanYesNoArParser.class)
private boolean approval;
// getters & setters
}