Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel

本项目源码 github 下载

1 新建 Spring Boot Maven 示例工程项目

注意:本示例是用 IDEA 开发工具

  1. File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步
  2. 填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步

    groupId=com.fishpro

    artifactId=excel
  3. 选择依赖 Spring Web Starter 前面打钩。
  4. 项目名设置为 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://poi.apache.org/

https://www.iteye.com/blog/chenhailong-1498528

Spring Boot 操作 Excel的更多相关文章

  1. Spring Boot 导出Excel表格

    Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...

  2. 使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef

    使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef ...

  3. MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB

    Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...

  4. spring boot缓存excel临时文件后再操作

    1. 引入poi的两个依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi ...

  5. Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  6. Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  7. spring boot 操作MySQL pom添加的配置

    1 在项目中的pom.xml配置文件添加依赖 <!--MySQL依赖 --> <dependency> <groupId>mysql</groupId> ...

  8. Spring boot 导出Excel

    Html页面: window.location.href="adjectfkController/exportTemplate?adjOrg="+ adjOrg +"&a ...

  9. spring boot读取Excel

    首先引入相关依赖 <!--解析office相关文件--> <dependency> <groupId>org.apache.poi</groupId> ...

随机推荐

  1. 【HTML】三种方法使HTML单页面输入密码才能访问

    方法一 <script type="text/javascript"> function password() { var testV = 1; var pass1 = ...

  2. python3练习100题——005

    继续做题-答案都会经过py3测试原题网址:http://www.runoob.com/python/python-exercise-example5.html 题目:输入三个整数x,y,z,请把这三个 ...

  3. 一键安装各个版本boost库(无需编译)

    1.NuGet 最简单的,用VS自带的NuGet包管理器安装,一般比较常用的上面都有 2.下载exe安装包 在这里https://sourceforge.net/projects/boost/file ...

  4. spring(五):AOP

    AOP(Aspect Oriented Programming) 面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行OOP开发时,都是基于对组件(比如 ...

  5. 图的最小生成树prim算法模板

    用prim算法构建最小生成树适合顶点数据较少而边较多的图(稠密图) prim算法生成连通图的最小生成树模板伪代码: G为图,一般为全局变量,数组d为顶点与集合s的最短距离 Prim(G, d[]){ ...

  6. 10.4.3反向迭代器Reverse_iterator笔记

    反向迭代器就是在容器中从尾元素向首元素反向移动的迭代器.对于反向携带器,递增(以及递减)操作的含义会颠倒过来.递增一个反向迭代器(++it)会移动到前一个元素:递减一个迭代器(--it)会移动到下一个 ...

  7. 如何在Go中获得 "A1","B2" 类似字符+数字的字符串

    package main import ( "fmt" ) func main() { // 字符串 str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ ...

  8. MySQL8.0服务启动(windows10)

    mysql下载地址:https://dev.mysql.com/downloads/mysql/ 根目录下配置文件:my.ini [mysqld]# 设置3306端口port=3306# 设置mysq ...

  9. vue工程 使用滚动组件 vue2-better-scroll 实现上拉加载 下拉刷新

    vue2-better-scroll 关于具体安装&使用过程 请移步api文档 已经很详细了 而且超清晰明了. https://cnpmjs.org/package/vue2-better-s ...

  10. 关于Ajax请求的JS封装函数

    每次连接ajax都要重复写很多代码,所以写了一个JS封装函数,如下: 再来解释一下其中obj对象的参数形式: obj={ 'type':提交方式,    get/post 'url' : 提交地址, ...