工业级运维app手机api
xufenglei
2017-11-14 91e5d3d85c737b96b2c4a1994e2b861cc083453c
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
package com.moral.monitor.util;
 
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
 
import javax.servlet.http.HttpServletResponse;
 
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import net.sf.json.JSONObject;
 
@SuppressWarnings("rawtypes")
public class ExportExcelUtils {
 
    public static OutputStream exportData(HttpServletResponse response, String fileName, List list,String[][] exportColumn)
            throws Exception {
 
        // 文件名加后缀信
        String excelFileName = fileName + ".xls";
 
        OutputStream outputStream = response.getOutputStream();
        // 设定输出文件头
        response.setHeader("Content-Type", "application/force-download");
        // 把文件名转码使前台可以显示中文名
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + new String(excelFileName.getBytes("gb2312"), "ISO8859-1")
                + "\"");
        // 生成excel表格
        exportExcel(outputStream,list, getColumn(exportColumn),getName(exportColumn), fileName);
 
        return outputStream;
    }
 
    public static void exportExcel(OutputStream os, List list, String[] colTitles, String[] fieldNames, String fileName) throws Exception {
        WritableWorkbook wwb = Workbook.createWorkbook(os);
 
        createSheet(wwb, fileName, 0, list, colTitles, fieldNames);
 
        wwb.write();
        wwb.close();
    }
 
    private static void createSheet(WritableWorkbook wwb, String sheetName, int index, List list, String[] colTitles,
            String[] fieldNames)
            throws RowsExceededException, WriteException, UnsupportedEncodingException {
 
        WritableSheet ws = wwb.createSheet(sheetName, index);
        // 标题定义样式
        WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD);
        WritableCellFormat wcf = new WritableCellFormat(font);
        
        for (int i = 0; i < colTitles.length; i++) {
            String title = colTitles[i];
            ws.addCell(new Label(i, 0, title.split(",")[0], wcf));
            ws.setColumnView(i, Integer.valueOf(title.split(",")[1]));
        }
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = JSONObject.fromObject(list.get(i));
            for (int j = 0; j < colTitles.length; j++) {
                ws.addCell(new Label(j, i + 1, jsonObject.getString(fieldNames[j])));
            }
        }
    }
 
    /**
     * 获得导出 列标题 和 列宽度
     * 
     * @return
     */
    public static String[] getColumn(String[][] exportColumn) {
        if (exportColumn != null) {
            String[] result = new String[exportColumn.length];
            for (int i = 0; i < exportColumn.length; i++) {
                result[i] = exportColumn[i][0] + "," + exportColumn[i][1];
            }
            return result;
        } else {
            return new String[0];
        }
    }
 
    /**
     * 获得 表针对字段
     * 
     * @return
     */
    public static String[] getName(String[][] exportColumn) {
        if (exportColumn != null) {
            String[] result = new String[exportColumn.length];
            for (int i = 0; i < exportColumn.length; i++) {
                result[i] = exportColumn[i][2];
            }
            return result;
        } else {
            return new String[0];
        }
    }
 
}