最近在工作中遇到一个需求,将页面上的数据导出/下载到excel,百度了几篇文章,照着写出来了,感觉不是很“平易近人”,于是自己总结一篇,方便大家参考。
工具:Intellij IDEA、Navicat、Postman
我创建的是maven项目,所以jar在pom文件中引入,如下
org.apache.poi
poi
3.17
org.apache.poi
poi-scratchpad
3.17
org.apache.poi
poi-ooxml
3.17
org.apache.poi
poi-ooxml-schemas
3.17
第一步,创建导出工具类,部分代码添加注释(其实也是在网上抄的自己改了一下哈哈哈哈哈)
package com.yuanmou.utils.poi;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Lebron
* @ClassName ZxExportExcel
* @Description TODO
* @Date 2019/8/27 15:04
* @Version 1.0
*/
public class ZxExportExcel {
/**
* 生成Excel
*/
public XSSFWorkbook getWorkBook() {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("0");
for (int i = 0; i < 9; i++) {
sheet.setColumnWidth(i, 4300);
}
/**
* 单元格 样式
*/
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
/**
* 标题样式 样式
*/
XSSFFont titleFont = wb.createFont();
titleFont.setFontHeight(24);
titleFont.setBold(true);
CellStyle titleCellStyle = wb.createCellStyle();
titleCellStyle.setBorderTop(BorderStyle.THIN);
titleCellStyle.setBorderBottom(BorderStyle.THIN);
titleCellStyle.setBorderLeft(BorderStyle.THIN);
titleCellStyle.setBorderRight(BorderStyle.THIN);
titleCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
titleCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
titleCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
titleCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
titleCellStyle.setFont(titleFont);
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
/**
* 主标题 在这里插入主标题
*/
Row titleRow;
Cell titleCell;
sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 2, (short) 0, (short) 8));
for (int i = 0; i <= 2; i++) {
titleRow = sheet.createRow(i);
for (int j = 0; j < 9; j++) {
titleCell = titleRow.createCell(j);
titleCell.setCellType(CellType.STRING);
titleCell.setCellStyle(titleCellStyle);
titleCell.setCellValue("测试导出标题");
}
}
/**
* 列标题 在这里插入标题
*/
Row rowLabel;
Cell cellLabel;
rowLabel = sheet.createRow(3);
cellLabel = rowLabel.createCell(0);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("设备编号");
cellLabel = rowLabel.createCell(1);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("规格型号");
cellLabel = rowLabel.createCell(2);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("设备类型");
cellLabel = rowLabel.createCell(3);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("登记日期");
cellLabel = rowLabel.createCell(4);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("安装地点");
cellLabel = rowLabel.createCell(5);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("安装坐标");
cellLabel = rowLabel.createCell(6);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("误差范围/米");
cellLabel = rowLabel.createCell(7);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("片区");
cellLabel = rowLabel.createCell(8);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("提交人");
/**
* 页脚
*/
setExcelFooterName("测试-页脚", 0, wb);
return wb;
}
/**
* 设置Excel页脚
*/
public void setExcelFooterName(String customExcelFooterName, int setExcelFooterNumber, XSSFWorkbook wb) {
wb.setSheetName(setExcelFooterNumber, customExcelFooterName);
}
/**
* 输出流 导出Excel到桌面
*/
public void exportOutPutExcel(String exportPositionPath, XSSFWorkbook wb) {
try {
File file = new File(exportPositionPath);
FileOutputStream fileOutputStream = new FileOutputStream(file);
wb.write(fileOutputStream);
fileOutputStream.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
mapper接口:因为是测试,所以随便查了一张表
List selectAll();
mapper.xml:大家根据需求贴上自己的sql即可
下面是查到的数据,因为是测试用的数据库,所以数据很乱。
service接口:
List selectAll();
serviceimpl实现service:
@Override
public List selectAll() {
return sysFacilityResisterMapper.selectAll();
}
感觉自己属实有点啰嗦......不过还是希望给小白带来帮助,毕竟大家都是从小白一路走来的。
接着是controller:
/**
* @param
* @return
* @Author Lebron
* @Date 2019/8/28 15:32
* @Description 测试下载/导出 TODO
*/
@GetMapping("/export")
public void export() {
ZxExportExcel zxExportExcel = new ZxExportExcel();
XSSFWorkbook wb = zxExportExcel.getWorkBook();
Sheet sheet = wb.getSheetAt(0);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
Cell cellCheck;
List facilityResisters = sysFacilityResisterService.selectAll();
for (int i = 0; i < facilityResisters.size(); i++) {
SysFacilityResisterParam facilityResister = facilityResisters.get(i);
String facilityCode = facilityResister.getFacilityCode();
String item = facilityResister.getItem();
String facilityType = facilityResister.getFacilityType();
Date createTime = facilityResister.getStartTime();
String sysDate = TimeUtil.getSysDate(createTime);
String installSite = facilityResister.getInstallSite();
String installCoordinate = facilityResister.getInstallCoordinate();
String coordinate = facilityResister.getCoordinate();
String aName = facilityResister.getAName();
String uName = facilityResister.getUName();
Row rowCheck = sheet.createRow(4 + i);
cellCheck = rowCheck.createCell(0);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(facilityCode);
cellCheck = rowCheck.createCell(1);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(item);
cellCheck = rowCheck.createCell(2);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(facilityType);
cellCheck = rowCheck.createCell(3);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(sysDate);
cellCheck = rowCheck.createCell(4);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(installSite);
cellCheck = rowCheck.createCell(5);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(installCoordinate);
cellCheck = rowCheck.createCell(6);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(coordinate);
cellCheck = rowCheck.createCell(7);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(aName);
cellCheck = rowCheck.createCell(8);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(uName);
}
zxExportExcel.exportOutPutExcel("C:\\\\xinao\\\\poi\\\\download\\\\测试.xlsx", wb);
}
因为平时开发都是使用Postman调试接口,所以就写了一个controller,如果你正巧没有使用过此工具可以百度一下使用方法,毕竟这个工具也是程序猿必备,我建议你了解一下。或者自己写个main函数测试一下,这里我就不贴代码了,相信聪明的你一定OK!
最后就是调接口测试辣!接口地址:http://localhost:8080/xinao/api/sys/facilityresister/export
下面是我下载出的内容,虽然很丑但是凑合看吧,测试也懒得去优化,代码也写得很乱,大家在用的时候不要嫌弃。
好啦,关于导出/下载到这儿就结束了,我觉着只要大家有耐心就一定没问题的,作为程序员,轻易放弃可不是什么好习惯,有问题可以在评论处留言,方便大家共同讨论!
突然想起我还用了一个时间工具类,下面也把代码贴出来,就是简单的转化格式。时间格式根据你存储的格式而定,如果你没有加时分秒下面的工具类也去掉即可。
public class TimeUtil {
public static String getSysDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = formatter.format(date);
return time;
}
}