Exporting Excel in Java

Source: Internet
Author: User

[Java]View Plaincopy
  1. Package oa.common.utils;
  2. Import Java.io.OutputStream;
  3. Import java.util.List;
  4. Import Javax.servlet.http.HttpServletResponse;
  5. Import Org.apache.struts2.ServletActionContext;
  6. Import Java.lang.reflect.Field;
  7. Import JXL. Workbook;
  8. Import jxl.format.Alignment;
  9. Import Jxl.format.Border;
  10. Import Jxl.format.BorderLineStyle;
  11. Import jxl.format.VerticalAlignment;
  12. Import Jxl.write.Label;
  13. Import Jxl.write.WritableCellFormat;
  14. Import Jxl.write.WritableFont;
  15. Import Jxl.write.WritableSheet;
  16. Import Jxl.write.WritableWorkbook;
  17. /***
  18. * @author LSF
  19. */
  20. Public class Exportexcel {
  21. /*************************************************************************** 
  22. * @param fileName Excel file name
  23. * @param listtitle Excel file first row header collection
  24. * @param listcontent Excel file Body data collection
  25. * @return
  26. */
  27. public final static string Exportexcel (String filename,string[] Title, list<object> listcontent) {
  28. String result="System tip: Excel file exported successfully!    ";
  29. ///below start output to Excel
  30. try {
  31. //define the output stream to open the Save dialog box ______________________begin
  32. HttpServletResponse Response=servletactioncontext.getresponse ();
  33. OutputStream OS = Response.getoutputstream (); //Get output stream
  34. Response.reset (); //Clear output stream
  35. Response.setheader ("content-disposition", "attachment;  Filename= "+ New String (Filename.getbytes (" GB2312 ")," iso8859-1 "));
  36. Set Output file header
  37. Response.setcontenttype ("Application/msexcel"); Defining the output Type
  38. //define the output stream to open the Save dialog box _______________________end
  39. /** ********** Create Workbook ************ * /
  40. Writableworkbook workbook = workbook.createworkbook (OS);
  41. /** ********** Creating Worksheets ************ * /
  42. Writablesheet sheet = workbook.createsheet ("Sheet1", 0);
  43. /** ********** Set vertical and horizontal printing (default is vertical), printer paper ***************** */
  44. Jxl. Sheetsettings Sheetset = Sheet.getsettings ();
  45. Sheetset.setprotected (false);
  46. /** ************ Set cell font ************** * /
  47. Writablefont Normalfont = new Writablefont (writablefont.arial, 10);
  48. Writablefont Boldfont = new Writablefont (writablefont.arial, 10,writablefont.bold);
  49. /** ************ The following three kinds of cell styles, flexible standby ************ * /
  50. //For title centering
  51. Writablecellformat wcf_center = new Writablecellformat (Boldfont);
  52. Wcf_center.setborder (Border.all, Borderlinestyle.thin); //Line
  53. Wcf_center.setverticalalignment (Verticalalignment.centre); //Text Vertical alignment
  54. Wcf_center.setalignment (Alignment.centre); //Text horizontal alignment
  55. Wcf_center.setwrap (false); //text is wrapped
  56. //For the text to reside left
  57. Writablecellformat wcf_left = new Writablecellformat (Normalfont);
  58. Wcf_left.setborder (Border.none, Borderlinestyle.thin); //Line
  59. Wcf_left.setverticalalignment (Verticalalignment.centre); //Text Vertical alignment
  60. Wcf_left.setalignment (Alignment.left); //Text horizontal alignment
  61. Wcf_left.setwrap (false); //text is wrapped
  62. /** *************** The following is the beginning of Excel headlines, temporarily omit ********************* * /
  63. //sheet.mergecells (0, 0, colwidth, 0);
  64. //sheet.addcell (New Label (0, 0, "xx report", Wcf_center));
  65. /** *************** The following is the first row of Excel headings ********************* * /
  66. For (int i = 0; i < title.length; i++) {
  67. Sheet.addcell (new Label (i, 0,title[i],wcf_center));
  68. }
  69. /** *************** The following is the Excel body data ********************* * /
  70. field[] fields=null;
  71. int i=1;
  72. For (Object obj:listcontent) {
  73. Fields=obj.getclass (). Getdeclaredfields ();
  74. int j=0;
  75. For (Field v:fields) {
  76. V.setaccessible (true);
  77. Object va=v.get (obj);
  78. if (va==null) {
  79. Va="";
  80. }
  81. Sheet.addcell (New Label (J, I,va.tostring (), wcf_left));
  82. j + +;
  83. }
  84. i++;
  85. }
  86. /** ********** writes the contents of the above cache to an Excel file ******** * /
  87. Workbook.write ();
  88. /** ********* Close File ************* * /
  89. Workbook.close ();
  90. } catch (Exception e) {
  91. result="System tip: Excel file export failed, Reason:" + e.tostring ();
  92. SYSTEM.OUT.PRINTLN (result);
  93. E.printstacktrace ();
  94. }
  95. return result;
  96. }
  97. }


Test:

    • * Export Excel
    • * @return
    • */
    • Public String Excelpage () {
    • Exportexcel excel=New Exportexcel ();
    • String str="";
    • try {
    • str = New String (Gethttp.getrequest (). GetParameter ("Wineorg.orgname"). GetBytes ("iso8859-1"),"  UTF-8 ");
    • } catch (Unsupportedencodingexception e) {
    • //TODO auto-generated catch block
    • E.printstacktrace ();
    • }
    • Wineorg.setorgname (str);
    • List<object> Li=service.exportexcel (wineorg);
    • String[] title={"Agency ID","Membership number","Categories","Name","Save ID","Province name","City ID","City Name","Detailed Address","Contacts","Gender","Contact Phone","Contact Phone","Fax","Mailbox", "QQ", "integral", "Customer Level", Span class= "string", "Cash Account balance", "purchase Count" ", " del ", " STS ", " Remarks ", "Owner id", "owner name", "Audit id", " reviewer name ", " Audit Date ", " Assigned Person ID ", "assigned name", "Assigned Date",
    • Excel.exportexcel ("customer profile information. xls", Title, Li);
    • return SUCCESS;
    • }

Exporting Excel in Java

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.