版权声明:版权属 z_xuewen 所有 ©胖子牛逼 https://blog.csdn.net/z_xuewen/article/details/82151584
相信很多写后端的小伙伴都会遇到需要导出Excel表格是需求,产品经理说:那个某某某,需要把系统的数据自动导出表格,方便统计。说句实话,这个功能确实很方便管理员整理数据,而且在后端的工作中也经常需要使用此功能,下面我们使用 apache的poi进行Excel表格的构建及导出数据。
首先,需要依赖,maven引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
导入依赖完成后就可以写逻辑了,先写几个需要用到的方法
创建表头
// 创建表头
private void createTitle(HSSFWorkbook workbook, HSSFSheet sheet) {
HSSFRow row = sheet.createRow(0);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(1, 20 * 256);
sheet.setColumnWidth(2, 20 * 256);
sheet.setColumnWidth(3, 20 * 256);
sheet.setColumnWidth(4, 70 * 256);
sheet.setColumnWidth(5, 20 * 256);
sheet.setColumnWidth(6, 20 * 256);
sheet.setColumnWidth(6, 30 * 256);
//设置为居中加粗
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
font.setFontName("微软雅黑");
// 1px = 20 short,此处相当于 14px
font.setFontHeight((short) 280);
font.setColor(Font.COLOR_RED);
// 表头 水平居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
HSSFCell cell;
// 表头列数 0 开始 属于第一列
cell = row.createCell(0);
// 表头名称
cell.setCellValue("ID");
// 表头样式
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("userName");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("FirstName");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("LastName");
cell.setCellStyle(style);
}
需要注意的是 cell 每次设置样式的时候都需要重新 cell.setCellStyle 否则只有第一个有效
生成 Excel 文件的方法
/**
* 生成 Excel 文件
*
* @param filename
* @param workbook
* @throws Exception
*/
protected void buildExcelFile(String filename, HSSFWorkbook workbook) throws Exception {
FileOutputStream fos = new FileOutputStream(filename);
workbook.write(fos);
fos.flush();
fos.close();
}
往浏览器下载 Excel
/**
* 往客户浏览器下载Excel
*
* @param filename
* @param workbook
* @param response
* @throws Exception
*/
protected void buildExcelDocument(String filename, HSSFWorkbook workbook, HttpServletResponse response) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(filename, "utf-8"));
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
上面三个需要用到的方法已经封装好了,接下来就在接口中写逻辑,插入数据,生成表格,下载表格就可以了。具体的实现如下的 controller 代码
@RequestMapping(value = "exportExcel")
@ResponseBody
public String downExcel(String fileName, HttpServletResponse response) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("统计表");
createTitle(workbook, sheet);
// 设置内容 列样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontName("微软雅黑");
// 相当于 12px,1 px = 20 short
font.setFontHeight((short) 240);
style.setFont(font);
// 设置 水平、垂直居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
int rowNum = 1;
// 表示创建第一行
HSSFRow row = sheet.createRow(rowNum);
// 设置行高
row.setHeight((short) 600);
// 设置第一列内容
HSSFCell cell = row.createCell(0);
cell.setCellValue("123");
cell.setCellStyle(style);
// 设置第二列内容
HSSFCell cell = row.createCell(1);
cell.setCellValue("456");
cell.setCellStyle(style);
try {
buildExcelFile(fileName, workbook);
buildExcelDocument(fileName, workbook, response);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
以上主要的功能就已经完成了,访问 exportExcel 接口就会自动下载 创建好的 Excel 表格了。
需要注意的是,插入数据时,一般是需要使用循环进行多行插入,在循环尾处进行 rowNum++,进行增加行数操作。如有不懂,请留言,楼主看到会回复哦~嘻嘻