欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Excel文件导入导出,excel导入导出,使用java 1.6及以

来源: javaer 分享于  点击 35560 次 点评:72

Excel文件导入导出,excel导入导出,使用java 1.6及以


使用java 1.6及以上版本应用jar文件:

<groupId>org.apache.poi</groupId><artifactId>poi-examples</artifactId><version>3.8-beta2</version>

文中有下载。

使用方法:

1.首先把数据封装成javabean,然后再属性上写上注解,注解对应Excel第一行的名字例:

public class ExcelModel{    @Property(name="id")private int id;    @Property(name="name")private String name;    public int getId() {return id;}    public void setId(int id) {this.id = id;}    public String getName() {return name;}    public void setName(String name){this.name = name;}}

2.使用ExcelOpt.调用执行方法

public class ExcelOptTest {    public static void main(String[] args) {        //导入        List<List<ExcelModel>> examplList =  null;        try {            examplList = ExcelOpt.readExcel("D:\\a.xls",ExcelModel.class);            for(List<ExcelModel> list : examplList){                for(ExcelModel em :list)                    System.out.println(em.getId()+":"+em.getName());                }                System.out.println();           }       }catch (Exception e) {           e.printStackTrace();      }      //导出      ExcelOpt.writeExcel(new File("D:\\b.xls"), ExcelModel.class, examplList);   }}

ExcelOpt.java

package org.beanguo.excelOpt;import java.io.File;import java.io.FileNotFoundException;import java.util.List;import org.beanguo.excelOpt.impl.Excel2003;import org.beanguo.excelOpt.impl.Excel2007;/** * 定义操作Excel功能 *  * @version 1.0 * @author Jamin * @createTime 2012-11-07 * */public class ExcelOpt {    /**     * 读取Excel文件     *      * @param Excel文件路径,数据的封装类型     * @return 以数据封装类型为元素的二维集合     * @throws FileNotFoundException      * */    public static <X> List<List<X>> readExcel(String filePath, Class<X> clazz) throws FileNotFoundException{        if(filePath == null){            throw new FileNotFoundException("参数filePath为null");        }        return readExcel(new File(filePath), clazz);    }    /**     * 读取Excel文件     *      * @param Excel文件对象,数据的封装类型     * @return 以数据封装类型为元素的二维集合     * */    public static <X> List<List<X>> readExcel(File file, Class<X> clazz){        Excel<X> excel = null;        try{            excel = new Excel2003<X>(file, clazz);            return excel.readExcel();        }catch(Exception e){            excel = new Excel2007<X>(file, clazz);            return excel.readExcel();        }    }    /**     * 按页读取Excel文件     *      * @param Excel文件路径,数据的封装类型,页码     * @return 以数据封装类型为元素的二维集合     * @throws FileNotFoundException     * */    public static <X> List<X> readExcel(String filePath, Class<X> clazz, int pageNo) throws FileNotFoundException{        if(filePath == null){            throw new FileNotFoundException("参数filePath为null");        }        return readExcel(filePath, clazz, pageNo);    }    /**     * 按页读取Excel文件     *      * @param Excel文件对象,数据的封装类型,页码     * @return 以数据封装类型为元素的二维集合     * */    public static <X> List<X> readExcel(File file, Class<X> clazz, int pageNo){        if(pageNo < 1){            throw new IndexOutOfBoundsException("参数pageNo="+pageNo+"必须从1开始");        }        Excel<X> excel = null;        try{            excel = new Excel2003<X>(file, clazz);            return excel.readExcel(pageNo-1);        }catch(Exception e){            excel = new Excel2007<X>(file, clazz);            return excel.readExcel(pageNo-1);        }    }    /**     * 生成Excel文件     *      * @param Excel文件对象,数据的封装类型,封装的数据     * */    public static <X> void writeExcel(File file, Class<X> clazz, List<List<X>> list){        Excel<X> excel = new Excel2003<X>(file, clazz);        excel.writeExcel(list);    }    /**     * 生成Excel文件     *      * @param Excel文件路径,数据的封装类型,封装的数据     * @throws FileNotFoundException      * */    public static <X> void writeExcel(String filePath, Class<X> clazz, List<List<X>> list) throws FileNotFoundException{        if(filePath == null){            throw new FileNotFoundException("参数filePath为null");        }        Excel<X> excel = new Excel2003<X>(new File(filePath), clazz);        excel.writeExcel(list);    }}
相关栏目:

用户点评