Java implementation Export Excel table POI

Source: Internet
Author: User

1. Import the relevant JAR package first

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.)
  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. * @ Function: Manually build an excel in a simple format
  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, "Zhang San", + , Df.parse ("1997-03-12"));
  20. Student user2 = new Student (2, "John Doe", + , Df.parse ("1996-08-12"));
  21. Student User3 = new Student (3, "Harry", + , 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. //First step, create a webbook, corresponding to an Excel file
  30. Hssfworkbook WB = new Hssfworkbook ();
  31. ///second step, add a sheet in WebBook, corresponding to the sheet in the Excel file
  32. Hssfsheet sheet = wb.createsheet ("student form One");
  33. ///step three, add the header line No. 0 to the sheet, and note that the older POI has a limit on the number of rows in Excel short
  34. Hssfrow row = Sheet.createrow ((int) 0);
  35. //Fourth step, create the cell and set the value header to center the table header
  36. Hssfcellstyle style = Wb.createcellstyle ();
  37. Style.setalignment (Hssfcellstyle.align_center); //Create a center format
  38. Hssfcell cell = Row.createcell ((short) 0);
  39. Cell.setcellvalue ("School Number");
  40. Cell.setcellstyle (style);
  41. Cell = Row.createcell ((short) 1);
  42. Cell.setcellvalue ("name");
  43. Cell.setcellstyle (style);
  44. Cell = Row.createcell ((short) 2);
  45. Cell.setcellvalue ("age");
  46. Cell.setcellstyle (style);
  47. Cell = Row.createcell ((short) 3);
  48. Cell.setcellvalue ("Birthday");
  49. Cell.setcellstyle (style);
  50. //Fifth, write the data from the database in the actual application of the physical data,
  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. //Fourth step, create a cell and set the value
  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. //Sixth, save the file to the specified location
  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 implementation Export Excel table POI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.