【Java】轻量Excel读写框架JXL学习
参考黑马教程:
https://www.bilibili.com/video/BV1eA41157va?p=5
写着写着发现有更好的Demo案例:
https://cloud.tencent.com/developer/article/1966065
所需依赖:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.27</version> <!-- 请检查并使用最新版本 -->
</dependency> <dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
Excel输出API常见方法:
@SneakyThrows
public void downloadXlsByJxl(HttpServletResponse response) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ServletOutputStream outputStream = response.getOutputStream(); /* 基于输出流创建一个新的工作簿 */
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
WritableSheet sheet = workbook.createSheet("第一个工作表", 0); /* 创建标题行 */
List<String> titleList = Arrays.asList("编号", "姓名", "手机号", "入职日期", "现在地址"); /* 创建列宽配置 */
int standardCharWidth = 1;
int chineseCharWidth = 3;
List<Integer> columnWidthSets = Arrays.asList(
standardCharWidth * chineseCharWidth * 2,
standardCharWidth * chineseCharWidth * 3,
standardCharWidth * standardCharWidth * 12,
standardCharWidth * 12,
standardCharWidth * standardCharWidth * 20); Label eachLabel = null;
for (int columnIdx = 0; columnIdx < titleList.size(); columnIdx++) {
String title = titleList.get(columnIdx);
eachLabel = new Label(columnIdx, 0, title);
sheet.addCell(eachLabel); /* 列宽设置, 列下表, 一个标准字母的宽度 */
sheet.setColumnView(columnIdx, columnWidthSets.get(columnIdx));
} List<User> allUserList = userMapper.selectAll();
for (int recordIdx = 0, rowIdx = 1; recordIdx < allUserList.size(); recordIdx ++, rowIdx ++) {
User user = allUserList.get(recordIdx);
eachLabel = new Label(0, rowIdx, String.valueOf(user.getId()));
sheet.addCell(eachLabel); eachLabel = new Label(1, rowIdx, user.getUserName());
sheet.addCell(eachLabel); eachLabel = new Label(2, rowIdx, user.getPhone());
sheet.addCell(eachLabel); eachLabel = new Label(3, rowIdx, sdf.format(user.getHireDate()));
sheet.addCell(eachLabel); eachLabel = new Label(4, rowIdx, user.getAddress());
sheet.addCell(eachLabel);
} /* 写入图片 非图片文件无法写入 Warning: Image type doc not supported. Supported types are png */
File file = new File("C:\\Users\\Administrator\\Pictures\\AngelLegionPhoto\\Girl_20240303155025.png");
WritableImage writableImage = new WritableImage(10, 10, 1, 1, file);
sheet.addImage(writableImage); /* 写入链接 */
WritableHyperlink writableHyperlink = new WritableHyperlink(10, 11, new URL("https://www.bilibili.com/video/BV1eA41157va"));
sheet.addHyperlink(writableHyperlink);
writableHyperlink = new WritableHyperlink(10, 12, new File("D:\\迅雷下载\\20121015120541355.doc"));
sheet.addHyperlink(writableHyperlink); String fileName = "JXL-Demo.xls";
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), StandardCharsets.ISO_8859_1));
response.setContentType("application/vnd.ms-excel"); workbook.write();
workbook.close();
outputStream.close();
}
输出结果:

Excel读取常见API:
获取存在记录的单元格的边界列和行
能满足图片,公式,普通值的获取
@SneakyThrows
public void readXlsByJxl(MultipartFile multipartFile) {
InputStream inputStream = multipartFile.getInputStream();
Workbook workbook = Workbook.getWorkbook(inputStream);
Sheet sheet = workbook.getSheet(0);
int[] columnPageBreaks = sheet.getColumnPageBreaks();
int[] rowPageBreaks = sheet.getRowPageBreaks();
int columns = sheet.getColumns();
int rows = sheet.getRows();
int numberOfImages = sheet.getNumberOfImages();
Hyperlink[] hyperlinks = sheet.getHyperlinks();
log.info("columnPageBreaks {}", Arrays.toString(columnPageBreaks));
log.info("rowPageBreaks {}", Arrays.toString(rowPageBreaks));
log.info("columns {}", columns);
log.info("rows {}", rows);
log.info("numberOfImages {}", numberOfImages);
for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
StringBuilder currentRow = new StringBuilder();
for (int colIdx = 0; colIdx < columns; colIdx++) {
Cell cell = sheet.getCell(colIdx, rowIdx);
CellType type = cell.getType();
String contents = cell.getContents();
currentRow.append("type ").append(type.toString()).append(" ,").append(contents).append(" |");
}
log.info("cell -> {}", currentRow);
}
/* 读取图片 */
if (0 != numberOfImages) {
for (int i = 0; i < numberOfImages; i++) {
Image image = sheet.getDrawing(i);
File imageFile = image.getImageFile();
String name = imageFile.getName();
double column = image.getColumn();
double row = image.getRow();
byte[] imageData = image.getImageData();
Tika tika = new Tika();
/* 图片是准确的mimeType, 非图片只能识别为octStream */
String detect = tika.detect(imageData);
log.info("image -> idx {}, {} - {}, {}, {}", i, (int) column, (int) row, name, detect);
}
}
for (Hyperlink hyperlink : hyperlinks) {
boolean isFile = hyperlink.isFile();
boolean isLocation = hyperlink.isLocation();
boolean isUrl = hyperlink.isURL();
log.info("isFile {}, isLocation {}, isUrl {}", isFile, isLocation, isUrl);
String path = isFile ? hyperlink.getFile().getPath() : isUrl ? hyperlink.getURL().getPath() : "internalLocation";
int column = hyperlink.getColumn();
int row = hyperlink.getRow();
log.info("hyperlink -> {}, {}", column + "," + row, path);
}
}
样例输出
columnPageBreaks null
rowPageBreaks null
columns 11
rows 16
numberOfImages 1
cell -> type Label ,编号 |type Label ,姓名 |type Label ,手机号 |type Label ,入职日期 |type Label ,现在地址 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,1 |type Label ,大一 |type Label ,13800000001 |type Label ,2001-01-01 |type Label ,北京市西城区宣武大街1号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,2 |type Label ,不二 |type Label ,13800000002 |type Label ,2002-01-02 |type Label ,北京市西城区宣武大街2号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,3 |type Label ,张三 |type Label ,13800000003 |type Label ,2003-03-03 |type Label ,北京市西城区宣武大街3号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,4 |type Label ,李四 |type Label ,13800000004 |type Label ,2004-02-04 |type Label ,北京市西城区宣武大街4号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,5 |type Label ,王五 |type Label ,13800000005 |type Label ,2005-03-05 |type Label ,北京市西城区宣武大街5号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,6 |type Label ,赵六 |type Label ,13800000006 |type Label ,2006-04-06 |type Label ,北京市西城区宣武大街6号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,7 |type Label ,沈七 |type Label ,13800000007 |type Label ,2007-06-07 |type Label ,北京市西城区宣武大街7号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,8 |type Label ,酒八 |type Label ,13800000008 |type Label ,2008-07-08 |type Label ,北京市西城区宣武大街8号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,9 |type Label ,第九 |type Label ,13800000009 |type Label ,2009-03-09 |type Label ,北京市西城区宣武大街9号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,10 |type Label ,石十 |type Label ,13800000010 |type Label ,2010-07-10 |type Label ,北京市西城区宣武大街10号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,11 |type Label ,肖十一 |type Label ,13800000011 |type Label ,2011-12-11 |type Label ,北京市西城区宣武大街11号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Label ,https://www.bilibili.com/video/BV1eA41157va |
cell -> type Label ,12 |type Label ,星十二 |type Label ,13800000012 |type Label ,2012-05-12 |type Label ,北京市西城区宣武大街12号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Label ,D:\迅雷下载\20121015120541355.doc |
cell -> type Label ,13 |type Label ,钗十三 |type Label ,13800000013 |type Label ,2013-06-13 |type Label ,北京市西城区宣武大街13号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,14 |type Label ,贾十四 |type Label ,13800000014 |type Label ,2014-06-14 |type Label ,北京市西城区宣武大街14号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,15 |type Label ,甄世武 |type Label ,13800000015 |type Label ,2015-06-15 |type Label ,北京市西城区宣武大街15号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
image -> idx 0, 10 - 9, Girl_20240303155025.png, image/png
isFile false, isLocation false, isUrl true
hyperlink -> 10,11, /video/BV1eA41157va
isFile true, isLocation false, isUrl false
hyperlink -> 10,12, D:\迅雷下载\2012
【Java】轻量Excel读写框架JXL学习的更多相关文章
- 轻量的web框架Bottle
简洁的web框架Bottle 简介 Bottle是一个非常简洁,轻量web框架,与django形成鲜明的对比,它只由一个单文件组成,文件总共只有3700多行代码,依赖只有python标准库.但是麻雀虽 ...
- Nancy总结(一)Nancy一个轻量的MVC框架
Nancy是一个基于.net 和Mono 构建的HTTP服务框架,是一个非常轻量级的web框架. 设计用于处理 DELETE, GET, HEAD, OPTIONS, POST, PUT 和 PATC ...
- 轻量型ORM框架Dapper的使用
在真实的项目开发中,可能有些人比较喜欢写SQL语句,但是对于EF这种ORM框架比较排斥,那么轻量型的Dapper就是一个不错的选择,即让你写sql语句了,有进行了关系对象映射.其实对于EF吧,我说下我 ...
- OSCHina技术导向:Java轻量web开发框架——JFinal
JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful.在拥有Java语言所有优势的同时再拥有ru ...
- win8.1 cygwin编译java轻量虚拟机avian
1.背景 昨天在网上看到别人用aauto写本地小程序写的很爽,我觉得如果java的jre能小一点,凭借java庞大的第三方类库写小工具也还算不错的.本人就经常用eclipse+一些commons包写些 ...
- 开源 , KoobooJson一款高性能且轻量的JSON框架
KoobooJson - 更小更快的C# JSON序列化工具(基于表达式树构建) 在C#领域,有很多成熟的开源JSON框架,其中最著名且使用最多的是 Newtonsoft.Json ,然而因为版本迭代 ...
- Prism-超轻量的开源框架
http://msdn.microsoft.com/en-us/library/ff648465.aspx prism 是微软模式与实践小组开发的一个进行MVVM模式开发,其中使用依赖注入等一些方法将 ...
- 之前项目使用的轻量的goweb框架
技术栈 go 主开发语言 基于 gorilla 项目 javascript(nodejs) 部分小工具,josn对象转换,自动编译 C#,codesmith通用代码生成,生成最基本的crud和翻页. ...
- Droplet——一款轻量的Golang应用层框架
Github地址 如标题所描述的,Droplet 是一个 轻量 的 中间层框架,何为中间层呢? 通常来说,我们的程序(注意这里我们仅仅讨论程序的范围,而非作为一个系统,因此这里不设计如 LB.Gate ...
- Java学习---Excel读写操作
1.1.1. 简介 Apache POI 使用Apache POI 完成Excel读写操作 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API ...
随机推荐
- SELinux 基本原理
首发公号:Rand_cs SELinux 基本原理 本文讲述 SELinux 保护安全的基本原理 安全检查顺序 不废话,直接先来看张图 当我们执行系统调用的时候,会首先对某些错误情况进行检查,如果失败 ...
- window.onload()函数和jQuery中的document.ready()有什么区别?
a.执行时间:window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.$(document).ready()是DOM结构绘制 完毕后就执行,不必等到加载完毕.$(doc ...
- Oracle常用命令收藏
查看数据库名 SELECT t.NAME FROM V$DATABASE t; 查看实例名 SELECT t.INSTANCE_NAME FROM V$INSTANCE t; 查看SID select ...
- 纯代码搭建iOS三级结构(UITabbarController+UINavigationController+UIViewController)
声明:这里所指的三级结构不是网上百度中所经常提及的三级框架或者MVC模式,而是指UITabbarController+UINavigationController+UIViewController. ...
- 实验11.ACL实验
# 实验11.ACL实验 本实验用于测试ACL,类似于防火墙. 拓扑 要求阻塞PC1到PC2和server的全部协议,阻塞client1到server1的icmp协议 具体配置 首先利用ospf协议实 ...
- 关于kubesphere集群calico网络组件报错的修复
最近公司的项目用到了Kubesphere,于是自己先在虚拟机上测试了一番,遇到了很多的问题,现将遇到的有关calico的问题记录一下 上一篇介绍了如何离线安装kubesphere v3.0,安装之后我 ...
- java --面试题大全
J2EE面试题 文档版本号:V2.0 2016年11月 目 录 1. Java基础部分 8 1.1. 一个".java"源文 ...
- ubuntu 使用natapp配置内网穿透
前言 在自己的服务器上起了服务,但由于域名还没申请下来,无法使用域名测试微信公众号接口,辛亏看到了这个博客:Natapp内网穿透服务工具.跟随这篇博客,我搭建了自己的内网穿透服务,现在记录如下. 过程 ...
- Spring5.X的注解配置项目
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- PTA数据结构和答案解析
背景:期末数据结构复习题 绪论和线性表 判断题 The Fibonacci number sequence {F N } is defined as: F 0 =0, F 1 =1, F N =F N ...