当前位置:网站首页>EasyExcel导出案例(只有你想不到)
EasyExcel导出案例(只有你想不到)
2022-07-24 01:00:00 【Ark方舟】
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.8</version>
</dependency>JavaBean
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.util.Date;
@ColumnWidth(20)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 9) // IndexedColors.WHITE
@HeadFontStyle(fontHeightInPoints = 10, fontName = "Arial")
@ContentStyle(dataFormat = 0x31, borderBottom = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderLeft = BorderStyle.THIN)
@ContentFontStyle(fontHeightInPoints = 10, fontName = "Arial")
public class LaoUser {
@ExcelProperty("ICCID")
private Long iccid;
@ExcelProperty("供应商")
private String supplierName;
@ExcelProperty("运营商")
private String operatorName;
@ExcelProperty("套餐")
private String comboName;
@ExcelProperty("使用量")
private String usageAmount;
@ExcelProperty("状态")
private String status;
@ExcelProperty("创建时间")
private Date createTime;
// set / get
}Controller接口
import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;
import com.test.mapper.ReportInfoMapper;
import com.test.poi.LaoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
@RestController
public class FileController {
@Autowired
ReportInfoMapper reportInfoMapper;
@GetMapping("/export")
public void exportFile(int num, HttpServletResponse response) throws IOException {
// 查询数据
Map<String, Object> params = new HashMap<>();
params.put("num", num);
List<LaoUser> laoUserList = reportInfoMapper.findReportList(params);
// 设响应头response信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// 写入数据到Excel中
EasyExcel.write(response.getOutputStream(), LaoUser.class).sheet("模板").doWrite(laoUserList);
}
}
自定义单元格写入类型(如对象属性是 Long类型,写入表格后是数字类型的单元格内容,想要转换成字符串,使用如下方法):
@ExcelProperty(value = "订单编号", converter = LongStringConverter.class)
import java.text.ParseException;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
/**
* Long and string converter
*/
public class LongStringConverter implements Converter<Long> {
@Override
public Class supportJavaTypeKey() {
return Long.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws ParseException {
return NumberUtils.parseLong(cellData.getStringValue(), contentProperty);
}
@Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty);
}
}
其他自行补脑(上诉案例够用了)

边栏推荐
- 入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰
- Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
- [QNX Hypervisor 2.2用户手册]9.1 配置变量
- Database connection pool & dbutils
- How to use SAP intelligent robotic process automation to automate Excel
- RPM build has installed the dependent package, but it still reports an error. There is no module provided in the package
- Sword finger offer frog jumps stairs
- Treatment of particle boundary collision
- Hot 100 dynamic programming
- Bert article translation
猜你喜欢

What impact does the European "gas shortage" have on China?

Deep understanding of collaborative process

The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is

Xilinx FPGA one way clock input two PLLs

Dark horse programmer - interface test - four day learning interface test - day 4 - postman reads external data files, reads data files, IHRM project practice, employee management module, adds employe

The way to access global variables in multi-source file mode (extern usage)

Centernet target detection model and centerfusion fusion target detection model

Database connection pool & dbutils

Seektiger's okaleido has a big move. Will the STI of ecological pass break out?

Focus on microservices
随机推荐
Source code installation and use of APIs IX
Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器
【Flyway 介绍】
对皮尔逊相关系数进行假设检验
Bean Validation自定义容器验证篇----06
Idea hot deployment (hot load)
Summary of the fourth week of summer vacation
Use of crawler request library 2
[QNX hypervisor 2.2 user manual]9 VM configuration reference
PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
Create a self signed certificate to digitally sign exe files
[QNX hypervisor 2.2 user manual]9.1 configuration variables
Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
Bean Validation使用篇----05
MySQL exercise: all employees reporting to the CEO
The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
Image processing: Generation 3 × Window of 3
Dataframe.groupby learning materials