需要导入的Jar包:
<dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.4.7</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.7</version> </dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
</dependency>
主代码:
package component; import model.Employee; import net.sf.jxls.transformer.XLSTransformer; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.testng.annotations.Test; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class TestExcel { @Test public void testExcel2() throws ParseException, IOException, InvalidFormatException { String srcFilePath = "D:\\IDEAworkspace\\testComponent\\src\\main\\resources\\ob_template.xls";//注意写自己的路径 List<Employee> employees = generateSampleEmployeeData(); Map<String, List> beans = new HashMap<String, List>(); beans.put("employees",employees); String destFilePath = "D:\\IDEAworkspace\\testComponent\\src\\main\\resources\\object_collection_output.xls"; XLSTransformer transformer = new XLSTransformer(); transformer.transformXLS(srcFilePath, beans, destFilePath); } public static List<Employee> generateSampleEmployeeData() throws ParseException { List<Employee> employees = new ArrayList<Employee>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", Locale.US); employees.add( new Employee("Elsa", dateFormat.parse("1970-Jul-10"), 1500, 0.15) ); employees.add( new Employee("Oleg", dateFormat.parse("1973-Apr-30"), 2300, 0.25) ); employees.add( new Employee("Neil", dateFormat.parse("1975-Oct-05"), 2500, 0.00) ); employees.add( new Employee("Maria", dateFormat.parse("1978-Jan-07"), 1700, 0.15) ); employees.add( new Employee("John", dateFormat.parse("1969-May-30"), 2800, 0.20) ); return employees; } }
Bean:
package model; import java.math.BigDecimal; import java.util.Date; public class Employee { private String name; private Date birthDate; private BigDecimal payment; private BigDecimal bonus; public Employee(String name, Date birthDate, BigDecimal payment, BigDecimal bonus) { this.name = name; this.birthDate = birthDate; this.payment = payment; this.bonus = bonus; } public Employee(String name, Date birthDate, double payment, double bonus) { this(name, birthDate, new BigDecimal(payment), new BigDecimal(bonus)); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public BigDecimal getPayment() { return payment; } public void setPayment(BigDecimal payment) { this.payment = payment; } public BigDecimal getBonus() { return bonus; } public void setBonus(BigDecimal bonus) { this.bonus = bonus; } }
模板文件:
导出的结果: