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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| package com.knowledge.point;
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.Font;
import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class TestGenExcel {
public static void main(String[] args) throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("测试生成 excel 表格是否乱码");
HSSFCellStyle titleStyle = workbook.createCellStyle(); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont titleFont = workbook.createFont(); titleFont.setItalic(false); titleFont.setColor(Font.COLOR_RED); titleFont.setFontHeightInPoints((short)16); titleFont.setFontName("宋体"); titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); titleStyle.setFont(titleFont);
HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); Font cellFont = workbook.createFont(); cellFont.setFontHeightInPoints((short)10); cellFont.setFontName("宋体"); cellStyle.setFont(cellFont);
HSSFRow headRow = sheet.createRow(0); HSSFCell headCell = headRow.createCell(0); headCell.setCellValue("更新时间"); headCell.setCellStyle(titleStyle); headCell = headRow.createCell(1); headCell.setCellValue("创建时间"); headCell.setCellStyle(titleStyle); headCell = headRow.createCell(2); headCell.setCellValue("删除状态"); headCell.setCellStyle(titleStyle); headCell = headRow.createCell(3); headCell.setCellValue("id"); headCell.setCellStyle(titleStyle); headCell = headRow.createCell(4); headCell.setCellValue("书名"); headCell.setCellStyle(titleStyle);
List<Map<String, Object>> list = getList();
for (int i = 0; i < list.size(); i++){ HSSFRow contentRow = sheet.createRow(sheet.getLastRowNum() + 1); for (int j = 0; j < 5; j++){ HSSFCell contentCell = contentRow.createCell(j); switch (j){ case 0: contentCell.setCellValue(String.valueOf(list.get(i).get("update_time"))); break; case 1: contentCell.setCellValue(String.valueOf(list.get(i).get("create_time"))); break; case 2: contentCell.setCellValue(String.valueOf(list.get(i).get("delete_status"))); break; case 3: contentCell.setCellValue(String.valueOf(list.get(i).get("id"))); break; case 4: contentCell.setCellValue(String.valueOf(list.get(i).get("content"))); break; } contentCell.setCellStyle(cellStyle); } }
sheet.setColumnWidth(0, 900 * 10); sheet.setColumnWidth(1, 900 * 10); sheet.setColumnWidth(2, 900 * 10); sheet.setColumnWidth(3, 900 * 10); sheet.setColumnWidth(4, 900 * 10);
File xlsFile = new File("测试生成Excel.xls"); workbook.write(new FileOutputStream(xlsFile)); workbook.close();
}
private static List<Map<String, Object>> getList(){
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("update_time", "1569404166000"); map1.put("create_time", "1569404122000"); map1.put("delete_status", "1"); map1.put("id", "2"); map1.put("content", "如何削铁"); list.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("update_time", "1509357581000"); map2.put("create_time", "1508893725000"); map2.put("delete_status", "1"); map2.put("id", "5"); map2.put("content", "莎士比亚"); list.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("update_time", "1510970055000"); map3.put("create_time", "1508986168000"); map3.put("delete_status", "1"); map3.put("id", "6"); map3.put("content", "亚里士多德"); list.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("update_time", "1510118932000"); map4.put("create_time", "1509001065000"); map4.put("delete_status", "1"); map4.put("id", "10"); map4.put("content", "亚历山大"); list.add(map4);
Map<String, Object> map5 = new HashMap<String, Object>(); map5.put("update_time", "1509002622000"); map5.put("create_time", "1509002622000"); map5.put("delete_status", "0"); map5.put("id", "11"); map5.put("content", "李白"); list.add(map5);
Map<String, Object> map6 = new HashMap<String, Object>(); map6.put("update_time", "1569376168000"); map6.put("create_time", "1569404290000"); map6.put("delete_status", "1"); map6.put("id", "12"); map6.put("content", "无用是什么"); list.add(map6);
Map<String, Object> map7 = new HashMap<String, Object>(); map7.put("update_time", "1569376160000"); map7.put("create_time", "1569404290000"); map7.put("delete_status", "0"); map7.put("id", "13"); map7.put("content", "选择吃什么饭是一件很痛苦的事"); list.add(map7);
Map<String, Object> map8 = new HashMap<String, Object>(); map8.put("update_time", "1510983431000"); map8.put("create_time", "1510983427000"); map8.put("delete_status", "1"); map8.put("id", "19"); map8.put("content", "文章test2"); list.add(map8);
Map<String, Object> map9 = new HashMap<String, Object>(); map9.put("update_time", "1569376164000"); map9.put("create_time", "1569404290000"); map9.put("delete_status", "1"); map9.put("id", "20"); map9.put("content", "就像选择在哪里睡觉一样"); list.add(map9);
Map<String, Object> map10 = new HashMap<String, Object>(); map10.put("update_time", "1569376166000"); map10.put("create_time", "1569404290000"); map10.put("delete_status", "1"); map10.put("id", "21"); map10.put("content", "effective of java"); list.add(map10);
Map<String, Object> map11 = new HashMap<String, Object>(); map11.put("update_time", "1569376172000"); map11.put("create_time", "1569404290000"); map11.put("delete_status", "1"); map11.put("id", "22"); map11.put("content", "荒岛求生"); list.add(map11);
Map<String, Object> map12 = new HashMap<String, Object>(); map12.put("update_time", "1569376170000"); map12.put("create_time", "1569404290000"); map12.put("delete_status", "0"); map12.put("id", "23"); map12.put("content", "天使永远就在身边"); list.add(map12);
return list; } }
|