1 packagecom.ym.admin.controller;2
3 importjava.io.FileOutputStream;4 importjava.text.SimpleDateFormat;5 importjava.util.ArrayList;6 importjava.util.Date;7 importjava.util.List;8
9 importorg.apache.poi.hssf.usermodel.HSSFCell;10 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;11 importorg.apache.poi.hssf.usermodel.HSSFRow;12 importorg.apache.poi.hssf.usermodel.HSSFSheet;13 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;14
15 /**
16 *@authorAnear:17 *@version创建时间:2017年12月23日 下午3:52:5418 */
19 public classTestExcelMain {20
21 /**
22 * @功能:手工构建一个简单格式的Excel23 */
24 private static List getMember() throwsException25 {26 List list = new ArrayList();27 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");28
29 Member user1 = new Member(1, "anpei", 16,df.parse("1994-08-19"));30 Member user2 = new Member(2, "熊二", 23, df.parse("1994-08-19"));31 Member user3 = new Member(3, "熊熊", 24, df.parse("1983-11-22"));32 list.add(user1);33 list.add(user2);34 list.add(user3);35
36 returnlist;37 }38
39 public static void main(String[] args) throwsException40 {41 //第一步,创建一个webbook,对应一个Excel文件
42 HSSFWorkbook wb = newHSSFWorkbook();43 //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
44 HSSFSheet sheet = wb.createSheet("学生表一");45 //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
46 HSSFRow row = sheet.createRow((int) 0);47 //第四步,创建单元格,并设置值表头 设置表头居中
48 HSSFCellStyle style =wb.createCellStyle();49 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式
50
51 HSSFCell cell = row.createCell((short) 0);52 cell.setCellValue("学号");53 cell.setCellStyle(style);54 cell = row.createCell((short) 1);55 cell.setCellValue("姓名");56 cell.setCellStyle(style);57 cell = row.createCell((short) 2);58 cell.setCellValue("年龄");59 cell.setCellStyle(style);60 cell = row.createCell((short) 3);61 cell.setCellValue("生日");62 cell.setCellStyle(style);63
64 //第五步,写入实体数据 实际应用中这些数据从数据库得到,
65 List list =TestExcelMain.getMember();66
67 for (int i = 0; i < list.size(); i++)68 {69 row = sheet.createRow((int) i + 1);70 Member stu =(Member) list.get(i);71 //第四步,创建单元格,并设置值
72 row.createCell((short) 0).setCellValue((double) stu.getCode());73 row.createCell((short) 1).setCellValue(stu.getName());74 row.createCell((short) 2).setCellValue((double) stu.getAge());75 cell = row.createCell((short) 3);76 cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu77 .getBirth()));78 }79 //第六步,将文件存到指定位置
80 try
81 {82 FileOutputStream fout = new FileOutputStream("E:/Members.xls");83 wb.write(fout);84 fout.close();85 }86 catch(Exception e)87 {88 e.printStackTrace();89 }90 }91
92 public static classMember{93 privateInteger code;94
95 privateString name;96
97 privateInteger age;98
99 privateDate birth;100
101 publicMember(Integer code, String name, Integer age, Date birth) {102 super();103 this.code =code;104 this.name =name;105 this.age =age;106 this.birth =birth;107 }108
109 publicInteger getCode() {110 returncode;111 }112
113 public voidsetCode(Integer code) {114 this.code =code;115 }116
117 publicString getName() {118 returnname;119 }120
121 public voidsetName(String name) {122 this.name =name;123 }124
125 publicInteger getAge() {126 returnage;127 }128
129 public voidsetAge(Integer age) {130 this.age =age;131 }132
133 publicDate getBirth() {134 returnbirth;135 }136
137 public voidsetBirth(Date birth) {138 this.birth =birth;139 }140
141
142
143 }144 }