国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot實現excel文件生成和下載

SpringBoot實現excel文件生成和下載

2021-08-06 12:12shengshenglalala Java教程

這篇文章主要為大家詳細介紹了SpringBoot實現excel文件生成和下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用SpringBoot實現excel生成和下載,生成模板如下

SpringBoot實現excel文件生成和下載

controller

?
1
2
3
4
5
6
7
8
9
10
@RequestMapping(value = { "/downloadExcelTemplate" }, method = RequestMethod.GET)
 public String downloadExcelTemplate(HttpSession httpSession, HttpServletResponse response) {
 try {
  dealExcelService.downloadExcelTemplate(response);
  return "success";
 } catch (Exception e) {
  logger.error("downloadExcelTemplate_error", e);
  return "failure";
 }
}

service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public void downloadExcelTemplate(HttpServletResponse response) throws Exception {
 //文件名
 SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMddHHmm");
 String fileName = new String(("文件名" + format3.format(new Date()) + "導入模板").getBytes(), "ISO8859_1");
 //配置請求頭
 ServletOutputStream outputStream = response.getOutputStream();
 // 組裝附件名稱和格式
 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
 // 創建一個workbook 對應一個excel應用文件
 XSSFWorkbook workBook = new XSSFWorkbook();
 // 在workbook中添加一個sheet,對應Excel文件中的sheet
 XSSFSheet sheet = workBook.createSheet("模板");
 ExportUtil exportUtil = new ExportUtil(workBook, sheet);
 XSSFCellStyle headStyle = exportUtil.getHeadStyle();
 XSSFCellStyle bodyStyle = exportUtil.getBodyStyle2();
 // 構建表頭
 XSSFRow headRow = ExportUtil.createRow(sheet, 0);
 XSSFCell cell;
 
 String[] titles = {"表頭一", "表頭二", "表頭三"};
 int index = 0;
 for (String title : titles) {
  cell = ExportUtil.createCell(headRow, index);
  cell.setCellStyle(headStyle);
  cell.setCellValue(title);
  index++;
 }
 
 try {
  workBook.write(outputStream);
  outputStream.flush();
  outputStream.close();
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  try {
  outputStream.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
}

ExportUtil導出工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.shengsheng.utils;
 
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
 
/**
 * excel 表格導出工具類
 *
 * @author shengshenglalala
 */
public class ExportUtil {
 private XSSFWorkbook wb;
 
 private XSSFSheet sheet;
 
 /**
 * @param wb
 * @param sheet
 */
 public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet) {
 this.wb = wb;
 this.sheet = sheet;
 }
 
 /**
 * 合并單元格后給合并后的單元格加邊框
 *
 * @param region
 * @param cs
 */
 public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs) {
 
 int toprowNum = region.getFirstRow();
 for (int i = toprowNum; i <= region.getLastRow(); i++) {
  XSSFRow row = sheet.getRow(i);
  for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
  XSSFCell cell = row.getCell(j);
  cell.setCellStyle(cs);
  }
 }
 }
 
 /**
 * 設置表頭的單元格樣式
 *
 * @return
 */
 public XSSFCellStyle getHeadStyle() {
 // 創建單元格樣式
 XSSFCellStyle cellStyle = wb.createCellStyle();
 // // 設置單元格的背景顏色為淡藍色
 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
 cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
 // 設置單元格居中對齊
 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
 // 設置單元格垂直居中對齊
 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
 // 創建單元格內容顯示不下時自動換行
 // cellStyle.setWrapText(true);
 // 設置單元格字體樣式
 XSSFFont font = wb.createFont();
 // 設置字體加粗
 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
 font.setFontName("宋體");
 // font.setFontHeight((short) 200);
 cellStyle.setFont(font);
 // 設置單元格邊框為細線條
// cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
 return cellStyle;
 }
 
 /**
 * 設置表體的單元格樣式
 *
 * @return
 */
 public XSSFCellStyle getBodyStyle2() {
 // 創建單元格樣式
 // 創建單元格樣式
 XSSFCellStyle cellStyle = wb.createCellStyle();
 // 創建單元格內容顯示不下時自動換行
 // cellStyle.setWrapText(true);
 // 設置單元格字體樣式
 XSSFFont font = wb.createFont();
 // 設置字體加粗
 // font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
 font.setFontName("宋體");
 font.setFontHeight((short) 200);
 font.setColor(HSSFColor.BLACK.index);
 cellStyle.setFont(font);
 // 設置單元格邊框為細線條
 return cellStyle;
 }
 
 /**
 * 沒有行,就創建行
 *
 * @param sheet
 * @param index
 * @return
 */
 public static XSSFRow createRow(XSSFSheet sheet, Integer index) {
 XSSFRow row = sheet.getRow(index);
 if (row == null) {
  return sheet.createRow(index);
 }
 return row;
 }
 
 /**
 * 如果沒有列,就創建列
 *
 * @param row
 * @param index
 * @return
 */
 public static XSSFCell createCell(XSSFRow row, Integer index) {
 XSSFCell cell = row.getCell(index);
 if (cell == null) {
  return row.createCell(index);
 }
 return cell;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/shengshenglalalala/article/details/113744729

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区二区高清视频 | 欧美区 日韩区 | 精品久久久久久久久久久久久久 | 午夜成人免费电影 | 午夜精品福利在线观看 | 欧美日本免费一区二区三区 | 亚洲一区二区在线播放 | 97操视频 | 一级黄色影视 | 龙珠z国语291集普通话 | 亚洲一区二区三区在线免费观看 | 日韩欧美国产精品 | 中文字幕av在线 | 久久精品99国产精品日本 | 在线观看成人高清 | 亚洲一区二区三区四区的 | 激情综合网址 | 免费观看一区二区三区 | 中文字幕在线免费视频 | 一级片大片 | 国产综合久久 | 久久伦理电影网 | 久久久午夜爽爽一区二区三区三州 | 午夜影院免费观看视频 | 久久精品一区二区三区不卡牛牛 | 精品护士一区二区三区 | 中文字幕在线三区 | 欧美电影网站 | 久久精选| 久久久成人av| 四虎在线视频 | 黄色一级毛片 | 国产精品久久久久久久美男 | 日韩在线免费观看网站 | 日韩av电影在线观看 | 午夜影院在线观看 | 日本1区2区 | 日韩在线不卡一区 | 国产一区自拍视频 | 黑人xxx视频 | www.中文字幕 |