ExcelUtils.java
4.5 KB
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
147
package com.jevon.utils;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* @author chen
* @version 1.0
* @date 2019/10/16 0016 18:46
*/
public class ExcelUtils {
//各个列的表头
private List<String> heardList;
//各个列的元素key值
private List<String> heardKey;
//需要填充的数据信息
private List<Map<String,String>> data;
//工作表
private String sheetName = "模板";
public List<String> getHeardList() {
return heardList;
}
public void setHeardList(List<String> heardList) {
this.heardList = heardList;
}
public List<String> getHeardKey() {
return heardKey;
}
public void setHeardKey(List<String> heardKey) {
this.heardKey = heardKey;
}
public List<Map<String, String>> getData() {
return data;
}
public void setData(List<Map<String, String>> data) {
this.data = data;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
/**
* 开始导出数据信息
*
*/
public byte[] exportExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
//检查参数配置信息
checkConfig();
//创建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet wbSheet = wb.createSheet(this.sheetName);
//在第0行创建rows
HSSFRow row = wbSheet.createRow((int)0);
//设置列头元素
HSSFCell cellHead = null;
for (int i = 0; i < heardList.size(); i++) {
cellHead = row.createCell(i);
cellHead.setCellValue(heardList.get(i));
}
//开始写入实体数据信息
int a = 1;
for (int i = 0; i < data.size(); i++) {
HSSFRow roww = wbSheet.createRow((int) a);
Map map = data.get(i);
HSSFCell cell = null;
for (int j = 0; j < heardKey.size(); j++) {
cell = roww.createCell(j);
Object valueObject = map.get(heardKey.get(j));
String value = null;
if (valueObject == null) {
valueObject = "";
}
if (valueObject instanceof String) {
//取出的数据是字符串直接赋值
value = (String) map.get(heardKey.get(j));
} else if (valueObject instanceof Integer) {
//取出的数据是Integer
value = String.valueOf(((Integer) (valueObject)).floatValue());
} else if (valueObject instanceof BigDecimal) {
//取出的数据是BigDecimal
value = String.valueOf(((BigDecimal) (valueObject)).floatValue());
} else {
value = valueObject.toString();
}
cell.setCellValue(Strings.isNullOrEmpty(value) ? "" : value);
}
a++;
}
//导出数据
try {
//设置Http响应头告诉浏览器下载这个附件
response.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls");
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
return wb.getBytes();
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException("导出Excel出现严重异常,异常信息:" + ex.getMessage());
}
}
/**
* 检查数据配置问题
*
* @throws IOException 抛出数据异常类
*/
protected void checkConfig() throws IOException {
if (heardKey == null || heardList.size() == 0) {
throw new IOException("列名数组不能为空或者为NULL");
}
if (StringUtils.isBlank(sheetName)) {
throw new IOException("工作表表名不能为NULL");
}
}
}