引用:
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;
代码:
@Override
public void exportChangeList(AnalysisVO analysis, HttpServletResponse response) {
List<AnalysisVO> records = this.changeList(new Page<>(1, -1), analysis).getRecords();
Assert.notEmpty(records, () -> new ServiceException("无导出数据"));
ExportParams exportParams = new ExportParams("变更列表(单位:个)", "变更列表", ExcelType.XSSF);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AnalysisExcel.class, BeanUtil.copy(records, AnalysisExcel.class));
try (OutputStream out = response.getOutputStream()) {
response.setHeader("Content-Disposition", "attachment; filename=changeList.xlsx");
response.setContentType("application/vnd.ms-excel");
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
实体类:
import cn.afterturn.easypoi.excel.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class AnalysisExcel {
@Excel(name = "序号", format = "isAddIndex")
private Integer index;
@ApiModelProperty(value = "主管单位ID")
private String budgetManageUnitId;
@Excel(name = "主管单位名称", width = 60)
private String budgetManageUnitName;
@Excel(name = "新建类", width = 25)
private String newClass;
@Excel(name = "运维类", width = 25)
private String operationsClass;
@Excel(name = "服务类", width = 25)
private String serviceClass;
@Excel(name = "排名", width = 30)
private String rank;
}