Java Import Export Excel

Source: Internet
Author: User

One, export Excel

1, using Excel templates

 Public voidExportlog ()throwsexception{systemuser currentusr=Getcurrentsystemuser (); //All logs for this userList<tloginfo> loglist=Logservice.getloginfosbyuserid (Currentusr.getid ()); List<TLogInfo> list=NewArraylist<tloginfo>(); if(loglist!=NULL&& loglist.size () >0){             for(Tloginfo log:loglist) {Tloginfo export=NewTloginfo ();                Export.setproname (Log.getproname ());                Export.setlogtitle (Log.getlogtitle ());                Export.setlogtype (Log.getlogtype ());                Export.setworktime (Log.getworktime ());                Export.setlogfilltime (Log.getlogfilltime ());            List.add (export); }} String Templatefilename= "Xlstemp/personallog.xls"; String Resultfilename= "Xlsresult/personallog.xls"; String Path=excelutil.createexcel (Templatefilename, List, resultfilename); File File=NewFile (path); byte[] bytes =Fileutils.readfiletobytearray (file); DownloadFile ("Information. xls", bytes);                    File.deleteonexit (); }
The Createexcel method of Excelutil class
 Public StaticString Createexcel (String templatefilename, list<?>list, String resultfilename) {        //Create a Xlstransformer objectXlstransformer transformer =NewXlstransformer (); //Get the Java Project post-compilation root pathString Class_path = Excelutil.class. getClassLoader (). GetResource (""). GetPath (); //Get template file pathString Srcfilepath = Class_path +Templatefilename; Map<string, object> beanparams =NewHashmap<string, object>(); Beanparams.put ("List", list); String Destfilepath= Class_path +Resultfilename; Try {            //To generate an Excel fileTransformer.transformxls (Srcfilepath, Beanparams, Destfilepath); returnDestfilepath; } Catch(parsepropertyexception e) {e.printstacktrace (); } Catch(invalidformatexception e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); }        return NULL; }

Not to be continued ....

Java Import Export Excel

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.