我是靠谱客的博主 生动萝莉,这篇文章主要介绍EXCEL文件生成xlsx文件xls文件EXCEL文件写到磁盘,现在分享给大家,希望可以做个参考。

xlsx文件

复制代码
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
@RequestMapping("/test2023.action") @ResponseBody public String test2023(HttpServletRequest request, HttpServletResponse response) throws Exception { // 声明一个工作薄 Workbook workbook = new XSSFWorkbook(); String sheetName = "已配置人员"; // 生成一个sheet,名称为sheetname Sheet sheet = workbook.createSheet(sheetName); sheet.setDefaultColumnWidth(20); Row rowSheet1 = sheet.createRow(0); // 定义说明第1行 Row rowSheet2 = sheet.createRow(1); // 定义说明第2行 rowSheet1.setZeroHeight(true);// 隐藏第一行,参数代码 // 设置带*字体 Font headfont = workbook.createFont(); headfont.setColor(Font.COLOR_RED);// 变红 // 设置带*单元格格式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格文本格式 DataFormat format = workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("@")); cellStyle.setFont(headfont); // 设置不带*字体 Font headfont1 = workbook.createFont(); headfont1.setColor(Font.COLOR_NORMAL);// 黑 // 设置不带*单元格格式 CellStyle cellStyle1 = workbook.createCellStyle(); // 设置单元格文本格式 DataFormat format1 = workbook.createDataFormat(); cellStyle1.setDataFormat(format1.getFormat("@")); cellStyle1.setFont(headfont1); // 设置1-4列为文本格式 sheet.setDefaultColumnStyle(0, cellStyle1); sheet.setDefaultColumnStyle(1, cellStyle1); sheet.setDefaultColumnStyle(2, cellStyle1); sheet.setDefaultColumnStyle(3, cellStyle1); /* String[] doList = {"新增", "删除"}; DVConstraint constraint = DVConstraint.createExplicitListConstraint(doList); // 创建约束范围四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(2, 9999, 4, 4); // 绑定下拉框和作用区域 HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint); sheet.addValidationData(dataValidation);*/ String[] AUDITORS_VALUE = {"buCode", "auditors", "categoryCode", "brandCode", "operateType"}; String[] AUDITORS_KEY = {"*事业部编码", "*配置人工号", "四级类目编码 ", "品牌编码", "操作"}; int length = AUDITORS_VALUE.length; for (int a = 0; a < length; a++) { Cell celltempcode = rowSheet1.createCell(a); Cell celltempdesc = rowSheet2.createCell(a); if (AUDITORS_KEY[a].startsWith("*")) { celltempdesc.setCellStyle(cellStyle); } else { celltempdesc.setCellStyle(cellStyle1); } celltempcode.setCellValue(AUDITORS_VALUE[a]); celltempdesc.setCellValue(AUDITORS_KEY[a]); } String isIE = request.getHeader("user-agent"); downExcel(response, workbook, "审核人员配置模板.xlsx", "", isIE); return "111"; } public static void downExcel(HttpServletResponse response, Workbook workbook, String excelname, String newexcelname, String isIE) { try { if (isIE.toLowerCase().indexOf("msie") < 0) { // 解决中文excel文件名乱码问题,非IE newexcelname = new String(excelname.getBytes("UTF-8"), "ISO-8859-1"); } else { // 解决中文excel文件名乱码问题,IE newexcelname = URLEncoder.encode(excelname, "UTF-8"); } } catch (UnsupportedEncodingException e1) { log.error("UnsupportedEncodingException Error!", e1); } response.reset(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-disposition", "attachment; filename="" + newexcelname + """); // 实现下载 try { workbook.write(response.getOutputStream());// 实现输出 } catch (IOException e) { log.error("IOException Error!", e); } try { response.flushBuffer(); } catch (IOException e) { log.error("IOException Error!", e); } }

xls文件


复制代码
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
@RequestMapping("/test2023.action") @ResponseBody public String test2023(HttpServletRequest request, HttpServletResponse response) throws Exception { // 声明一个工作薄 Workbook workbook = new HSSFWorkbook(); String sheetName = "已配置人员"; // 生成一个sheet,名称为sheetname Sheet sheet = workbook.createSheet(sheetName); sheet.setDefaultColumnWidth(20); Row rowSheet1 = sheet.createRow(0); // 定义说明第1行 Row rowSheet2 = sheet.createRow(1); // 定义说明第2行 rowSheet1.setZeroHeight(true);// 隐藏第一行,参数代码 // 设置带*字体 Font headfont = workbook.createFont(); headfont.setColor(Font.COLOR_RED);// 变红 // 设置带*单元格格式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格文本格式 DataFormat format = workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("@")); cellStyle.setFont(headfont); // 设置不带*字体 Font headfont1 = workbook.createFont(); headfont1.setColor(Font.COLOR_NORMAL);// 黑 // 设置不带*单元格格式 CellStyle cellStyle1 = workbook.createCellStyle(); // 设置单元格文本格式 DataFormat format1 = workbook.createDataFormat(); cellStyle1.setDataFormat(format1.getFormat("@")); cellStyle1.setFont(headfont1); // 设置1-4列为文本格式 sheet.setDefaultColumnStyle(0, cellStyle1); sheet.setDefaultColumnStyle(1, cellStyle1); sheet.setDefaultColumnStyle(2, cellStyle1); sheet.setDefaultColumnStyle(3, cellStyle1); String[] doList = {"新增", "删除"}; DVConstraint constraint = DVConstraint.createExplicitListConstraint(doList); // 创建约束范围四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(2, 9999, 4, 4); // 绑定下拉框和作用区域 HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint); sheet.addValidationData(dataValidation); String[] AUDITORS_VALUE = {"buCode", "auditors", "categoryCode", "brandCode", "operateType"}; String[] AUDITORS_KEY = {"*事业部编码", "*配置人工号", "四级类目编码 ", "品牌编码", "操作"}; int length = AUDITORS_VALUE.length; for (int a = 0; a < length; a++) { Cell celltempcode = rowSheet1.createCell(a); Cell celltempdesc = rowSheet2.createCell(a); if (AUDITORS_KEY[a].startsWith("*")) { celltempdesc.setCellStyle(cellStyle); } else { celltempdesc.setCellStyle(cellStyle1); } celltempcode.setCellValue(AUDITORS_VALUE[a]); celltempdesc.setCellValue(AUDITORS_KEY[a]); } String isIE = request.getHeader("user-agent"); downExcel(response, workbook, "审核人员配置模板.xls", "", isIE); return "111"; } public static void downExcel(HttpServletResponse response, Workbook workbook, String excelname, String newexcelname, String isIE) { try { if (isIE.toLowerCase().indexOf("msie") < 0) { // 解决中文excel文件名乱码问题,非IE newexcelname = new String(excelname.getBytes("UTF-8"), "ISO-8859-1"); } else { // 解决中文excel文件名乱码问题,IE newexcelname = URLEncoder.encode(excelname, "UTF-8"); } } catch (UnsupportedEncodingException e1) { log.error("UnsupportedEncodingException Error!", e1); } response.reset(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-disposition", "attachment; filename="" + newexcelname + """); // 实现下载 try { workbook.write(response.getOutputStream());// 实现输出 } catch (IOException e) { log.error("IOException Error!", e); } try { response.flushBuffer(); } catch (IOException e) { log.error("IOException Error!", e); } }

EXCEL文件写到磁盘

复制代码
1
2
3
4
5
FileOutputStream fos = new FileOutputStream("d://aaa.xls"); workbook.write(fos); fos.close();

最后

以上就是生动萝莉最近收集整理的关于EXCEL文件生成xlsx文件xls文件EXCEL文件写到磁盘的全部内容,更多相关EXCEL文件生成xlsx文件xls文件EXCEL文件写到磁盘内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(92)

评论列表共有 0 条评论

立即
投稿
返回
顶部