当前位置:网站首页>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 )

边栏推荐
- Centernet target detection model and centerfusion fusion target detection model
- Off screen rendering & FBO
- Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
- Kubernets déploiement du tableau de bord (interface visuelle)
- Notes: binary tree pruning (recursion, iteration)
- [QNX Hypervisor 2.2用户手册]9 VM配置参考
- Implementation of singleton mode and prevention of reflection and serialization
- Bert article translation
- WinVerifyTrust调用返回80096005错误,时间戳签名或证书无法验证或已损坏
- Focus on microservices
猜你喜欢

How to use SAP intelligent robotic process automation to automate Excel

Prometheus+node exporter+grafana monitoring server system resources

Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes

Deep understanding of collaborative process

C language database: detailed description. Use the student management system to understand the operation of the database, which is simple and easy to understand.

Pbootcms database conversion tutorial (SQLite to MySQL detailed tutorial)

Sublime text 3 汉化+添加常用插件

IDEA设置 自动导包删无用包

MySQL exercise: all employees reporting to the CEO

Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
随机推荐
Socket basic knowledge and various usage scenarios
C language database: an online dictionary based on TCP multi process, with detailed steps illustrated. Welcome to watch it
SQL CASE 多条件用法
1000 okaleido tiger launched binance NFT, triggering a rush to buy
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
docker redis
OSPF experiment
cnpm 执行时卡住应该怎么解决?
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
Determination of host byte order
Image processing 1:rgb888_ YCbCr444
Understand the locks that can't
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Bubble sort, quick sort
How to relieve the pillow quickly
Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
JS related knowledge
Case error of MySQL branch statement
vim常用命令