POI Rack Package export Excel, and download

Source: Internet
Author: User

Export Excel has been applied to many systems, with two simple examples as an introduction:

1, import Poi-3.9.jar, you can download http://poi.apache.org on the official website.

2, first write a simple test class, there is a detailed explanation, the code is as follows:

1 ImportJava.io.FileOutputStream;2 Importjava.io.IOException;3 ImportOrg.apache.poi.hssf.usermodel.HSSFCell;4 ImportOrg.apache.poi.hssf.usermodel.HSSFRow;5 ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;6 ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;7 Importorg.junit.Test;8 9  Public classOutputexcel {Ten @Test One      Public voidOutputexcel ()throwsioexception{ AFileOutputStream fos=NewFileOutputStream ("D://data.xls"); -         //writes the data of Excel, which you read from the database according to your own situation.  -String[][] data={{"0001", "XX Bureau", "Xx."},{"0002", "XX Bureau a organization", "" "},{" 0003 "," XX Bureau B Agency "," 00 "}}; theHssfworkbook workbook=NULL; -String[] title={"unit Code", "unit name", "Parent Unit Code"};//the title of the first line in Excel -workbook=NewHssfworkbook ();//Create Excel -Hssfsheet sheet=workbook.createsheet ("Export Data case");//Work Table +Hssfrow row=sheet.createrow (0);//line, default starting from 0 -Hssfcell cell=NULL;//columns, and rows, each time you create a row, create a column corresponding to that row +         //First line Enter title A          for(intj=0;j<title.length;j++){ atCell=Row.createcell (j); - Cell.setcellvalue (Title[j]); -         } -         //writing data to Excel -          for(intt=0;t<data.length;t++){ -Row=sheet.createrow (t+1);//created from the second line, preceded by the first row of headings in              for(intk=0;k<3;k++){ -Cell=Row.createcell (k); to Cell.setcellvalue (Data[t][k]); +             } -         } the         Try { *Workbook.write (FOS);//write Excel through the data stream $ Fos.flush ();Panax Notoginseng fos.close (); -}finally{ the             if(fos!=NULL){ + Fos.flush (); A fos.close (); the             } +         } -     } $}

Run the above code to see the Data.xls export file on the D drive.

Open as follows:

One thing to emphasize here is that when setting values for a column, someone often uses cell.setcellvalue (new hssfrichtextstring (TITLE[J)); This is a bad habit, because if the amount of data is large, then new hssfrichtextstring () can cause memory crashes.

3, the above way is to write dead path export, and in practical applications often do not write, Excel files are either stored in a server file, or from the database to obtain data to create, and then the user through the download to obtain.

Here is the introduction, the service layer returns a workbook to the Controller layer implementation download, the code is as follows:

1 @Service2  Public classOutputexcelservice {3 @Autowired4     PrivateAccbookdao Accbookdao;5 @Autowired6     PrivateAssetcarddao Assetcarddao;7 @Autowired8     PrivateGldefdao Gldefdao;9 @AutowiredTen     PrivateIpcompanydao Ipcompanydao; One      A@SuppressWarnings ("Unchecked") -      Publichssfworkbook outputexcel (String nd,string co_code) { -Hssfworkbook workbook=NULL; theString[] title={"unit Code", "unit name", "Parent Unit Code", "Public Profile", "General Ledger Data", "Fixed asset data"}; -         //parameter Definition -Map<string, object> param=NewHashmap<string, object>(); -Param.put ("Co_code", Co_code); +         //Get Unit Code, organization name, parent unit code -List<pagevo> dwlist=Ipcompanydao.findpagebycode (param); +         //querying other table data based on Dwlist AList<string> Data=NewArraylist<string>(); at          for(intI=0;i<dwlist.size (); i++){ - Data.add (Dwlist.get (i). Getco_code ()); - Data.add (Dwlist.get (i). Getco_name ()); -              -String dw_code=Dwlist.get (i). Getco_code (); -Map<string, object> param2=NewHashmap<string, object>(); inParam2.put ("nd", ND); -Param2.put ("Dw_code", Dw_code); to              + Data.add (Ipcompanydao.selectpcocode (Dw_code)); -              the             if(Accbookdao.selectaccbookno (param2)! =NULL){ *Data.add ("Imported"); $}Else{Panax NotoginsengData.add ("None"); -             } the             if(Gldefdao.selectaccbookno (param2)! =NULL){                 +Data.add ("Imported"); A}Else{ theData.add ("None"); +             } -             if(Assetcarddao.selectassetno (param2)! =NULL){ $Data.add ("Imported");  $}Else{ -Data.add ("None"); -             } the         }     -workbook=NewHssfworkbook ();//Create ExcelWuyiHssfsheet sheet=workbook.createsheet ("Export Data case");//Work Table theHssfrow row=sheet.createrow (0);//Line -Hssfcell cell=NULL;//column Wu         //First line Enter title -          for(intj=0;j<title.length;j++){ AboutCell=Row.createcell (j); $ Cell.setcellvalue (Title[j]); -         } -         //writing data to Excel -          for(intT=0;t<data.size ()/6;t++){ ARow=sheet.createrow (t+1); +              for(intk=0;k<6;k++){ theCell=Row.createcell (k); -Cell.setcellvalue (Data.get (t*6+k)); $             } the         } the         returnWorkBook; the     } the}

This returns a workbook to the controller layer, which is then called by the controller layer to obtain the foreground data and return data to the foreground. Http://www.cnblogs.com/yaket/p/5830531.html

POI Rack Package export Excel, and download

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.