java excel to bean_还原Excel内容到 java bean list 的通用方法.

这篇博客介绍了如何利用jxls库将Excel文件高效地转换为JavaBean列表。通过配置文件,可以轻松映射Excel列到JavaBean的属性,简化了数据导入的过程。虽然引入的依赖较多,但提供的便利性弥补了这一不足。示例代码展示了从Excel文件读取数据并转换为Person对象的实现。
摘要由CSDN通过智能技术生成

以前通常会把java bean list 输出为EXCEL文件,作为报表供客户下载,有时候也需要读入客户准备好的Excel文档,并将这些数据导入到数据库中,或者做其他处理, 一般来说导入 excel 文件到内存中应该比较容易,但jxls这个库提供了另外一种处理方式,通过配置文件配置指定导入的column和对应的pojo类的属性.虽然自己也可以做到,但人家已经实现了,还是很方便的, 唯一郁闷的是 导入的jar包比较多,比较变态.

主要用到的工具 jxls: http://jxls.sourceforge.net/

5d461d58fe9183d68bb5061106274639.png

1. 一个公用的处理方法, 将导入的excel文件转为 java bean list:package com.yihaomen.parseexceltojavabeanlist;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import net.sf.jxls.reader.ReaderBuilder;

import net.sf.jxls.reader.XLSReader;

public class Util{

public ListparseExcelFileToBeans(File xlsFile,

File jxlsConfigFile)

throws Exception {

XLSReader xlsReader = ReaderBuilder.buildFromXML(jxlsConfigFile);

Listresult = new ArrayList();

Mapbeans = new HashMap();

beans.put("result", result);

InputStream inputStream = null;

try {

inputStream = new BufferedInputStream(new FileInputStream(xlsFile));

xlsReader.read(inputStream, beans);

}catch(Exception e){

e.printStackTrace();

}finally{

if (inputStream !=null){

inputStream.close();

}

}

return result;

}

}

要转换的java bean 的定义如下:

package com.yihaomen.parseexceltojavabeanlist;

public class Person {

private String firstName;

private String lastName;

private int age;

public Person() {

}

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;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

重点是配置文件的处理,看 excel 文件 与 java bean之间的 对应关系:

person.firstName

person.lastName

person.age

测试代码如下:

package com.yihaomen.parseexceltojavabeanlist;

import java.io.File;

import java.util.List;

public class TestApp {

public static void main(String[] args) throws Exception {

String path = System.getProperty("user.dir");

Util util = new Util();

String excelFilePath = path + File.separator + "personData.xls";

String configFilePath = path + File.separator + "personConfig.xml";

Listpersons = util.parseExcelFileToBeans(new File(excelFilePath),

new File(configFilePath));

for(Person p : persons){

System.out.println(p.getFirstName());

}

}

}

Excel 文件内容如下:

FirstName LastName Age

Joe Bloggs 25

John Doe 30

621fe420ad997d8d1950087449bd43f1.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值