(十一)SpringBoot导出excel文件
2021-06-15 11:05
标签:rri column 表格 ooxml create object c 生成 tst framework 创建core→utils→ExcelUtils 创建core→constant→ExcelConstant 输入localhost:8080/excel/test (十一)SpringBoot导出excel文件 标签:rri column 表格 ooxml create object c 生成 tst framework 原文地址:https://www.cnblogs.com/yui66/p/9632873.html一:添加POI依赖
二:创建Excel实体类
package com.example.demo.model;
import java.io.Serializable;
import java.util.List;
public class ExcelData implements Serializable {
private static final long serialVersionUID = 6133772627258154184L;
/**
* 表头
*/
private List
> rows;
/**
* 页签名称
*/
private String name;
public List
> getRows() {
return rows;
}
public void setRows(List
> rows) {
this.rows = rows;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三:创建表格工具类
package com.example.demo.core.utils;
import com.example.demo.model.ExcelData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import javax.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
public class ExcelUtils {
/**
* 使用浏览器选择路径下载
* @param response
* @param fileName
* @param data
* @throws Exception
*/
public static void exportExcel(HttpServletResponse response, String fileName, ExcelData data) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "utf-8"));
exportExcel(data, response.getOutputStream());
}
public static int generateExcel(ExcelData excelData, String path) throws Exception {
File f = new File(path);
FileOutputStream out = new FileOutputStream(f);
return exportExcel(excelData, out);
}
private static int exportExcel(ExcelData data, OutputStream out) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
int rowIndex = 0;
try {
String sheetName = data.getName();
if (null == sheetName) {
sheetName = "Sheet1";
}
XSSFSheet sheet = wb.createSheet(sheetName);
rowIndex = writeExcel(wb, sheet, data);
wb.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
//此处需要关闭 wb 变量
out.close();
}
return rowIndex;
}
/**
* 表不显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
// private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) {
// int rowIndex = 0;
// writeTitlesToExcel(wb, sheet, data.getTitles());
// rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
// autoSizeColumns(sheet, data.getTitles().size() + 1);
// return rowIndex;
// }
/**
* 表显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) {
int rowIndex = 0;
rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
autoSizeColumns(sheet, data.getTitles().size() + 1);
return rowIndex;
}
/**
* 设置表头
*
* @param wb
* @param sheet
* @param titles
* @return
*/
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List
> rows, int rowIndex) {
int colIndex;
Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
for (List
四:创建ExcelConstant
package com.example.demo.core.constant;
public class ExcelConstant {
/**
* 生成文件存放路径
*/
public static final String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\";
/**
* 表格默认名称
*/
public static final String FILE_NAME = "TEST.xls";
}
五:创建ExcelController
package com.example.demo.controller;
import com.example.demo.core.constant.ExcelConstant;
import com.example.demo.core.ret.RetResponse;
import com.example.demo.core.ret.RetResult;
import com.example.demo.core.ret.ServiceException;
import com.example.demo.core.utils.ExcelUtils;
import com.example.demo.model.ExcelData;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("excel")
public class ExcelController {
@Resource
private UserInfoService userInfoService;
@RequestMapping("/test")
public RetResult
> rows = new ArrayList();
for(int i = 0, length = list.size();i
> rows = new ArrayList();
for(int i = 0, length = list.size();i
七:测试
文章标题:(十一)SpringBoot导出excel文件
文章链接:http://soscw.com/index.php/essay/94149.html