当前位置: 首页 > news >正文

网站维护什么情况网站运营教程

网站维护什么情况,网站运营教程,it前端是做网站的,小程序制作开发定制在Spring Boot项目中导出复杂对象到Excel文件&#xff0c;可以利用Hutool或EasyExcel等库来简化操作。这里我们将详细介绍如何使用Hutool和EasyExcel两种方式来实现这一功能。 使用Hutool导出复杂对象到Excel 首先确保你的pom.xml中添加了Hutool的依赖&#xff1a; <depe…

在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel等库来简化操作。这里我们将详细介绍如何使用Hutool和EasyExcel两种方式来实现这一功能。

使用Hutool导出复杂对象到Excel

首先确保你的pom.xml中添加了Hutool的依赖:

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.10</version> <!-- 请根据实际情况选择最新版本 -->
</dependency>

接下来是一个简单的示例,展示如何导出一个包含复杂对象的列表到Excel文件。

示例代码

假设我们有一个User类,它包含一个嵌套的Address对象。

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;@RestController
@RequestMapping("/api")
public class UserController {@GetMapping("/exportUsers")public void exportUsers(HttpServletResponse response) throws IOException {// 模拟获取用户数据List<User> users = getUsers();// 创建ExcelWriter实例ExcelWriter writer = ExcelUtil.getWriter(true); // true表示自动创建表头// 将复杂对象转换为Map列表,方便写入ExcelList<Map<String, Object>> dataList = users.stream().map(user -> {Map<String, Object> row = new HashMap<>();row.put("ID", user.getId());row.put("姓名", user.getName());row.put("邮箱", user.getEmail());row.put("年龄", user.getAge());row.put("城市", user.getAddress().getCity());row.put("街道", user.getAddress().getStreet());return row;}).collect(Collectors.toList());// 写入数据writer.write(dataList, true);// 设置响应内容类型和头部信息response.setContentType("application/vnd.ms-excel;charset=utf-8");String fileName = URLEncoder.encode("用户列表", "UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");// 将输出流写入responseServletOutputStream out = response.getOutputStream();writer.flush(out, true);out.close();writer.close();}private List<User> getUsers() {List<User> users = new ArrayList<>();Address address = new Address("北京", "中关村大街");users.add(new User(1L, "张三", "zhangsan@example.com", 28, address));return users;}
}class User {private Long id;private String name;private String email;private Integer age;private Address address;public User(Long id, String name, String email, Integer age, Address address) {this.id = id;this.name = name;this.email = email;this.age = age;this.address = address;}// getter和setter方法
}class Address {private String city;private String street;public Address(String city, String street) {this.city = city;this.street = street;}// getter和setter方法
}

使用EasyExcel导出复杂对象到Excel

EasyExcel是阿里巴巴开源的一个非常高效的Excel处理库,特别适合处理大数据量的Excel文件。首先,在pom.xml中添加EasyExcel的依赖:

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.10</version> <!-- 请根据实际情况选择最新版本 -->
</dependency>

接下来是一个使用EasyExcel导出复杂对象的例子。

示例代码

假设我们仍然使用上面提到的UserAddress类。

import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;@RestController
@RequestMapping("/api")
public class EasyExcelController {@GetMapping("/exportUsers")public void exportUsers(HttpServletResponse response) throws IOException {// 模拟获取用户数据List<User> users = getUsers();// 设置响应内容类型和头部信息response.setContentType("application/vnd.ms-excel;charset=utf-8");String fileName = URLEncoder.encode("用户列表", "UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");// 使用EasyExcel写出数据到输出流EasyExcel.write(response.getOutputStream(), UserData.class).sheet("用户信息").doWrite(users);}private List<User> getUsers() {List<User> users = new ArrayList<>();Address address = new Address("北京", "中关村大街");users.add(new User(1L, "张三", "zhangsan@example.com", 28, address));return users;}
}// 数据实体类
class UserData {@com.alibaba.excel.annotation.ExcelProperty("ID")private Long id;@com.alibaba.excel.annotation.ExcelProperty("姓名")private String name;@com.alibaba.excel.annotation.ExcelProperty("邮箱")private String email;@com.alibaba.excel.annotation.ExcelProperty("年龄")private Integer age;@com.alibaba.excel.annotation.ExcelProperty("城市")private String city;@com.alibaba.excel.annotation.ExcelProperty("街道")private String street;// 构造函数、getter和setter方法public UserData(User user) {this.id = user.getId();this.name = user.getName();this.email = user.getEmail();this.age = user.getAge();this.city = user.getAddress().getCity();this.street = user.getAddress().getStreet();}// getter和setter方法
}

在这个例子中,我们定义了一个UserData类来映射User对象的数据,并使用EasyExcel将这些数据写入Excel文件。

通过上述方法,你可以轻松地在Spring Boot项目中导出复杂对象到Excel文件。无论是使用Hutool还是EasyExcel,都可以有效地简化Excel处理的工作。

http://www.yayakq.cn/news/247326/

相关文章:

  • 网站 毕业设计代做徐州建设工程公共资源交易
  • 建视频网站需要多少钱做网站的应该怎么发广告
  • 如何创建旅游网站长江工程建设局网站
  • 网站后台上传文字图片app开发公司名字
  • 衡阳做网站成都管理咨询公司排名
  • 爬虫做视频网站手机兼职app
  • 番禺品牌型网站建设运行两个wordpress
  • 呼和浩特网站建设计算机培训短期速成班
  • 如何给异地网站做镜像网站开发能进无形资产吗
  • 大连开发区网站开发公司电话node.js做网站好累
  • 做360手机网站快wordpress订阅者投稿
  • 做网站广告中敏感词会涉及到工商手机网站开发源码
  • 攸县做网站的济南好的seo
  • vs做网站好不好中和华丰建设有限责任公司网站
  • 网站建设的总结100字谷歌推广公司
  • 网站模板内容怎么添加图片做带支付平台的网站
  • 云服务器 可以做网站吗拼多多商品关键词搜索排名
  • 网站地图html怎么做哈尔滨网页设计招聘
  • 毕业设计选择做网站的意义沧州青县机械加工网
  • 网站 工作室 建设 方案家具网站开发目的
  • 电销客户数据怎么买seo长尾快速排名
  • 大型电子商务网站建设2015年做那些网站致富
  • dwcc怎么做网站wordpress淘宝客 采集器
  • 深圳个人网站制作群晖打开wordpress4.9.8
  • 网站建设及解析流程北京有哪些电商平台公司
  • 某些网站网速慢怎样创建网站赚钱
  • h5网站设计报价济宁企业网站建设
  • 浙江备案需要开启网站吗余姚网站建设公司
  • 企业网站建设 广州北京市公司网站制作
  • 江苏省建设厅工会网站佛山债优化