SpringBoot集成poi实现xlsx导入导出

SpringBoot集成poi实现xlsx导入导出

Apache POI

Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式的开源项目。简单来说就是它可以通过Java程序读写Excel。

模块

HSSF - 提供读写Microsoft Excel XLS格式(Microsoft Excel 97 (-2003))档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式(Microsoft Excel XML (2007+))档案的功能。
SXSSF - 提供低内存占用量读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式(Microsoft Word 97 (-2003))档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式(WordprocessingML (2007+))档案的功能。
HSLF/XSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF/XDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

本篇文章主要介绍XSSF(表格)导入导出, 工作中最常使用到的

(本篇demo技术架构:SpringBoot + Swagger + MyBatis-Plus)

  • pom文件引入poi依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>
  • 本篇文章用到的表
create table boot_user
(
    user_id       int auto_increment comment '用户ID' primary key,
    user_name     varchar(32)   null comment '用户名',
    user_sex      int default 0 null comment '性别(0:男,1:女)',
    user_age      int           null comment '年龄',
    user_password varchar(32)   null comment '密码',
    user_status   int default 0 null comment '状态(0:启用,1禁用)'
)
    comment '用户表';
  • 导出工具类
/**
 * 导出工具类
 * @Author: CTW
 * @Date: create in 2021/7/24
 */
public class ExportUtils {
   

    public static void ExportBootUser(HttpServletResponse response, String sheetName, String[] title, List<BootUser> bootUserList) {
   
        //新建文档实例
        XSSFWorkbook workbook = new XSSFWorkbook();

        //在文档中添加表单
        XSSFSheet sheet = workbook.createSheet(sheetName);

        //创建单元格格式,并设置居中
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFCellStyle style2 = workbook.createCellStyle();

        style.setAlignment(HorizontalAlignment.CENTER);
        style2.setAlignment(HorizontalAlignment.CENTER);

        //标题边框
        //下边框
        style.setBorderBottom(BorderStyle.THICK);
        //左边框
        style.setBorderLeft(BorderStyle.THICK);
        //右边框
        style.setBorderRight(BorderStyle.THICK);
        //上边框
        style.setBorderTop(BorderStyle.THICK);

        //普通边框
        //下边框
        style2.set
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现Excel导入导出,需要用到POI库。下面介绍一下使用SpringBoot集成MyBatis和POI实现Excel导入导出的步骤。 1. 引入依赖 在pom.xml文件中添加以下依赖: ``` <!-- SpringBoot MyBatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- POI 依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> ``` 2. 创建实体类 创建一个实体类,用于映射Excel文件中的数据。 ```java public class User { private Integer id; private String name; private String email; private String phone; // 省略 getter 和 setter 方法 } ``` 3. 创建Mapper 创建一个Mapper,用于操作数据库。 ```java @Mapper public interface UserMapper { List<User> findAll(); void insert(User user); void batchInsert(List<User> userList); } ``` 4. 创建Service 创建一个Service,用于导入导出Excel文件。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> findAll() { return userMapper.findAll(); } public void insert(User user) { userMapper.insert(user); } public void batchInsert(MultipartFile file) throws IOException { List<User> userList = readExcel(file); userMapper.batchInsert(userList); } public void exportExcel(HttpServletResponse response) throws IOException { List<User> userList = userMapper.findAll(); writeExcel(response, userList); } private List<User> readExcel(MultipartFile file) throws IOException { List<User> userList = new ArrayList<>(); Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); User user = new User(); user.setName(row.getCell(0).getStringCellValue()); user.setEmail(row.getCell(1).getStringCellValue()); user.setPhone(row.getCell(2).getStringCellValue()); userList.add(user); } return userList; } private void writeExcel(HttpServletResponse response, List<User> userList) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Users"); Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("Name"); headerRow.createCell(1).setCellValue("Email"); headerRow.createCell(2).setCellValue("Phone"); for (int i = 0; i < userList.size(); i++) { Row row = sheet.createRow(i + 1); User user = userList.get(i); row.createCell(0).setCellValue(user.getName()); row.createCell(1).setCellValue(user.getEmail()); row.createCell(2).setCellValue(user.getPhone()); } response.setHeader("Content-Disposition", "attachment; filename=users.xlsx"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); workbook.write(response.getOutputStream()); } } ``` 5. 创建Controller 创建一个Controller,用于接收导入导出Excel文件的请求。 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public List<User> findAll() { return userService.findAll(); } @PostMapping("/users") public void insert(@RequestBody User user) { userService.insert(user); } @PostMapping("/users/import") public void batchInsert(@RequestParam("file") MultipartFile file) throws IOException { userService.batchInsert(file); } @GetMapping("/users/export") public void exportExcel(HttpServletResponse response) throws IOException { userService.exportExcel(response); } } ``` 至此,就完成了SpringBoot集成MyBatis和POI实现Excel导入导出的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撸码社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值