Java使用POI导出到Excel
来源:互联网 发布:部落冲突男王数据 编辑:程序博客网 时间:2024/06/06 00:48
转载:http://blog.csdn.net/itchiang/article/details/8219842
使用POI。谷歌输入POI 下一个poi的jar包。
- public class CreateSimpleExcelToDisk {
-
-
-
-
- private static List<Student> getStudent() throws Exception{
- List list = new ArrayList();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
-
- Student user1 = new Student(1,"张三",16,df.parse("1997-03-12"));
- Student user2 = new Student(2,"李四",17,df.parse("1996-08-12"));
- Student user3 = new Student(3,"王五",26,df.parse("1985-11-12"));
- list.add(user1);
- list.add(user2);
- list.add(user3);
-
- return list;
- }
- public static void main(String[] args) throws Exception {
-
- HSSFWorkbook wb = new HSSFWorkbook();
-
- HSSFSheet sheet = wb.createSheet("学生表一");
-
- HSSFRow row = sheet.createRow((int)0);
-
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
- HSSFCell cell = row.createCell((short)0);
- cell.setCellValue("学号"); cell.setCellStyle(style);
- cell = row.createCell((short)1);
- cell.setCellValue("姓名"); cell.setCellStyle(style);
- cell = row.createCell((short)2);
- cell.setCellValue("年龄"); cell.setCellStyle(style);
- cell = row.createCell((short)3);
- cell.setCellValue("生日"); cell.setCellStyle(style);
-
-
- List list = CreateSimpleExcelToDisk.getStudent();
-
- for(int i=0;i<list.size();i++){
- row = sheet.createRow((int)i+1);
- Student stu = (Student) list.get(i);
-
- row.createCell((short)0).setCellValue((double)stu.getId());
- row.createCell((short)1).setCellValue(stu.getName());
- row.createCell((short)2).setCellValue((double)stu.getAge());
- cell = row.createCell((short)3);
- cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
- }
-
- try {
- FileOutputStream fout = new FileOutputStream("E:/students.xls");
- wb.write(fout);
- fout.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
0 0