@GetMapping("/export")
public void export(Consult consult, HttpServletResponse response) {
consultService.export(consult,response);
}
//导出excel
@Override
public void export(Consult consult, HttpServletResponse response) {
//目标数据
Page<Consult> consultPage = selectPage(1, 1000, consult);
List<Consult> list = consultPage.getRecords();
//创建一个工作铺
XSSFWorkbook workbook=new XSSFWorkbook();
//创建一个sheet
XSSFSheet sheet = workbook.createSheet("sheet名称");
String[] heads={"序号","标题","内容","删除","创建日期","修改日期"};
//背景颜色
XSSFColor color = new XSSFColor(new java.awt.Color(0, 0, 255));
//单元格样式
XSSFCellStyle headStyle = getHeadStyleWithBorder(workbook, color, "宋体", (short) 12, true);
//表头行
XSSFRow headRow = sheet.createRow(0);
//首行添加内容
for (int i = 0; i < heads.length; i++) {
//创建headRow首行单元格
XSSFCell cell = headRow.createCell(i);
cell.setCellValue(heads[i]);
cell.setCellStyle(headStyle);
}
//contentStyle内容单元格样式
XSSFCellStyle contentStyle = getContentStyleWithBorder(workbook);
for (int i = 0; i < list.size(); i++) {
//获取对象
Consult record = list.get(i);
//创建行,从第二行开始
XSSFRow row = sheet.createRow(i + 1);
//创建单元格设置+样式
//序号
XSSFCell cell0 = row.createCell(0);
cell0.setCellValue(i+1);
cell0.setCellStyle(contentStyle);
//标题
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue(record.getTitle());
cell1.setCellStyle(contentStyle);
//内容
XSSFCell cell2 = row.createCell(2);
cell2.setCellValue(record.getContent());
cell2.setCellStyle(contentStyle);
//删除
XSSFCell cell3 = row.createCell(3);
cell3.setCellValue(record.getDeleted());
cell3.setCellStyle(contentStyle);
//创建日期
XSSFCell cell4 = row.createCell(4);
cell4.setCellValue(getDateStr(record.getCreateTime()));
cell4.setCellStyle(contentStyle);
//修改日期
XSSFCell cell5 = row.createCell(5);
cell5.setCellValue(getDateStr(record.getUpdateTime()));
cell5.setCellStyle(contentStyle);
}
//设置1、3列列宽
sheet.setColumnWidth(1,3000);
sheet.setColumnWidth(2,7000);
exportToWeb(response,workbook,"信息表");
}
//表头单元格类型
public static XSSFCellStyle getHeadStyleWithBorder(XSSFWorkbook workbook, XSSFColor color,String word,Short wordSize,Boolean isBold) {
XSSFCellStyle headStyle = workbook.createCellStyle();
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headStyle.setWrapText(true);
headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headStyle.setBorderBottom(BorderStyle.THIN);
headStyle.setBorderLeft(BorderStyle.THIN);
headStyle.setBorderRight(BorderStyle.THIN);
headStyle.setBorderTop(BorderStyle.THIN);
headStyle.setFillForegroundColor(color);
XSSFFont font = workbook.createFont();
font.setFontName(word);
font.setFontHeightInPoints(wordSize);
font.setBold(isBold);
headStyle.setFont(font);
return headStyle;
}
//内容单格类型
public static XSSFCellStyle getContentStyleWithBorder(XSSFWorkbook workbook) {
XSSFCellStyle contentStyle = workbook.createCellStyle();
contentStyle.setAlignment(HorizontalAlignment.CENTER);
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentStyle.setBorderBottom(BorderStyle.THIN);
contentStyle.setBorderLeft(BorderStyle.THIN);
contentStyle.setBorderRight(BorderStyle.THIN);
contentStyle.setBorderTop(BorderStyle.THIN);
contentStyle.setWrapText(true);
return contentStyle;
}
//导出设置
public void exportToWeb(HttpServletResponse response, XSSFWorkbook workbook, String name){
OutputStream out = null;
try {
response.setContentType("application/x-download");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
out = response.getOutputStream();
workbook.write(out);
} catch (IOException e) {
// log.error("导出"+name+"失败", e);
}finally {
IOUtils.closeQuietly(out);
}
}
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>