Export data to the EXL table

Source: Internet
Author: User

The project uses the HHI framework, which is accessed through struts to the action

XML file:

<Actionname= "Fabattributedaochu"class= "com. Fabattributeaction "Method= "Daochu" >    <resultname= "Success"type= "Stream">              <!--Specify the file type of the download file -              <paramname= "ContentType">Xls</param>              <!--Specify the file location of the download file -              <paramname= "InputName">Downexpfile</param>              <!--Specify the file name of the download -              <paramname= "Contentdisposition">Attachment;filename= "${contentdisposition}"</param>              <!--specify the buffer size of the download file -              <paramname= "BufferSize">4096</param>        </result></Action>

Action file:

ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;ImportOrg.apache.poi.hssf.usermodel.HSSFDataFormat;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;PrivateString contentdisposition;//must have properties PublicString getcontentdisposition () {//get  
returnContentdisposition;
} Public voidSetcontentdisposition (String contentdisposition) {//set
This. contentdisposition = contentdisposition;
} PublicString Daochu ()throwsexception{//Front Desk Press the export key to jump to this method, you can set some of the conditions to determine whether to continue returnsuccess;} PublicInputStream Getdownexpfile ()throwsexception{//a Get value is defined based on the return of the XML, and a method is entered,Contentdisposition = Java.net.URLEncoder.encode ("Buyorder.xls", "UTF-8");//Specify a file name returnFabattributebo.daochu ();//Call the BO layer data to return a InputStream object}

Logical Business Layer (BO):

     /*** Export *@throwsboexception *@throwsFileNotFoundException **/ PublicFileInputStream Daochu ()throwsboexception {Try{List<FabAttributeVo> list = This. Getfabattributedao (). Selectfabattribute (NULL). GetList ();//the collection of exported data--dataInteger RowNumber =list.size (); //Create a new ExcelHssfworkbook Workbook =NewHssfworkbook (); //Create a worksheet nameHssfsheet sheet = workbook.createsheet ("Label 1"); //Defining RowsHssfrow row =NULL; //titlelist as Unit göminglist<string > Titlelist = This. Obtaintitlelist ();//call a defined column header//defining cells with arraysHssfcell [] Hssfcells =Newhssfcell[titlelist.size ()]; //Formatting cellsHssfcellstyle CellStyle =Workbook.createcellstyle (); Hssfdataformat format=Workbook.createdataformat (); Cellstyle.setdataformat (Format.getformat ("@")); //here's how to create the first lineRow =sheet.createrow (0);  for(intJ=0;j<titlelist.size (); j + +) {Hssfcells[j]=Row.createcell (j);            Hssfcells[j].setcellstyle (CellStyle);        Hssfcells[j].setcellvalue (Titlelist.get (j)); }        //The following is the creation of data rows         for(inti = 1; rownumber-i>=0; i++) {row =Sheet.createrow (i);  for(intj = 0; J < Titlelist.size (); J + +) {Hssfcells[j]=Row.createcell (j);                Hssfcells[j].setcellstyle (CellStyle);                Hssfcells[j].setcelltype (hssfcell.cell_type_string); Switch(j) {//each line needs to be inserted to get a piece of                     Case0:hssfcells[j].setcellvalue (List.get (i-1). getattr_id ()); Break;  Case1:hssfcells[j].setcellvalue (List.get (i-1). Gettype_name ()); Break;  Case2:hssfcells[j].setcellvalue (List.get (i-1). Getattr_name ()); Break;  Case3:hssfcells[j].setcellvalue (List.get (i-1). Getattr_input_type ()); Break;  Case4:hssfcells[j].setcellvalue (List.get (i-1). Getattr_values ()); Break;  Case5:hssfcells[j].setcellvalue (List.get (i-1). Getsortorder ()); Break; }            }        }        File File=NewFile ( This. GetClass (). getClassLoader (). GetResource (""). GetPath () + "Fabric/tmp/buyorder.xls"); if(File.exists ()) File.delete (); //Create a new output file streamFileOutputStream Fout =Newfileoutputstream (file); //write the corresponding Excel workbookWorkbook.write (Fout); FileInputStream in=Newfileinputstream (file);            Fout.close (); returnIn ; }Catch(Exception e) {e.printstacktrace (); }        return NULL;}

Set the first column lines of the table:

/** * Settings for column names */ Private List<string> obtaintitlelist () {/// Define yourself well, you need to list the EML in the table    new arraylist<string >();    Titlelist.add ("ID");    Titlelist.add ("Commodity type name");    Titlelist.add ("attribute name");    Titlelist.add ("input type");    Titlelist.add ("attribute value");    Titlelist.add ("Order of Arrangement")    ; return titlelist;}


This will be OK!!

Export data to the EXL table

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.