日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc导出excel并弹出下载框

發布時間:2025/1/21 c/c++ 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc导出excel并弹出下载框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近做grid列表相關數據導出到excel功能,根據自己選擇的列導出成excel 并且下載到本地。廢話不說 直接上關鍵代碼:

需要引入相關的包:

compile 'org.apache.poi:poi-ooxml:3.9'
compile 'org.apache.poi:poi:3.9'
compile 'org.apache.poi:poi-scratchpad:3.9'

這是我項目中gradle的配置。

1.js 代碼

1

2

window.location.href="export/exportExcel?ids="?+?this.selectedId;

//? 此處的selectedId 就是單選或者多選多列的id集合

2. controller 

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

import?com.fndsoft.bcis.model.WithdrawDeposit;

import?com.fndsoft.bcis.service.SettlementService;

import?com.fndsoft.bcis.utils.ExcelUtil;

import?org.springframework.beans.factory.annotation.Autowired;

import?org.springframework.stereotype.Controller;

import?org.springframework.web.bind.annotation.RequestMapping;

import?org.springframework.web.bind.annotation.RequestMethod;

import?org.springframework.web.bind.annotation.RequestParam;

import?org.springframework.web.bind.annotation.ResponseBody;

? ?

import?javax.servlet.ServletOutputStream;

import?javax.servlet.http.HttpServletResponse;

import?java.io.*;

import?java.text.SimpleDateFormat;

import?java.util.*;

? ?

/**

?* Created by bai on 2016/7/26.

?*/

@Controller

@RequestMapping(value =?"/export")

public?class?ExportExcelController {

??@Autowired

??private?SettlementService settlementService;

? ?

? ?

??/**

???* 根據多選的結算申請導出excel

???*

???* @return

???*/

??@RequestMapping(value =?"/exportExcel", method = RequestMethod.GET)

??@ResponseBody

??public?Map<String, Object> exportExcel(@RequestParam?Integer[] ids, HttpServletResponse response)?throws?IOException {

????String fileName =?new?Date().getTime() +?".xls";

????//填充projects數據

????List<WithdrawDeposit> projects = createData(ids);

????List<Map<String, Object>> list = createExcelRecord(projects);

????String columnNames[] = {"申請號",?"提現金額",?"申請時間",?"支付類型",?"賬號",?"狀態"};//列名

????String keys[] = {"applicationNo",?"amount",?"opTime",?"payType",?"accountNo",?"status"};//map中的key

????ByteArrayOutputStream os =?new?ByteArrayOutputStream();

????try?{

??????ExcelUtil.createWorkBook(list, keys, columnNames).write(os);

????}?catch?(IOException e) {

??????e.printStackTrace();

????}

????byte[] content = os.toByteArray();

????InputStream is =?new?ByteArrayInputStream(content);

????// 設置response參數,可以打開下載頁面

????response.reset();

????response.setContentType("application/vnd.ms-excel;charset=utf-8");

????response.setHeader("Content-Disposition",?"attachment;filename="+?new?String(fileName.getBytes(),?"iso-8859-1"));

????ServletOutputStream out = response.getOutputStream();

????BufferedInputStream bis =?null;

????BufferedOutputStream bos =?null;

????try?{

??????bis =?new?BufferedInputStream(is);

??????bos =?new?BufferedOutputStream(out);

??????byte[] buff =?new?byte[2048];

??????int?bytesRead;

??????// Simple read/write loop.

??????while?(-1?!= (bytesRead = bis.read(buff,?0, buff.length))) {

????????bos.write(buff,?0, bytesRead);

??????}

????}?catch?(final?IOException e) {

??????throw?e;

????}?finally?{

??????if?(bis !=?null)

????????bis.close();

??????if?(bos !=?null)

????????bos.close();

????}

????return?null;

??}

? ?

??private?List<WithdrawDeposit> createData(Integer[] ids) {<br><br>????//自己實現

????return?null;

??}

? ?

??private?List<Map<String, Object>> createExcelRecord(List<WithdrawDeposit> projects) {

????List<Map<String, Object>> listmap =?new?ArrayList<Map<String, Object>>();

????Map<String, Object> map =?new?HashMap<String, Object>();

????SimpleDateFormat format =?new?SimpleDateFormat("yyyy-MM-dd");

????map.put("sheetName",?"sheet1");

????listmap.add(map);

????WithdrawDeposit project;

????for?(int?j =?0; j < projects.size(); j++) {

??????project = projects.get(j);

??????Map<String, Object> mapValue =?new?HashMap<>();

??????mapValue.put("applicationNo", project.getApplicationNo());

??????mapValue.put("amount", project.getAmount());

??????mapValue.put("opTime", format.format(project.getOpTime()));

??????mapValue.put("payType", project.getPayType());

??????mapValue.put("accountNo", project.getAccountNo());

??????mapValue.put("status", project.getStatus());

??????listmap.add(mapValue);

????}

????return?listmap;

??}

}

3.excel導出幫助類

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

import?java.util.List;

import?java.util.Map;

? ?

import?org.apache.poi.hssf.usermodel.HSSFWorkbook;

import?org.apache.poi.ss.usermodel.Cell;

import?org.apache.poi.ss.usermodel.CellStyle;

import?org.apache.poi.ss.usermodel.Font;

import?org.apache.poi.ss.usermodel.IndexedColors;

import?org.apache.poi.ss.usermodel.Row;

import?org.apache.poi.ss.usermodel.Sheet;

import?org.apache.poi.ss.usermodel.Workbook;

? ?

/**

?* 導出Excel文檔工具類

?*

?* @author bai

?* @date 2016-7-25

?*/

public?class?ExcelUtil {

? ?

??/**

???* 創建excel文檔,

???*

???* @param list??????? 數據

???* @param keys??????? list中map的key數組集合

???* @param columnNames excel的列名

???*/

??public?static?Workbook createWorkBook(List<Map<String, Object>> list, String[] keys, String columnNames[]) {

????// 創建excel工作簿

????Workbook wb =?new?HSSFWorkbook();

????// 創建第一個sheet(頁),并命名

????Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString());

????// 手動設置列寬。第一個參數表示要為第幾列設;,第二個參數表示列的寬度,n為列高的像素數。

????for?(int?i =?0; i < keys.length; i++) {

??????sheet.setColumnWidth((short) i, (short) (35.7?*?150));

????}

? ?

????// 創建第一行

????Row row = sheet.createRow((short)?0);

? ?

????// 創建兩種單元格格式

????CellStyle cs = wb.createCellStyle();

????CellStyle cs2 = wb.createCellStyle();

? ?

????// 創建兩種字體

????Font f = wb.createFont();

????Font f2 = wb.createFont();

? ?

????// 創建第一種字體樣式(用于列名)

????f.setFontHeightInPoints((short)?10);

????f.setColor(IndexedColors.BLACK.getIndex());

????f.setBoldweight(Font.BOLDWEIGHT_BOLD);

? ?

????// 創建第二種字體樣式(用于值)

????f2.setFontHeightInPoints((short)?10);

????f2.setColor(IndexedColors.BLACK.getIndex());

? ?

????// 設置第一種單元格的樣式(用于列名)

????cs.setFont(f);

????cs.setBorderLeft(CellStyle.BORDER_THIN);

????cs.setBorderRight(CellStyle.BORDER_THIN);

????cs.setBorderTop(CellStyle.BORDER_THIN);

????cs.setBorderBottom(CellStyle.BORDER_THIN);

????cs.setAlignment(CellStyle.ALIGN_CENTER);

? ?

????// 設置第二種單元格的樣式(用于值)

????cs2.setFont(f2);

????cs2.setBorderLeft(CellStyle.BORDER_THIN);

????cs2.setBorderRight(CellStyle.BORDER_THIN);

????cs2.setBorderTop(CellStyle.BORDER_THIN);

????cs2.setBorderBottom(CellStyle.BORDER_THIN);

????cs2.setAlignment(CellStyle.ALIGN_CENTER);

????//設置列名

????for?(int?i =?0; i < columnNames.length; i++) {

??????Cell cell = row.createCell(i);

??????cell.setCellValue(columnNames[i]);

??????cell.setCellStyle(cs);

????}

????//設置每行每列的值

????for?(short?i =?1; i < list.size(); i++) {

??????// Row 行,Cell 方格 , Row 和 Cell 都是從0開始計數的

??????// 創建一行,在頁sheet上

??????Row row1 = sheet.createRow(i);

??????// 在row行上創建一個方格

??????for?(short?j =?0; j < keys.length; j++) {

????????Cell cell = row1.createCell(j);

????????cell.setCellValue(list.get(i).get(keys[j]) ==?null???" "?: list.get(i).get(keys[j]).toString());

????????cell.setCellStyle(cs2);

??????}

????}

????return?wb;

??}

? ?

}

   ?

執行之后會在你瀏覽器的下邊看到下載的文件直接雙擊打開就行。

總結

以上是生活随笔為你收集整理的springmvc导出excel并弹出下载框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。