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; } }
|