Use the jxl framework to parse Excel files

Source: Internet
Author: User

1. Import the Excel file to the database during project development and apply it to jxl. jar for parsing.

2. as follows

  1. Package test;
  2. Import java. Io. file;
  3. Import java. Io. ioexception;
  4. Import java. Io. unsupportedencodingexception;
  5. Import javax. servlet. http. httpservletresponse;
  6. Import jxl. sheet;
  7. Import jxl. workbook;
  8. Import jxl. workbooksettings;
  9. Import jxl. Read. Biff. biffexception;
  10. Import jxl. Write. label;
  11. Import jxl. Write. writablesheet;
  12. Import jxl. Write. writableworkbook;
  13. Import jxl. Write. writeexception;
  14. Public class jxltest {
  15. Public static void main (string [] ARGs ){
  16. (New jxltest (). excelread ();
  17. }
  18. Public void excelread (){
  19. File file = new file ("D: // cicc825.xls ");
  20. Try {
  21. Workbooksettings = new workbooksettings ();
  22. Workbooksettings. setencoding ("ISO-8859-1"); // key code to address Chinese garbled characters
  23. Workbook book = Workbook. getworkbook (file, workbooksettings );
  24. // Workbook book = Workbook. getworkbook (File );
  25. Sheet sheet = book. getsheet (0 );
  26. Int ROW = sheet. getrows ();
  27. Int column = sheet. getcolumns ();
  28. For (INT I = 0; I <row; I ++ ){
  29. For (Int J = 0; j <column; j ++ ){
  30. System. Out. println (sheet. getcell (J, I). getcontents ());
  31. }
  32. }
  33. } Catch (biffexception e ){
  34. E. printstacktrace ();
  35. } Catch (ioexception e ){
  36. E. printstacktrace ();
  37. }
  38. }
  39. Public void excelwrite (httpservletresponse response ){
  40. Long time = system. currenttimemillis (); // use the current system time as the default table name
  41. String name = string. valueof (time );
  42. Name = "position ";
  43. String pathname = Name + ". xls ";
  44. Try {
  45. Pathname = new string (pathname. getbytes (), "ISO-8859-1 ");
  46. } Catch (unsupportedencodingexception e ){
  47. E. printstacktrace ();
  48. }
  49. // Obtain the Save path and table name in the pop-up save box.
  50. Response. setcontenttype ("application/OCTET-STREAM ");
  51. Response. setheader ("content-disposition", "attachment; filename =/" "+ pathname + "/"");
  52. Writableworkbook workbook;
  53. Workbooksettings = new workbooksettings ();
  54. Workbooksettings. setencoding ("gb2312 ");
  55. Try {
  56. Workbook = Workbook. createworkbook (response. getoutputstream (), workbooksettings );
  57. Writablesheet Ws = Workbook. createsheet ("Sheet 1", 0 );
  58. WS. setcolumnview (0, 22 );
  59. Label cell = NULL;
  60. For (INT I = 0; I <2; I ++ ){
  61. Cell = new label (0, I + 1, "ID ");
  62. WS. addcell (cell );
  63. Cell = new label (1, I + 1, "name ");
  64. WS. addcell (cell );
  65. }
  66. Workbook. Write ();
  67. Workbook. Close (); // be sure to close it; otherwise, no Excel is saved;
  68. } Catch (ioexception e ){
  69. E. printstacktrace ();
  70. } Catch (writeexception e ){
  71. E. printstacktrace ();
  72. }
  73. }
  74. }

3. The parsing process is relatively simple.

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.