vue
<template>
<el-col>
<el-button type="primary" @click="excel()">导出</el-button>
</el-col>
</template>
import querystring from "querystring";
methods: {
excel(){
const query = require("querystring")
let queryExParam = '';
if(this.exParam){
queryExParam = '?' +query.stringify(this.exParam);
}
let downloadUrl = `${baseUrl}/boss-sms/sms-mt/export${queryExParam}`;
downloadFile({ url: downloadUrl })
},
downloadFile = ({ url = '' } = {}) => {
const aLink = document.createElement('a')
aLink.href = url
aLink.click()
document.body.appendChild(aLink)
document.body.removeChild(aLink)
}
}
java
实体类
package net.qh.boss.sms.dto;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
public class SmsMtExportDto {
private static final long serialVersionUID = 1L;
@Excel(name = "发送用户", height = 10, width = 20, orderNum = "1")
private String createBy;
@Excel(name = "手机号码", height = 10, width = 20, orderNum = "2")
private String phone;
@Excel(name = "短信内容", height = 10, width = 20, orderNum = "3")
private String smsContent;
}
pom
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
ExcelStyleUtil
package net.qh.boss.sms.utils;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;
public class ExcelStyleUtil implements IExcelExportStyler {
private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
private static final short FONT_SIZE_TEN = 10;
private static final short FONT_SIZE_ELEVEN = 11;
private static final short FONT_SIZE_TWELVE = 12;
private CellStyle headerStyle;
private CellStyle titleStyle;
private CellStyle styles;
public ExcelStyleUtil(Workbook workbook) {
this.init(workbook);
}
private void init(Workbook workbook) {
this.headerStyle = initHeaderStyle(workbook);
this.titleStyle = initTitleStyle(workbook);
this.styles = initStyles(workbook);
}
@Override
public CellStyle getHeaderStyle(short color) {
return headerStyle;
}
@Override
public CellStyle getTitleStyle(short color) {
return titleStyle;
}
@Override
public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
return styles;
}
@Override
public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
return getStyles(true, entity);
}
@Override
public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
return null;
}
private CellStyle initHeaderStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
return style;
}
private CellStyle initTitleStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return style;
}
private CellStyle initStyles(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
style.setDataFormat(STRING_FORMAT);
style.setFillBackgroundColor(IndexedColors.RED.getIndex());
style.setFillForegroundColor(IndexedColors.RED.getIndex());
return style;
}
private CellStyle getBaseCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setWrapText(true);
return style;
}
private Font getFont(Workbook workbook, short size, boolean isBold) {
Font font = workbook.createFont();
font.setFontName("宋体");
font.setBold(isBold);
font.setFontHeightInPoints(size);
return font;
}
}
Controller
@GetMapping("/export")
@ApiOperation("excel导出")
public void export(SmsMtSearchVo searchVo, HttpServletResponse response) {
smsMtService.export(searchVo,response);
}
ServiceImpl
@Override
public void export(SmsMtSearchVo searchVo, HttpServletResponse response) {
String sheetName = "短信发送记录_" + DateUtil.format(new Date(), "yyyy-MM-dd");
try (OutputStream out = response.getOutputStream()){
response.getOutputStream();
ExportParams params = new ExportParams(null, sheetName, ExcelType.XSSF);
params.setTitleHeight((short) 20);
params.setStyle(ExcelStyleUtil.class);
List<SmsMtDto> smsMtDtos = this.selectSmsMtList(searchVo);
List<SmsMtExportDto> numberExportDTOS = smsMtDtos.stream().map(item -> {
SmsMtExportDto exportDto = CopyUtils.INSTANCE.map(SmsMtExportDto.class, item);
exportDto.setCreateTime(DateUtil.format(item.getCreateTime(), "yyyy-MM-dd
return exportDto;
}).collect(toList());
Workbook workbook = ExcelExportUtil.exportExcel(params, SmsMtExportDto.class, numberExportDTOS);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename="
+ new String((sheetName + ".xlsx").getBytes("GBK"), "ISO-8859-1"));
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}