Spring Boot 操作 Excel
Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel
1 新建 Spring Boot Maven 示例工程项目
注意:本示例是用 IDEA 开发工具
- File > New > Project,如下图选择
Spring Initializr然后点击 【Next】下一步 - 填写
GroupId(包名)、Artifact(项目名) 即可。点击 下一步
groupId=com.fishpro
artifactId=excel - 选择依赖
Spring Web Starter前面打钩。 - 项目名设置为
spring-boot-study-excel.
文件上传不需要引入第三方组件。
2 依赖引入 Pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3 操作 Excel
不同的 Excel 版本具有不同的类来操作本示例中使用 xls 后缀版本。详细请参见 官方文档
3.1 创建 Workbook
HSSFWorkbook是操作 Excel2003 以前(包括2003)的版本,扩展名是.xls;XSSFWorkbook是操作 Excel2007 后的版本,扩展名是.xlsx;SXSSFWorkbook是操作 Excel2007 后的版本,扩展名是.xlsx;
public static void CreateNewWorkbook() {
Workbook wb = new HSSFWorkbook();
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
Workbook wb2 = new XSSFWorkbook();
try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
wb2.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
3.2 创建工作表 Sheet
- 工作表名称不要超过 31 个字符
- 名称不能含有特殊字符
- 可以使用 WorkbookUtil.createSafeSheetName 来创建安全的工作表名称
public static void CreateNewSheet() {
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("new second sheet");
String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
Sheet sheet3 = wb.createSheet(safeName);
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
3.3 创建单元格 Cells
- 先有行在有列,先要创建 Row 在创建 Cell
- 创建一个样式
- 创建一个日期类型的值
- 创建日期、小数、字符 、布尔等类型
- 创建一个边框类型单元格
- 数据格式化单元格
/**
*## 3.3 创建单元格 Cells
* - 先有行在有列,先要创建 Row 在创建 Cell
* */
public void CreateNewCell() {
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
//先行后列
Row row = sheet1.createRow(0);
//创建列
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
//创建一个列的样式
CellStyle cellStyle = wb.createCellStyle();
//获取一个帮助类设置样式
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//使用 Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
//创建不同的类型的单元格
row.createCell(3).setCellValue(1.1);
row.createCell(4).setCellValue(new Date());
row.createCell(5).setCellValue(Calendar.getInstance());
row.createCell(6).setCellValue("a string");
row.createCell(7).setCellValue(true);
row.createCell(8).setCellType(CellType.ERROR);
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
3.4 读取与获取Excel
使用 File 的方式读取 Excel
public static void OpenExcelByFile(){
try {
Workbook wb = WorkbookFactory.create(new File("workbook.xls"));
//读取
Sheet sheet=wb.getSheetAt(0);//第一个
Sheet sheet1=wb.getSheet("sheet1");//根据名称读取
Row row=sheet.getRow(0);//获取行
Cell cell=row.getCell(0);//获取第一行
}catch (Exception ex){
}
}
使用 FileInputStream 需要内存支持
public static void OpenExcelByFileInputStream(){
try {
Workbook wb = WorkbookFactory.create(new FileInputStream("workbook.xls"));
//遍历
}catch (Exception ex){
}
}
参考:
https://www.iteye.com/blog/chenhailong-1498528
Spring Boot 操作 Excel的更多相关文章
- Spring Boot 导出Excel表格
Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...
- 使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef
使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef ...
- MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...
- spring boot缓存excel临时文件后再操作
1. 引入poi的两个依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi ...
- Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
- Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
- spring boot 操作MySQL pom添加的配置
1 在项目中的pom.xml配置文件添加依赖 <!--MySQL依赖 --> <dependency> <groupId>mysql</groupId> ...
- Spring boot 导出Excel
Html页面: window.location.href="adjectfkController/exportTemplate?adjOrg="+ adjOrg +"&a ...
- spring boot读取Excel
首先引入相关依赖 <!--解析office相关文件--> <dependency> <groupId>org.apache.poi</groupId> ...
随机推荐
- python中使用anaconda对不平衡数据的处理包imblearn的安装
为了建模,处理不平衡数据,想使用SMOTEENN方法进行数据平衡处理,为此需要下载对应的包imblearn 最开始直接从anaconda中进行: conda install imblearn 报 ...
- html中特殊符号对应表
html中特殊符号的对应表 符号 说明 编码 ‘' 双引号 " & and符 & < 小于号 < > 大于号 > 空格 ...
- codeforces 1285E. Delete a Segment
链接:https://codeforces.com/problemset/problem/1285/E 题意:给一个数轴上有n个线段集,线段集若有相交,则合并为一个新的合并线段集,比如[1,6]和[2 ...
- CentOS7下使用C/C++连接MariaDB/MySQL
前言 连接数据库通常在Java中使用比较多,但是C/C++在Linux下操作数据库也是比较重要的,很多时候都能用得到,在网上查了很多教程,大多写的有些问题,通过自己摸索,终于成功的连接了MariaDB ...
- 关于Excute()方法,与in参数连用
DECLARE @tempTbl TABLE(OrderNo VARCHAR(50)) DECLARE @orderNos VARCHAR(4000) SET @orderNos='''3f1a82c ...
- 记manjaro图形驱动删除后的一次补救
#一.前言 众所周知,NVIDIA的闭源驱动在Linux上的兼容性不是很好,再加上我不玩游戏,于是我就想卸载独显只留核显.我以为我装了独显和核显两种驱动,原本想直接删除独显驱动,没想到删除的是bumb ...
- 通过CSS禁用页面内容选中和复制操作
-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;user ...
- 工具使用:xmind
概念 心智图,又称脑图.思维导图.灵感触发图.概念地图或思维地图,是一种图像式思维的工具与及一种利用图像式思考辅助工具来表达思维的工具. 详细的可以查看这里(维基百科)还有这里(百度百科) 用了思维导 ...
- Python之xml读写
遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ...
- [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对
题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...