cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.util;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
/**
 * @author YK 生成Execl
 */
public class ExeclUtil {
 
    /**
     * 将List<Map>格式的数据集转换为execl
     * @param title execl sheet的名字
     * @param file 生成execl后存放的路径
     * @param list 
     * 作者:杨凯
     */
    public static void list2Execl(String title, File file, List<Map> list) {
        try {
            WritableWorkbook book = Workbook.createWorkbook(file);
            WritableSheet sheet = book.createSheet(title, 0);
            
            if (list == null || list.size() == 0) {
                throw new RuntimeException("传入的list为null或空");
            }
            Map keyMap = list.get(0);
            
            Iterator iterator = keyMap.keySet().iterator();
            String[] keys = new String[keyMap.keySet().size()];
            int i=0;
            while (iterator.hasNext()) {
                String key = ConvertUtil.obj2Str(iterator.next());
                Label cell = new Label(i,0,key);
                sheet.addCell(cell);
                keys[i] = key;
                i++;
            }
            int rowIndex = 1;
            int columnIndex = 0;
            for (Map map : list) {
                for (String key : keys) {
                    Label cell = new Label(columnIndex,rowIndex,ConvertUtil.obj2Str(map.get(key)));
                    sheet.addCell(cell);
                    columnIndex ++ ;
                }
                columnIndex = 0;
                rowIndex ++ ;
            }
            book.write();
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
    
    /**
     * 将List<Map>格式的数据集转换为execl
     * @param title execl sheet的名字
     * @param list 
     * 作者:jxl
     */
    public static ByteArrayOutputStream list2Execl(String title, List<Map> list) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            WritableWorkbook book = Workbook.createWorkbook(baos);
            WritableSheet sheet = book.createSheet(title, 0);
            
            if (list == null || list.size() == 0) {
                throw new RuntimeException("传入的list为null或空");
            }
            Map keyMap = list.get(0);
            
            
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); 
            WritableCellFormat titleFormat = new WritableCellFormat(wfont); 
            
            Iterator iterator = keyMap.keySet().iterator();
            String[] keys = new String[keyMap.keySet().size()];
            int i=0;
            while (iterator.hasNext()) {
                String key = ConvertUtil.obj2Str(iterator.next());
                Label cell = new Label(i,0,key,titleFormat);
                sheet.addCell(cell);
                keys[i] = key;
                i++;
            }
            int rowIndex = 1;
            int columnIndex = 0;
            
            
            
            
            for (Map map : list) {
                for (String key : keys) {
                    Label cell = new Label(columnIndex,rowIndex,ConvertUtil.obj2Str(map.get(key)));
                    sheet.addCell(cell);
                    columnIndex ++ ;
                }
                columnIndex = 0;
                rowIndex ++ ;
            }
            book.write();
            book.close();
            return baos;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public static void main(String[] args) {
        List<Map> list = new ArrayList<Map>();
        
        Map map1 = new HashMap();
        map1.put("name", "江小磊");
        map1.put("age", "20");
        
        Map map2 = new HashMap();
        map2.put("name", "江小磊1");
        map2.put("age", "21");
        
        list.add(map1);
        list.add(map2);
        
        list2Execl("test",list);
    }
}