Making Excel format reports in Java

Source: Internet
Author: User
Tags format dateformat
Excel
try{         getdbconnection ();          pstmt = conn.preparestatement (strSQL);         ret = Pstmt.executequery ();         file Tempfile=new file (filename);          try{         Writableworkbook Workbook = Workbook.createworkbook (tempfile);         WritableSheet sheet = Workbook.createsheet ("Sheet1", 0);         Label l=null;          Jxl.write.Number n=null;         Jxl.write.DateTime d=null;         Writablefont HeaderFont = new Writablefont (Writablefont.arial,12,writablefont.bold,false, Underlinestyle.no_underline,jxl.format.colour.blue) ;          Writablecellformat Headerformat = new Writablecellformat (HeaderFont);

Writablefont Titlefont = new Writablefont (Writablefont.arial,10,writablefont.no_bold,false, UnderlineStyle.NO_         underline,jxl.format.colour.red); Writablecellformat TitleFormat = new Writablecellformat (Titlefont);

Writablefont Detfont = new Writablefont (Writablefont.arial,10,writablefont.no_bold,false, UnderlineStyle.NO_         Underline,jxl.format.colour.black); Writablecellformat Detformat = new Writablecellformat (Detfont);

NumberFormat nf=new NumberFormat ("0.00000"); Writablecellformat Priceformat = new Writablecellformat (Detfont, NF);

DateFormat df=new DateFormat ("Yyyy-mm-dd"); Writablecellformat DateFormat = new Writablecellformat (Detfont, DF);

         l=new Label (2,0, "", Headerformat);          Sheet.addcell (l);         int inttitle = title.length;          for (int i=0;i<inttitle;i++)           {           l=new Label (I,2,title[i], TitleFormat);           Sheet.addcell (l);         }        int k = 0;         while (Ret.next ())                  {                   k++;                   FOR (int j=1;j<inttitle;j++)                          {               l=new Label (J-1,k+3,ret.getstring (j), Detformat);               Sheet.addcell (L);                          // Sheet.setcolumnview (j,30);                        }                }         workbook.write ();          workbook.close ();        } catch (exception e) {}   }   catch (SQLException ae) {        throw new ApplicationException ("Query database operation failed while creating Excel!") +              "error message:" +strsql + ", exception information is: \ n" + ae.getmessage ());    } finally {    & nbsp;           Closeresultset (ret);                 closestatement (pstmt);                 CloseConnection ();                   }}


Related Article

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.