|
1 | 1 | package cn.netbuffer.jfinal_bootstrap_table.util;
|
2 | 2 |
|
| 3 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 4 | +import org.apache.poi.ss.usermodel.*; |
| 5 | + |
3 | 6 | import java.io.FileOutputStream;
|
4 | 7 | import java.io.IOException;
|
5 |
| -import java.util.HashMap; |
6 | 8 | import java.util.Iterator;
|
7 | 9 | import java.util.List;
|
8 | 10 | import java.util.Map;
|
9 | 11 | import java.util.Set;
|
10 | 12 |
|
11 |
| -import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
12 |
| -import org.apache.poi.ss.usermodel.Cell; |
13 |
| -import org.apache.poi.ss.usermodel.CellStyle; |
14 |
| -import org.apache.poi.ss.usermodel.CreationHelper; |
15 |
| -import org.apache.poi.ss.usermodel.Font; |
16 |
| -import org.apache.poi.ss.usermodel.Hyperlink; |
17 |
| -import org.apache.poi.ss.usermodel.IndexedColors; |
18 |
| -import org.apache.poi.ss.usermodel.Row; |
19 |
| -import org.apache.poi.ss.usermodel.Sheet; |
20 |
| -import org.apache.poi.ss.usermodel.Workbook; |
21 |
| -import org.apache.poi.ss.util.CellUtil; |
22 |
| - |
23 | 13 | public class POIExcelUtil {
|
24 |
| - public static void exec(String path) { |
25 |
| - Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook(); |
26 |
| - CreationHelper createHelper = wb.getCreationHelper(); |
27 |
| - Sheet sheet = wb.createSheet("用户信息"); |
28 |
| - // 冻结该行,使其无法移动 |
29 |
| - sheet.createFreezePane(0, 1, 0, 1); |
30 |
| - // Header header = sheet1.getHeader(); |
31 |
| - // header.setCenter("Center Header"); |
32 |
| - // header.setLeft("Left Header"); |
33 |
| - // header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + |
34 |
| - // HSSFHeader.fontSize((short) 16) + |
35 |
| - // "Right w/ Stencil-Normal Italic font and size 16"); |
36 |
| - Row row = sheet.createRow((short) 0); |
37 |
| - row.setHeightInPoints(30); |
38 |
| - Cell cell = row.createCell(0); |
39 |
| - CellStyle cellStyle = wb.createCellStyle(); |
40 |
| - cellStyle.setAlignment(CellStyle.ALIGN_CENTER); |
41 |
| - cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
42 |
| - cell.setCellValue("用户昵称"); |
43 |
| - cell.setCellStyle(cellStyle); |
44 |
| - row.createCell(1).setCellValue("用户性别"); |
45 |
| - |
46 |
| - CellStyle hlink_style = wb.createCellStyle(); |
47 |
| - Font hlink_font = wb.createFont(); |
48 |
| - hlink_font.setUnderline(Font.U_SINGLE); |
49 |
| - hlink_font.setColor(IndexedColors.BLUE.getIndex()); |
50 |
| - hlink_style.setFont(hlink_font); |
51 |
| - hlink_style.setAlignment(CellStyle.ALIGN_CENTER); |
52 |
| - hlink_style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
53 |
| - Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); |
54 |
| - link.setAddress("http://poi.apache.org/"); |
55 |
| - cell.setHyperlink(link); |
56 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM); |
57 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM); |
58 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM); |
59 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM); |
60 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.TOP_BORDER_COLOR,IndexedColors.RED.getIndex()); |
61 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex()); |
62 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.LEFT_BORDER_COLOR,IndexedColors.RED.getIndex()); |
63 |
| - CellUtil.setCellStyleProperty(cell, wb, CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex()); |
64 |
| - row.createCell(2).setCellValue( |
65 |
| - createHelper.createRichTextString("This is a string用户年龄")); |
66 |
| - row.createCell(3).setCellValue("用户手机"); |
67 |
| - row.createCell(4).setCellValue("收货地址"); |
68 |
| - row.createCell(5).setCellValue("注册时间"); |
69 |
| - sheet.autoSizeColumn(0); // adjust width of the first column |
70 |
| - sheet.autoSizeColumn(1); |
71 |
| - sheet.autoSizeColumn(2); |
72 |
| - sheet.autoSizeColumn(3); |
73 |
| - sheet.autoSizeColumn(4); |
74 |
| - |
75 |
| - FileOutputStream fileOut; |
76 |
| - try { |
77 |
| - fileOut = new FileOutputStream(path); |
78 |
| - wb.write(fileOut); |
79 |
| - fileOut.close(); |
80 |
| - } catch (IOException e) { |
81 |
| - e.printStackTrace(); |
82 |
| - } |
83 |
| - |
84 |
| - } |
85 |
| - |
86 |
| - public static void export(List<String> titles,List<Map<String, Object>> datas,String path) { |
87 |
| - Workbook wb = new HSSFWorkbook(); |
88 |
| - CreationHelper createHelper = wb.getCreationHelper(); |
89 |
| - Sheet sheet = wb.createSheet(titles.get(0)); |
90 |
| - // 冻结该行,使其无法移动 |
91 |
| - sheet.createFreezePane(0, 1, 0, 1); |
92 |
| - // Header header = sheet1.getHeader(); |
93 |
| - // header.setCenter("Center Header"); |
94 |
| - // header.setLeft("Left Header"); |
95 |
| - // header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + |
96 |
| - // HSSFHeader.fontSize((short) 16) + |
97 |
| - // "Right w/ Stencil-Normal Italic font and size 16"); |
98 |
| - Row row = sheet.createRow((short) 0); |
99 |
| - row.setHeightInPoints(30); |
100 |
| - int titleCount=titles.size(); |
101 |
| - Font titleFont=wb.createFont(); |
102 |
| - titleFont.setBold(true); |
103 |
| - titleFont.setColor(IndexedColors.AQUA.getIndex()); |
104 |
| - CellStyle cellStyle = wb.createCellStyle(); |
105 |
| - cellStyle.setAlignment(CellStyle.ALIGN_CENTER); |
106 |
| - cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
107 |
| - cellStyle.setFont(titleFont); |
108 |
| - for(int i=0;i<titleCount;i++){ |
109 |
| - Cell cell = row.createCell(i); |
110 |
| - cell.setCellValue(titles.get(i)); |
111 |
| - cell.setCellStyle(cellStyle); |
112 |
| - } |
113 |
| - CellStyle contentStyle = wb.createCellStyle(); |
114 |
| - cellStyle.setAlignment(CellStyle.ALIGN_CENTER); |
115 |
| - cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
116 |
| - int dataCount=datas.size(); |
117 |
| - for(int i=1;i<dataCount+1;i++){ |
118 |
| - Row rowIndex = sheet.createRow((short) i); |
119 |
| - Map<String, Object> data=datas.get(i-1); |
120 |
| - Set<String> dataSet=data.keySet(); |
121 |
| - Iterator<String> d=dataSet.iterator(); |
122 |
| - int k=0; |
123 |
| - while (d.hasNext()) { |
124 |
| - Cell cell = rowIndex.createCell(k); |
125 |
| - cell.setCellStyle(contentStyle); |
126 |
| - String key = (String) d.next(); |
127 |
| - cell.setCellValue(data.get(key)==null?"":data.get(key).toString()); |
128 |
| - k++; |
129 |
| - } |
130 |
| - } |
131 |
| -// CellStyle hlink_style = wb.createCellStyle(); |
132 |
| -// Font hlink_font = wb.createFont(); |
133 |
| -// hlink_font.setUnderline(Font.U_SINGLE); |
134 |
| -// hlink_font.setColor(IndexedColors.BLUE.getIndex()); |
135 |
| -// hlink_style.setFont(hlink_font); |
136 |
| -// hlink_style.setAlignment(CellStyle.ALIGN_CENTER); |
137 |
| -// hlink_style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
138 |
| -// Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); |
139 |
| -// link.setAddress("http://poi.apache.org/"); |
140 |
| -// cell.setHyperlink(link); |
141 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM); |
142 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM); |
143 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM); |
144 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM); |
145 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.TOP_BORDER_COLOR,IndexedColors.RED.getIndex()); |
146 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex()); |
147 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.LEFT_BORDER_COLOR,IndexedColors.RED.getIndex()); |
148 |
| -// CellUtil.setCellStyleProperty(cell, wb, CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex()); |
149 |
| -// row.createCell(2).setCellValue( |
150 |
| -// createHelper.createRichTextString("This is a string用户年龄")); |
151 |
| -// row.createCell(3).setCellValue("用户手机"); |
152 |
| -// row.createCell(4).setCellValue("收货地址"); |
153 |
| -// row.createCell(5).setCellValue("注册时间"); |
154 |
| -// sheet.autoSizeColumn(0); // adjust width of the first column |
155 |
| -// sheet.autoSizeColumn(1); |
156 |
| -// sheet.autoSizeColumn(2); |
157 |
| -// sheet.autoSizeColumn(3); |
158 |
| -// sheet.autoSizeColumn(4); |
159 |
| - FileOutputStream fileOut; |
160 |
| - try { |
161 |
| - fileOut = new FileOutputStream(path); |
162 |
| - wb.write(fileOut); |
163 |
| - fileOut.close(); |
164 |
| - } catch (IOException e) { |
165 |
| - e.printStackTrace(); |
166 |
| - } |
167 | 14 |
|
168 |
| - } |
| 15 | + public static void export(List<String> titles, List<Map<String, Object>> datas, String path) { |
| 16 | + Workbook wb = new HSSFWorkbook(); |
| 17 | + Sheet sheet = wb.createSheet(titles.get(0)); |
| 18 | + // 冻结该行,使其无法移动 |
| 19 | + sheet.createFreezePane(0, 1, 0, 1); |
| 20 | +// Header header = sheet.getHeader(); |
| 21 | +// header.setCenter("Center Header"); |
| 22 | +// header.setLeft("Left Header"); |
| 23 | +// header.setRight(HSSFHeader.font("Stencil-Normal", "Italic"); |
| 24 | +// HSSFHeader.fontSize((short) 16); |
| 25 | + // "Right w/ Stencil-Normal Italic font and size 16"); |
| 26 | + Row row = sheet.createRow((short) 0); |
| 27 | + row.setHeightInPoints(30); |
| 28 | + int titleCount = titles.size(); |
| 29 | + Font titleFont = wb.createFont(); |
| 30 | + titleFont.setBold(true); |
| 31 | + titleFont.setColor(IndexedColors.AQUA.getIndex()); |
| 32 | + CellStyle cellStyle = wb.createCellStyle(); |
| 33 | + cellStyle.setAlignment(CellStyle.ALIGN_CENTER); |
| 34 | + cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
| 35 | + cellStyle.setFont(titleFont); |
| 36 | + for (int i = 0; i < titleCount; i++) { |
| 37 | + Cell cell = row.createCell(i); |
| 38 | + cell.setCellValue(titles.get(i)); |
| 39 | + cell.setCellStyle(cellStyle); |
| 40 | + } |
| 41 | + CellStyle contentStyle = wb.createCellStyle(); |
| 42 | + cellStyle.setAlignment(CellStyle.ALIGN_CENTER); |
| 43 | + cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
| 44 | + int dataCount = datas.size(); |
| 45 | + for (int i = 1; i < dataCount + 1; i++) { |
| 46 | + Row rowIndex = sheet.createRow((short) i); |
| 47 | + Map<String, Object> data = datas.get(i - 1); |
| 48 | + Set<String> dataSet = data.keySet(); |
| 49 | + Iterator<String> d = dataSet.iterator(); |
| 50 | + int k = 0; |
| 51 | + while (d.hasNext()) { |
| 52 | + Cell cell = rowIndex.createCell(k); |
| 53 | + cell.setCellStyle(contentStyle); |
| 54 | + String key = d.next(); |
| 55 | + cell.setCellValue(data.get(key) == null ? "" : data.get(key).toString()); |
| 56 | + k++; |
| 57 | + } |
| 58 | + } |
| 59 | + FileOutputStream fileOut; |
| 60 | + try { |
| 61 | + fileOut = new FileOutputStream(path); |
| 62 | + wb.write(fileOut); |
| 63 | + fileOut.close(); |
| 64 | + } catch (IOException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + } |
169 | 68 | }
|
0 commit comments