controller
@GetMapping("/download/excel")
@Operation(summary = "下载技术引入Excel模板")
public void downloadTemplateExcel(HttpServletResponse response) {
technologyService.getTechnologyExcelModel(response);
}
service
public void getTechnologyExcelModel(HttpServletResponse response) {
List technologyImportExcelDtoList = getTechnologyImportExcelVOList();
File file;
try {
//使用临时文件
file = File.createTempFile("开源技术导入模板", ".xlsx");
FileOutputStream fileOutputStream = new FileOutputStream(file);
ExportParams params = new ExportParams("表格标题", "Sheet1", ExcelType.XSSF);
String tipsMessage = "开源技术导入需遵循以下规则:xxx";
params.setTitle(tipsMessage);
Workbook workbook = ExcelExportUtil.exportExcel(params, TechnologyImportExcelDto.class, technologyImportExcelDtoList);
// 获取Sheet对象
Sheet sheet = workbook.getSheetAt(0); // 只有一个Sheet
// 设置标题行样式
Row titleRow = sheet.getRow(0); // 标题在第一行
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.LEFT); // 设置水平对齐方式为左对齐
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直对齐方式为居中
titleStyle.setWrapText(true); // 设置文本自动换行
// 设置边框
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
// 设置标题行每个单元格的样式
for (Cell cell : titleRow) {
cell.setCellStyle(titleStyle);
}
// 设置行高
titleRow.setHeightInPoints(titleRow.getHeightInPoints() * 4); // 设置为五行字的高度
// 设置字体加粗
Font font = workbook.createFont();
font.setBold(true);
titleStyle.setFont(font);
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
//下载,读取文件流并将其添加到response的输出流中,同时设置响应的Header字段
try (InputStream inputStream = new FileInputStream(file);
//获取r服务器托管网esponse的输出流对象
OutputStream outputStream = response.getOutputStream()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
} catch (Exception e) {
throw new RuntimeException();
} finally {
//清除文件
file.delete();
}
}
@NotNull
private static List getTechnologyImportExcelVOList() {
List technologyImportExcelDtoList服务器托管网 = new ArrayList();
TechnologyImportExcelDto technologyImportExcelDto = new TechnologyImportExcelDto(
"系统软件/固件",
"模板",
"x",
"1.0.0",
"GPL-2.0",
"http://xx.com",
"5",
"2023-03-15 14:17:02",
"http://download.com",
"描述内容");
technologyImportExcelDtoList.add(technologyImportExcelDto);
return technologyImportExcelDtoList;
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
C++ 再谈谈注册(本质是建立映射)与回调 在之前的博文中, 我们探讨过映射的重要作用, 请直接看:http://blog.csdn.net/stpeace/article/details/39452203, 在那篇文章中, 我们是用STL中的map来做的…