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);
}
}
Java 使用 Jxl 实现 Excel 导入导出的更多相关文章
- Java之POI的excel导入导出
一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...
- java利用jxl实现Excel导入功能
本次项目实践基于Spring+SpringMvc+MyBatis框架,简单实现了Excel模板导出.和Excel批量导入的功能.实现过程如下:. 1.maven导入所需jar包 <depende ...
- 记录-java(jxl) Excel导入数据库
本内容主要包括(文件上传.excel2003数据导入数据库)excel导入数据库功能需要jxl jar包支持 下面是文件上传的前端测试代码 <%@ page language="ja ...
- java jxl excel 导入导出的 总结(建立超链接,以及目录sheet的索引)
最近项目要一个批量导出功能,而且要生成一个单独的sheet页,最后后面所有sheet的索引,并且可以点击进入连接.网上搜索了一下,找到一个方法,同时把相关的excel导入导出操作记录一下!以便以后使用 ...
- Java基础学习总结(49)——Excel导入导出工具类
在项目的pom文件中引入 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifac ...
- Java Excel 导入导出(一)
本文主要描述通过java实现Excel导入导出 一.读写Excel三种常用方式 1.JXL——Java Excel开放源码项目:读取,创建,更新 2.POI——Apache POI ,提供API给Ja ...
- JAVA实现Excel导入/导出【转】
JAVA实现Excel导入/导出[转] POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bi ...
- java简易excel导入导出工具(封装POI)
Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...
- Java基于注解和反射导入导出Excel
代码地址如下:http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包以 ...
随机推荐
- linux上遇到tomcat报Out of Memory错误,导致jenkins崩溃的问题
今天遇到一个问题,就是JENKINS在同时部署两个前端应用时会出现崩溃的现象. 排查过程如下 查看tomcat-jenkins/bin/hs_err_pid27127.log发现: Out of Me ...
- 监控服务器配置(四)-----OracleDb_exporter安装配置
1.下载oracle客户端安装包(linux版)到 /opt/minitor/oracleDb . 下载地址:https://download.csdn.net/download/a155657721 ...
- (三)Bootstrap.jar
catalina.bat 在最后启动了bootstrap.jar, 传递了start作为参数(如果多个参数的话,start在尾部). 然后org.apache.catalina.startup.Boo ...
- Tableau可视化绘图教程
https://www.w3cschool.cn/tableau/tableau_environment_setup.html
- CSS3网页动画
CSS3网页动画 概要:CSS3变形是一些效果的集合 如:平移.旋转.缩放.倾斜效果 每个效果都可以称为变形(transform)他们可以分别操控元素发生平移.旋转.缩放.倾斜等变化. 网页中能够实现 ...
- Java Swing实现展示数据,以及过滤排序
public class RelationCostctrTable extends DefaultTableModel { public RelationCostctrTable(Vector< ...
- iframe和form表单实现ajax请求上传数据
form的target属性设置为iframe的name值时,表示提交到url后返回的数据显示到iframe区域 <form action="/upload.html" met ...
- 【NIFI】 Apache NiFI 集群搭建
NiFI 集群介绍 NiFi集群架构 NiFi采用Zero-Master Clustering范例.集群中的每个节点对数据执行相同的任务,但每个节点都在不同的数据集上运行.其中一个节点自动选择(通过A ...
- HDU-4763 Theme Section KMP
题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...
- mongodb内嵌文档的javaapi,增删改查
数据结构: {"_id" : "000000001", //Mongodb默认主键 "UID" : "000000001&quo ...