本文实例为大家分享了java web将数据导出为Excel格式文件的具体代码,供大家参考,具体内容如下
1、jsp代码
<input type="button" class="btn btn-info" onclick="getVerExcel();" value="导出为Excel文件" />
2、js代码
?
|
1
2
3
4
|
function getVerExcel() {
window.location.href = '/pms/jsp/version/getPrdVerListExcel?page='
+ $("#getPage").html() + '&key=' + $("#select").val();
}
|
3、java代码
?
|
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/**
*
* Purpose :将产品版本列表导出为Excel文件
* @param req
* 请求
* @param resp
* 应答
* @param page
* 当前页数
* @param key
* 查询条件
* @return
*/
@RequestMapping("getPrdVerListExcel")
public void getExcel(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) {
// 设置文件的mime类型
resp.setContentType("application/vnd.ms-excel");
// 得到所有的数据
List<Version> verList = prdVersionSer.getAllPrdVersion(key);
// 若没有数据,则给用户提示
if (verList.size() == 0) {
req.setAttribute("getFileMsg", "没有符合条件的信息!");
req.setAttribute("select", key);
try {
req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 存储编码后的文件名
String name = "name";
// 存储文件名称
String n = "";
if (key != "") {
n = verList.get(0).getPrdName() + "的版本列表";
} else {
n = "产品版本列表";
}
try {
name = URLEncoder.encode(n, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
resp.setHeader("content-disposition",
"attachment;filename=" + name + ".xls;filename*=utf-8''" + name + ".xls");
System.out.println("key:" + key);
// 从session中删除saveExcelMsg属性
req.getSession().removeAttribute("saveExcelMsg");
// 定义一个输出流
ServletOutputStream sos = null;
// 创建一个工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 创建一个工作表
HSSFSheet sheet = null;
if (key != "") {
sheet = wb.createSheet(verList.get(0).getPrdName() + "的版本信息");
} else {
sheet = wb.createSheet("产品版本信息");
}
// 返回数据格式对象
// 从格式对象中获取对应日期格式的编号,如果格式不存在,该方法会为它生成新的编号
HSSFDataFormat format = wb.createDataFormat();
short dfNum = format.getFormat("yyyy-mm-dd");
// 创建样式对象
CellStyle style = wb.createCellStyle();
// 设置数据格式
style.setDataFormat(dfNum);
// 创建第一行(表格标题)
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
if (key != "") {
cell.setCellValue(verList.get(0).getPrdName() + "的产品版本列表");
} else {
cell.setCellValue("产品版本列表");
}
// 创建第二行(表头)
row = sheet.createRow(1);
cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("序号");
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("产品名称");
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本号");
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("发布日期");
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本类型");
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本描述");
int num = 1;
// 遍历输出verList中的数据,将其存入Excel中
for (int i = 0; i < verList.size(); i++) {
row = sheet.createRow(i + 2);
// 写入序号
cell = row.createCell(0, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(num);
num++;
// 写入产品名称
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getPrdName());
// 写入版本号
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerNo());
// 写入发布日期(日期格式做处理)
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
// 将样式应用于单元格
cell.setCellStyle(style);
cell.setCellValue(verList.get(i).getVerDate());
// 写入版本类型
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerType());
// 写入版本描述
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerDesc());
}
try {
// 保存到文件中
sos = resp.getOutputStream();
wb.write(sos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sos != null) {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Laravel执行migrate命令提示:No such file or directory的解决方法
2025-05-29 43 -
2025-05-27 88
-
2025-05-26 65
-
2025-05-26 77
-
2025-05-27 84
热门评论

