Write the Excel file content to the database-gentle

Source: Internet
Author: User
  1. Package com. Zhupan. Spring;
  2. Import java. Io. file;
  3. Import javax. servlet. http. httpservletrequest;
  4. Import javax. servlet. http. httpservletresponse;
  5. Import jxl. sheet;
  6. Import jxl. workbook;
  7. Import org. springframework. validation. bindexception;
  8. Import org. springframework. Web. multipart. multipartfile;
  9. Import org. springframework. Web. multipart. multiparthttpservletrequest;
  10. Import org. springframework. Web. servlet. modelandview;
  11. Import org. springframework. Web. servlet. MVC. simpleformcontroller;
  12. Import com. ctgusec. model. student_info;
  13. Import com. ctgusec. Service. istudent_infomanage;
  14. /***/
  15. /**
  16. * @ Author Zhupan
  17. *
  18. */
  19. Public class estudentinsertexcelcontroller extends simpleformcontroller {
  20. Private istudent_infomanage studentmanage;
  21. @ Override
  22. Protected modelandview onsubmit (httpservletrequest request,
  23. Httpservletresponse response, object command, bindexception errors)
  24. Throws exception {
  25. Student_info = (student_info) command;
  26. Try {
  27. Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
  28. Multipartfile file = multipartrequest. GetFile ("excelfile"); // obtain the file:
  29. File tofile = new file ("C: // student information overview file .xls"); // generate file names and empty files
  30. File. transferto (tofile); // File Upload
  31. Workbook book = Workbook. getworkbook (tofile); // get the work thin
  32. Sheet sheet = book. getsheet (0); // obtain the first worksheet object
  33. Int ROW = sheet. getrows (); // obtain the number of rows for this sheet.
  34. Int column = sheet. getcolumns (); // obtain the number of columns for this sheet.
  35. System. Out. println ("number of data rows =" + row );
  36. System. Out. println ("Data columns =" + column );
  37. For (INT I = 1; I <row; I ++ ){
  38. For (Int J = 0; j <column; j ++ ){
  39. System. Out. println ("J =" + J );
  40. Sheet. getcell (J, I). getcontents (); // obtain the class capacity of cells in row I of column J.
  41. Student_info
  42. . Setstudentid (sheet. getcell (J, I). getcontents ());
  43. Student_info.setname (sheet. getcell (++ J, I). getcontents ());
  44. Student_info.setsex (sheet. getcell (++ J, I). getcontents ());
  45. Student_info.setunit (sheet. getcell (++ J, I). getcontents ());
  46. Student_info.setclass _ (sheet. getcell (++ J, I). getcontents ());
  47. Student_info.setspecialty (sheet. getcell (++ J, I)
  48. . Getcontents ());
  49. Student_info.setremark (sheet. getcell (++ J, I). getcontents ());
  50. }
  51. If (this. studentmanage. getstudentbystudentid (
  52. Student_info.getstudentid (). Size ()! = 0)
  53. Return new modelandview ("Education/e-studentinfoadderror ");
  54. This. studentmanage. insertstudent_info (student_info );
  55. }
  56. Book. Close ();
  57. Return new modelandview ("Education/e-studentinfoaddexcelsuccess ",
  58. "Row", new INTEGER (Row-1 ));
  59. } Catch (exception e ){
  60. E. printstacktrace ();
  61. }
  62. Return new modelandview ("Education/e-studentinfoaddexcelerror ");
  63. }
  64. Public void setstudentmanage (istudent_infomanage studentmanage ){
  65. This. studentmanage = studentmanage;
  66. }
  67. }

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.