Java导出(动态导出,表头和Value动态获取)
项⽬上业务需求,导出数据为动态数据,没有固定表头,没有固定值,每个表单下导出得数据都不同,普通导出⽆法实现,下⾯我就给⼤家展⽰我得做法:
下⾯是我的Controller写法:
注意的是:根据Hashmap获取不同数据在根据key value区分开,从新拼接,主要是看我的⼯具类,⼤家可以学习,希望能帮助你们‘
1 /**
2 * 表单导出
3 *
4 * @param response
5 * @param export
6 * @param request
7 */
8 @RequestMapping("/fForm/toExportExcel")
9 public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
10 try {
11 Map<String, Object> paramMap = vertRequestToMap(request);
12 Long formId = getLong(paramMap, "formId");
13 // 姓名⼿机号条件查询
14 String condition = getString(paramMap, "condition");
15 // 表头
16 ArrayList<String> excelHeader = new ArrayList<String>();
17 List<Map<String, Object>> list1 = fFormService.findFFromFliedConfig(formId);
18 excelHeader.add("序号");
19 for (Map<String, Object> map : list1) {
20 String showName = ("showName").toString();
21 excelHeader.add(showName);
22 }
23 excelHeader.add("提交时间");
24 String[] arrString = (String[]) Array(new String[excelHeader.size()]);
25 // 数据
26 List<Map<String, Object>> fromDataList = fFormService.findFFormData1(formId, condition);
27 List<Object[]> dataList = new ArrayList<Object[]>();
28 ArrayList<String> datal = null;
29 for (Map<String, Object> map : fromDataList) {
30 datal = new ArrayList<String>();
31 datal.("id").toString());
32 if (("sex") == null) {
33 map.put("sex", "");
34 } else if (("sex").equals("1")) {
35 map.put("sex", "男");
36 } else if (("sex").equals("2")) {
37 map.put("sex", "⼥");
38 }
39 for (Map<String, Object> map1 : list1) {
40 String dataName = String.("dataName"));
41 String data = String.(dataName));
42 if (data == "null") {
43 datal.add("");
44 } else {
45 datal.add(data);
46 }
47 }
48 SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm :ss");
49 datal.add(String.valueOf(s.("commit_time"))));
50 Object[] array = Array(new String[datal.size()]);
51 dataList.add(array);
52 }
53 portExcel(request, response, "表单导出", arrString, dataList, "yyyy-MM-dd HH:mm :ss");
54 } catch (
55
56 Exception e) {
57 e.printStackTrace();
58 }
59 }
导出⼯具类:
1/**
1/**
2 * 动态导出⼯具(根据数据列动态获取集合中数据)
3 *
4 * @author v_guoyunlong
5 *
6 */
7public class ExportExcel {
8
9 /**
10 *
11 *
12 * @param fileName ⽂件名
13 * @param headers 表格属性列名数组
14 * @param dataset 需要显⽰的数据集合
15 * @param pattern 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"
16 * @param file1
17 * @param zc2
18 * @param file
19 * @param file
20 * @throws Exception
21 */
22 @SuppressWarnings("deprecation")
23 public static HSSFWorkbook exportExcel(HttpServletRequest request, HttpServletResponse response, String fileName,
24 String[] headers, List<Object[]> dataset, String pattern) throws Exception {
25 // 设置请求
26 response.setHeader("content-disposition",
27 String.format("attachment;filename*=utf-8'zh_cn'%s.xls", de(fileName, "utf-8")));
28 response.setContentType("application/msexcel;charset=UTF-8");
29 // 创建⼀个⼯作薄
30 HSSFWorkbook workbook = new HSSFWorkbook();
31 // ⽣成⼀个表格
32 HSSFSheet sheet = ateSheet("表单导出");
33 // 产⽣表格标题⾏
34 HSSFRow row = ateRow(0);
35 for (short i = 0; i < headers.length; i++) {
36 HSSFCell cell = ateCell(i);
37 HSSFRichTextString text = new HSSFRichTextString(headers[i]);
38 cell.setCellValue(text);
39 }
40
41 // 遍历集合数据,产⽣数据⾏
42 Iterator<Object[]> it = dataset.iterator();
43 int index = 0;
44 while (it.hasNext()) {
45 index++;
46 // 从第1⾏开始创建
47 row = ateRow(index);
48 Object[] obj = (Object[]) it.next();
49 for (short i = 0; i < obj.length; i++) {
50 HSSFCell cell = ateCell(i);
51 Object value = obj[i];
52 String textValue = null;
53 if (!"".equals(value) && value != null) {
54 if (value instanceof Integer) {
55 int intValue = (Integer) value;
56 cell.setCellValue(intValue);
57 } else if (value instanceof Float) {
58 float fValue = (Float) value;
59 cell.setCellValue(fValue);
60 } else if (value instanceof Double) {
61 double dValue = (Double) value;
62 cell.setCellValue(dValue);
63 } else if (value instanceof Long) {
64 long longValue = (Long) value;
65 cell.setCellValue(longValue);
66 } else if (value instanceof Date) {
66 } else if (value instanceof Date) {
67 Date date = (Date) value;
68 if (null == pattern || pattern.equals("")) {
69 pattern = "yyyy-MM-dd";
70 }
71 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
72 textValue = sdf.format(date);
73 cell.setCellValue(textValue);
74 } else {
75 // 其它数据类型都当作字符串简单处理
76 textValue = String();
77 // 设置单元格的值
78 cell.setCellValue(textValue);
79 }
80 } else {
81 cell.setCellValue("");
82 }
83 }
84
85 }
86
87 OutputStream outputStream = OutputStream();// 打开流
88 // ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
89 // // 压缩包名
90 // String fileNames = "表单压缩⽂件" + ".zip";
91 // response.setContentType("application/octet-stream ");
92 // // 表⽰不能⽤浏览器直接打开
93 // response.setHeader("Connection", "close");
94 // // 告诉客户端允许断点续传多线程连接下载
95 // // response.setHeader("Accept-Ranges", "bytes");
96 // response.setHeadear("Content-Disposition", "attachment;filename=" + de(fileNames, "utf-8"));
97 // response.setCharacterEncoding("UTF-8");
98 // // 创建⼀个zip实体
99 // ZipEntry entry = new ZipEntry(fileName + ".xls");
100 // zipOutputStream.putNextEntry(entry);
101 // workbook.write(zipOutputStream);
102 // // 刷新关闭输出流
103 // zipOutputStream.flush();
104 // zipOutputStream.close();
105 // 让列宽随着导出的列长⾃动适应
106 for (int colNum = 0; colNum < headers.length; colNum++) {
107 int columnWidth = ColumnWidth(colNum) / 256;
108 for (int rowNum = 0; rowNum < LastRowNum(); rowNum++) {
109 HSSFRow currentRow;
110 // 当前⾏未被使⽤过
111 if (Row(rowNum) == null) {
112 currentRow = ateRow(rowNum);
113 } else {
114 currentRow = Row(rowNum);
115 }
116 if (Cell(colNum) != null) {
117 HSSFCell currentCell = Cell(colNum);
118 if (CellType() == HSSFCell.CELL_TYPE_STRING) {
119 int length = StringCellValue() != null
120 ? StringCellValue().getBytes().length : 10;
121 if (columnWidth < length) {
122 columnWidth = length;
123 }
124 }
125 }
126 }
127 if (colNum == 0) {
128 sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
129 } else {
130 sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
131 }
131 }
java valueof132 }
133 // HSSFWorkbook写⼊流
134 workbook.write(outputStream);
135 // HSSFWorkbook关闭
136 workbook.close();
137 // 刷新流
138 outputStream.flush();
139 // 关闭流
140 outputStream.close();
141 return workbook;
142 }
143
最后说的是,还是根据⾃⼰项⽬中的业务需求去写,⼯具类是通⽤,谢谢
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论