当前位置:网站首页>Easyexcel export case (only you can't think of it)
Easyexcel export case (only you can't think of it)
2022-07-24 01:03:00 【Ark 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(" supplier ")
private String supplierName;
@ExcelProperty(" Operator, ")
private String operatorName;
@ExcelProperty(" package ")
private String comboName;
@ExcelProperty(" Usage quantity ")
private String usageAmount;
@ExcelProperty(" state ")
private String status;
@ExcelProperty(" Creation time ")
private Date createTime;
// set / get
}Controller Interface
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 {
// Query data
Map<String, Object> params = new HashMap<>();
params.put("num", num);
List<LaoUser> laoUserList = reportInfoMapper.findReportList(params);
// Set response header response Information
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(" test ", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// Write data to Excel in
EasyExcel.write(response.getOutputStream(), LaoUser.class).sheet(" Templates ").doWrite(laoUserList);
}
}
Custom cell write type ( For example, the object attribute is Long type , After the table is written, the cell content of the number type , Want to convert to string , Use the following methods ):
@ExcelProperty(value = " The order no. ", 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);
}
}
Other self tonifying ( There are enough appeals )

边栏推荐
- Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
- 【LeetCode第 83 场双周赛】
- Memory forensics nssctf otterctf 2018 (replay)
- Prometheus operator user guide notes
- Create a self signed certificate to digitally sign exe files
- Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P
- vim常用命令
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- URL query parameter encoding problem (golang)
- Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi
猜你喜欢

Graphic pipeline (I) post-processing stage alpha test template test depth test mix

Kubernets déploiement du tableau de bord (interface visuelle)

Bert article translation

【LeetCode第 83 场双周赛】

How to use SAP intelligent robotic process automation to automate Excel

Sparksql design and introduction, 220722,

kubernetes 部署 dashboard(可视化界面)

这是一道大水题

Focus on microservices

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
随机推荐
深入理解协程
Chapter 1 water test --*offer
Tutorial on the principle and application of database system (050) -- MySQL query (XII): analysis of the execution process of select command
kubernetes 部署 dashboard(可视化界面)
What the hell is ThreadLocal doing?
爬虫requests模块的基本使用
C language database: an online dictionary based on TCP multi process, with detailed steps illustrated. Welcome to watch it
Source code installation and use of APIs IX
Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
Creo 9.0 mouse button operation for model observation
freemarker
Bubble sort, quick sort
Establishment of static route
【LeetCode第 83 场双周赛】
Bert article translation
js的相关知识
Concurrent programming 1-2
The postman test interface has 404 or 500 errors when the URL is configured correctly
VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network
Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by