欢迎与我联系   

Java 使用 Jxl 实现 Excel 导入导出

开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的。这里做一下使用JXL的学习记录。首先需要导入相应的jar包,pom.xml中添加如下内容即可

            <dependency>
                    <groupId>net.sourceforge.jexcelapi</groupId>
                    <artifactId>jxl</artifactId>
                    <version>2.6.12</version>
            </dependency>

看图说话:

0、数据实体类:
 


1、导出代码:
 


2、导入代码:
 


3、导出测试:
 


4、导出测试结果:
因为jxl不支持新本Excel格式,旧版本的表格最多只支持65536条数据;
这里测试导出最大65536条数据耗时1秒左右,速度还是非常快的,当然我这个跟我测试的实体字段比较少可能也有关系
 


5、导入测试:
 


6、导入测试结果:

以上就是Java实现Excel 导入导出的全部代码了

下面附上代码

数据实体类:

public class Customer {
        private String name;
        private Integer age;
        private String telephone;
        private String address;
        //这里get/set方法省略不贴
}

1、导出代码:

public static void excelExport(List<Customer> list, String path) {
        WritableWorkbook book = null;
        try {
                // 创建一个Excel文件对象
                book = Workbook.createWorkbook(new File(path));
                // 创建Excel第一个选项卡对象
                WritableSheet sheet = book.createSheet("第一页", 0);
                // 设置表头,第一行内容
                // Label参数说明:第一个是列,第二个是行,第三个是要写入的数据值,索引值都是从0开始
                Label label1 = new Label(0, 0, "姓名");// 对应为第1列第1行的数据
                Label label2 = new Label(1, 0, "年龄");// 对应为第2列第1行的数据
                Label label3 = new Label(2, 0, "手机号码");// 对应为第3列第1行的数据
                Label label4 = new Label(3, 0, "住址");// 对应为第4列第1行的数据
                // 添加单元格到选项卡中
                sheet.addCell(label1);
                sheet.addCell(label2);
                sheet.addCell(label3);
                sheet.addCell(label4);
                // 遍历集合并添加数据到行,每行对应一个对象
                for (int i = 0; i < list.size(); i++) {
                        Customer customer = list.get(i);
                        // 表头占据第一行,所以下面行数是索引值+1
                        // 跟上面添加表头一样添加单元格数据,这里为了方便直接使用链式编程
                        sheet.addCell(new Label(0, i + 1, customer.getName()));
                        sheet.addCell(new Label(1, i + 1, customer.getAge().toString()));
                        sheet.addCell(new Label(2, i + 1, customer.getTelephone()));
                        sheet.addCell(new Label(3, i + 1, customer.getAddress()));
                }
                // 写入数据到目标文件
                book.write();
        } catch (Exception e) {
                e.printStackTrace();
        } finally {
                try {
                        // 关闭
                        book.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

2、导入代码:

public static List<Customer> excelImport(String path) {
        List<Customer> list = new ArrayList<>();
        Workbook book = null;
        try {
                // 获取Excel对象
                book = book.getWorkbook(new File(path));
                // 获取Excel第一个选项卡对象
                Sheet sheet = book.getSheet(0);
                // 遍历选项卡,第一行是表头,所以索引数-1
                for (int i = 0; i < sheet.getRows() - 1; i++) {
                        Customer customer = new Customer();
                        // 获取第一列第二行单元格对象
                        Cell cell = sheet.getCell(0, i + 1);
                        customer.setName(cell.getContents());
                        // 获取第二行其他数据
                        customer.setAge(Integer.parseInt(sheet.getCell(1, i + 1).getContents()));
                        customer.setTelephone(sheet.getCell(2, i + 1).getContents());
                        customer.setAddress(sheet.getCell(3, i + 1).getContents());
                        list.add(customer);
                }
                // 返回导入的数据集合
                return list;
        } catch (Exception e) {
                e.printStackTrace();
        } finally {
                try {
                        // 关闭
                        book.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        return null;
}

3、导出测试代码:

public static void main(String[] args) {
        List<Customer> list = new ArrayList<>();
        // 创建数据
        for (int i = 0; i < 65535; i++) {
                Customer customer = new Customer();
                customer.setName("隔壁老王_" + i);
                customer.setAge(i);
                customer.setTelephone("1301234" + i);
                customer.setAddress("浙江杭州西湖");
                list.add(customer);
        }
        String path = "D:\\eclelTest\\xs.xls";
        System.out.println("开始导出...");
        long s1 = new Date().getTime();
        // 开始导出
        excelExport(list, path);
        long s2 = new Date().getTime();
        long time = s2 - s1;
        System.out.println("导出完成!消耗时间:" + time + "毫秒");
}

4、导入测试代码:

public static void main(String[] args) {
        String path = "D:\\eclelTest\\xs.xls";
        List<Customer> list = excelImport(path);
        for (Customer customer : list) {
                System.out.println(customer);
        }
}
posted @ 2018-09-03 16:04  小珍珠在河里敲代码  阅读(2212)  评论(0编辑  收藏  举报