java excel导入导出

阅读: 评论:0

java excel导入导出

java excel导入导出

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

net.sourceforge.jexcelapi

jxl

2.6.12

看图说话:

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 list, String path) {

WritableWorkbook book = null;

try {

// 创建一个Excel文件对象

book = ateWorkbook(new File(path));

// 创建Excel第一个选项卡对象

WritableSheet sheet = ateSheet("第一页", 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 = (i);

// 表头占据第一行,所以下面行数是索引值+1

// 跟上面添加表头一样添加单元格数据,这里为了方便直接使用链式编程

sheet.addCell(new Label(0, i + 1, Name()));

sheet.addCell(new Label(1, i + 1, Age().toString()));

sheet.addCell(new Label(2, i + 1, Telephone()));

sheet.addCell(new Label(3, i + 1, Address()));

}

// 写入数据到目标文件

book.write();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 关闭

book.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

2、导入代码:

public static List excelImport(String path) {

List list = new ArrayList<>();

Workbook book = null;

try {

// 获取Excel对象

book = Workbook(new File(path));

// 获取Excel第一个选项卡对象

Sheet sheet = Sheet(0);

// 遍历选项卡,第一行是表头,所以索引数-1

for (int i = 0; i < Rows() - 1; i++) {

Customer customer = new Customer();

// 获取第一列第二行单元格对象

Cell cell = Cell(0, i + 1);

customer.Contents());

// 获取第二行其他数据

customer.setAge(Integer.Cell(1, i + 1).getContents()));

customer.Cell(2, i + 1).getContents());

customer.Cell(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 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 list = excelImport(path);

for (Customer customer : list) {

System.out.println(customer);

}

}

本文发布于:2024-02-08 20:10:35,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170739450368744.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:java   excel
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23