1. 程式人生 > >Java 實現導出excel表 POI

Java 實現導出excel表 POI

list() tar creates turn 實體 指定位置 com .text orm

1.先導入相關jar包

2.Student.java

  1. import java.util.Date;
  2. public class Student
  3. {
  4. private int id;
  5. private String name;
  6. private int age;
  7. private Date birth;
  8. public Student()
  9. {
  10. }
  11. public Student(int id, String name, int age, Date birth)
  12. {
  13. this.id = id;
  14. this.name = name;
  15. this.age = age;
  16. this.birth = birth;
  17. }
  18. public int getId()
  19. {
  20. return id;
  21. }
  22. public void setId(int id)
  23. {
  24. this.id = id;
  25. }
  26. public String getName()
  27. {
  28. return name;
  29. }
  30. public void setName(String name)
  31. {
  32. this.name = name;
  33. }
  34. public int getAge()
  35. {
  36. return age;
  37. }
  38. public void setAge(int age)
  39. {
  40. this.age = age;
  41. }
  42. public Date getBirth()
  43. {
  44. return birth;
  45. }
  46. public void setBirth(Date birth)
  47. {
  48. this.birth = birth;
  49. }
  50. }

3.CreateSimpleExcelToDisk.java

  1. import java.io.FileOutputStream;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. public class CreateSimpleExcelToDisk
  11. {
  12. /**
  13. * @功能:手工構建一個簡單格式的Excel
  14. */
  15. private static List<Student> getStudent() throws Exception
  16. {
  17. List list = new ArrayList();
  18. SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
  19. Student user1 = new Student(1, "張三", 16, df.parse("1997-03-12"));
  20. Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
  21. Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
  22. list.add(user1);
  23. list.add(user2);
  24. list.add(user3);
  25. return list;
  26. }
  27. public static void main(String[] args) throws Exception
  28. {
  29. // 第一步,創建一個webbook,對應一個Excel文件
  30. HSSFWorkbook wb = new HSSFWorkbook();
  31. // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
  32. HSSFSheet sheet = wb.createSheet("學生表一");
  33. // 第三步,在sheet中添加表頭第0行,註意老版本poi對Excel的行數列數有限制short
  34. HSSFRow row = sheet.createRow((int) 0);
  35. // 第四步,創建單元格,並設置值表頭 設置表頭居中
  36. HSSFCellStyle style = wb.createCellStyle();
  37. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
  38. HSSFCell cell = row.createCell((short) 0);
  39. cell.setCellValue("學號");
  40. cell.setCellStyle(style);
  41. cell = row.createCell((short) 1);
  42. cell.setCellValue("姓名");
  43. cell.setCellStyle(style);
  44. cell = row.createCell((short) 2);
  45. cell.setCellValue("年齡");
  46. cell.setCellStyle(style);
  47. cell = row.createCell((short) 3);
  48. cell.setCellValue("生日");
  49. cell.setCellStyle(style);
  50. // 第五步,寫入實體數據 實際應用中這些數據從數據庫得到,
  51. List list = CreateSimpleExcelToDisk.getStudent();
  52. for (int i = 0; i < list.size(); i++)
  53. {
  54. row = sheet.createRow((int) i + 1);
  55. Student stu = (Student) list.get(i);
  56. // 第四步,創建單元格,並設置值
  57. row.createCell((short) 0).setCellValue((double) stu.getId());
  58. row.createCell((short) 1).setCellValue(stu.getName());
  59. row.createCell((short) 2).setCellValue((double) stu.getAge());
  60. cell = row.createCell((short) 3);
  61. cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
  62. .getBirth()));
  63. }
  64. // 第六步,將文件存到指定位置
  65. try
  66. {
  67. FileOutputStream fout = new FileOutputStream("E:/students.xls");
  68. wb.write(fout);
  69. fout.close();
  70. }
  71. catch (Exception e)
  72. {
  73. e.printStackTrace();
  74. }
  75. }
  76. }

Java 實現導出excel表 POI