EXTJS4+SSH Implementation Excel Export

Source: Internet
Author: User
Tags dateformat first row
1. Notes

In projects, we often need to export data to Excel.

Front desk Use Extjs4.2

The background uses SSH.

Export Excel to use Apache's POI component


Poi Download Address: http://poi.apache.org/download.html

POI Excel Export Learning site: http://poi.apache.org/spreadsheet/quick-guide.html

Reference Blog: http://blog.csdn.net/gavinloo/article/details/6707828


2. Background using SSH technology + POI components to achieve the Excel export function (1) Export function to achieve the source code as follows:

Import Java.io.OutputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.List;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFFont;
Import Org.apache.poi.hssf.usermodel.HSSFHeader;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;

Import Org.apache.poi.hssf.util.HSSFColor; /** * Use open Source Component POI export Excel document * @param <T> application generics, representing any JavaBean-style class * * * public class Exportexcelutil<t> {//exc
        The name of the sheet in El is private String sheetname = "Report Content";
	Public Exportexcelutil () {super ();
		Public Exportexcelutil (String sheetname) {super ();
	This.sheetname = SheetName;            /** * This is a common method that utilizes the Java reflection mechanism to output data placed in a Java collection and symbols of a certain condition in Excel to a specified IO device * @param headers * Table Property Columns Array Group * @param dataseT * A collection of data that needs to be displayed, and the collection must place objects that conform to the JavaBean-style class. * @param response * A Stream object associated with the output device that can export an Excel document to a local file or network/@SuppressWarnings ("unchecked") publ  
        IC void Setexcelcontents (string[] headers,list<vmreport> dataset,httpservletresponse response) {//Create a new Excel  
        Hssfworkbook workbook = new Hssfworkbook ();
        Create sheet page Hssfsheet sheet = workbook.createsheet (sheetname);
        Sets the table default column width to 25 bytes sheet.setdefaultcolumnwidth ((short) 25);  
        Create Header page Hssfheader Header = Sheet.getheader ();  
        
        Set the title centered Header.setcenter ("title");
        Generate a style Hssfcellstyle style = Workbook.createcellstyle ();
        Set these styles Style.setfillforegroundcolor (HSSFColor.SKY_BLUE.index);
        Style.setfillpattern (Hssfcellstyle.solid_foreground);
        Style.setborderbottom (Hssfcellstyle.border_thin);
        Style.setborderleft (Hssfcellstyle.border_thin); Style.sEtborderright (Hssfcellstyle.border_thin);
        Style.setbordertop (Hssfcellstyle.border_thin);
        Style.setalignment (Hssfcellstyle.align_center);
        Generate a font Hssffont font = Workbook.createfont ();
        Font.setcolor (HSSFColor.VIOLET.index);
        Font.setfontheightinpoints ((short) 12);
        Font.setboldweight (Hssffont.boldweight_bold);
        
        Apply the font to the current style style.setfont (font);
        Build and set another style Hssfcellstyle Style2 = Workbook.createcellstyle ();
        Style2.setfillforegroundcolor (HSSFColor.LIGHT_YELLOW.index);
        Style2.setfillpattern (Hssfcellstyle.solid_foreground);
        Style2.setborderbottom (Hssfcellstyle.border_thin);
        Style2.setborderleft (Hssfcellstyle.border_thin);
        Style2.setborderright (Hssfcellstyle.border_thin);
        Style2.setbordertop (Hssfcellstyle.border_thin);
        Style2.setalignment (Hssfcellstyle.align_center);
    Style2.setverticalalignment (Hssfcellstyle.vertical_center);    Generate another font Hssffont Font2 = Workbook.createfont ();
        Font2.setboldweight (Hssffont.boldweight_normal);
        
        Apply the font to the current style Style2.setfont (Font2);  
        Sets the first behavior header hssfrow row = sheet.createrow (0);
             Sets the content and style for the first row for (int i=0;i 

(2) The call at the action of Struts

Public String Exportvminfo () {
		//parsing the parameters of the foreground pass//
		...
		Gets the data in the database, where the dataset is data obtained from the database. For the sake of secrecy, no writing.
		list<vmreport> dataSet = null;
		
		String title= The name of the report sheet ... ";
		String[] headers = {"Name", "IP Address", "Highest CPU utilization", "Lowest CPU utilization", "highest memory utilization", "Lowest memory Utilization"};
		Gets the response object
		httpservletresponse response = Servletactioncontext.getresponse ();
		Export data to Excel
		exportexcelutil<vmreport> exportexcelutil = new exportexcelutil<vmreport> ();
		Exportexcelutil.setsheetname (title);
		Exportexcelutil.setexcelcontents (headers, dataset,response);
		Returns null only to return
		null correctly;
	}

(3) Struts XML configuration file

<action name= "Exportvminfo" class= "vmreportaction" method= "Exportvminfo" >
	 <result type= "Stream" >
	 </result>
</action>

3. Foreground ExtJS call, realize Excel's export

Window.location.href = ' vmreport/exportvminfo.do? ' +
    	   ' starttime= ' + starttime +
    	   ' &endtime= ' + endtime +
    	   ' &metrictype= ' + metrictype;  

Directly prompts the user whether to save or open this document


To open a saved file:


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.