使用apache poi进行excel导出

使用apache poi进行excel导出

文件索引:
1 TestExport.java 主程序
2 student.java 数据bean
3 template.xls 模板文件


以下为例子程序:
TestExport.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;

public class TestExport {

    public void export() {
        List<Student> studentList = new ArrayList<Student>();
       
        Student studentA = new Student();
        studentA.setName("tom");
        studentA.setAge(20);
        Student studentB = new Student();
        studentB.setName("John");
        studentB.setAge(21);
       
        studentList.add(studentA);
        studentList.add(studentB);
       
        Map beans = new HashMap();
        beans.put("studentList", studentList);
        XLSTransformer transformer = new XLSTransformer();
        try {
            InputStream xlsTemplateIO = new BufferedInputStream(new FileInputStream("./template.xls"));
            Workbook book = transformer.transformXLS(xlsTemplateIO, beans);
            OutputStream os = new FileOutputStream("./abc.xls");
            book.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

------------------------------------------------------------------------------------------------------------


Student.java

public class Student {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

 

------------------------------------------------------------------------------------------------------------

 

模板文件:template.xls

name age
<jx:forEach items="${studentList}"  var="student">  
${student.name} ${student.age}
</jx:forEach>  

 

  ------------------------------------------------------------------------------------------------------------


使用到的jar
jxls-core-1.0-RC-1.jar
jxls-reader-1.0-RC-1.jar
poi-3.6.jar
poi-ooxml-3.6.jar
poi-ooxml-schemas-3.6.jar
commons-logging-1.1.1.jar
commons-digester-1.6.jar
commons-collections-3.2.1.jar
commons-beanutils-1.8.2.jar
commons-jexl-2.0.1.jar


以上只是示例,并且已经在项目中应用过,项目要求的功能就是这么简单,本人也没有深入研究过。

你可能感兴趣的:(使用apache poi进行excel导出)