Java Browser download file for Excel (SPRINGMVC mode)

Source: Internet
Author: User

Methods in action

/**
* Excel file Download processing
* @return
      */
@RequestMapping ("/downloanexcel")
Public Modelandview Downloanexcel () {
list<aucontract> List = new arraylist<aucontract> ();
          
list= service.findaucontractlist ();//Get all the contract collections from the database
map<string,list<aucontract>> Map = new hashmap<string, list<aucontract>> ();
map.put ("infolist", list);
Excelview ve = new Excelview ();
return new Modelandview (VE,MAP);
        } 

Excel Action Tool Class

Excelview

Package com.ekyb.common.auContract.action;

import Java.io.OutputStream;
import Java.text.SimpleDateFormat;
import java.util.List;
import Java.util.Map;

import javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;

import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
import Org.apache.poi.hssf.usermodel.HSSFFont;
import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
import Org.apache.poi.hssf.util.HSSFColor;
import Org.apache.poi.ss.usermodel.Cell;
import Org.apache.poi.ss.usermodel.Row;
import Org.apache.poi.ss.usermodel.Sheet;
import Org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ekyb.common.auContract.entity.AuContract;
import Com.ekyb.common.util.CharEncodingEdit;

/**
* Download Excel View
 *
* @author GX
 */
Public class Excelview extends Abstractexcelview {

@SuppressWarnings ("static-access")
protected void Buildexceldocument (map<string, object> model, Hssfworkbook workbook, HttpServletRequest Reque St,
httpservletresponse response) throws Exception {
        
@SuppressWarnings ("unchecked")
list<aucontract> List = (list<aucontract>) model.get ("Infolist");
        
Hssffont font= Workbook.createfont ();
font.setfontheightinpoints ((short) 12); Set the size of the font
font.setfontname ("Microsoft Jas Black"); Set the style of the font, such as: Arial, Microsoft ya black, etc.
Font.setitalic (FALSE); Italic true is italic
font.setboldweight (Hssffont.boldweight_bold); Bold in the text
Font.setcolor (HSSFColor.PINK.index); Set the color of a font
Hssfcellstyle style = Workbook.createcellstyle ();
Style.setfont (font);
        
if (list = null && list.size ()! = 0) {
int length = List.size ();
Sheet Sheet = Workbook.createsheet ();
        
//First line text description
Row row = Sheet.createrow (0);
cell cell = Row.createcell (0, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("contract name");

cell = Row.createcell (1, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("Contract unit");

cell = Row.createcell (2, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("Time of Contract registration");
            
cell = Row.createcell (3, cell.cell_type_string);
Cell.setcellstyle (style);
cell.setcellvalue ("Contract Amount");

cell = Row.createcell (4, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("Performance Method");

cell = Row.createcell (5, cell.cell_type_string);
Cell.setcellstyle (style);
cell.setcellvalue ("contract type");

cell = Row.createcell (6, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("Start Time");

cell = Row.createcell (7, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("End Time");

cell = Row.createcell (8, cell.cell_type_string);
Cell.setcellstyle (style);
Cell.setcellvalue ("Remarks");

//Below is the specific content
for (int i = 0; i < length; i++) {
Sheet.setcolumnwidth (short) I, (short) (35.7 *));
row = Sheet.createrow (i + 1);
//Contract name
cell = Row.createcell (0, cell. cell_type_string);
Cell.setcellvalue (List.get (i). GetName ());
//Contract units
cell = Row.createcell (1, cell. cell_type_string);
Cell.setcellvalue (List.get (i). Getunit ());

//Contract Registration Time
cell = Row.createcell (2, cell. cell_type_string);
Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (List.get (i). Getreg_time ()));

                
//Contract Amount
cell = Row.createcell (3, cell. cell_type_string);
//Convert a Float object to a string object
float Con_money=list.get (i). Getcon_money ();
String str =string.valueof (Con_money);
Cell.setcellvalue (str);

//Contract Fulfillment Method
cell = Row.createcell (4, cell. cell_type_string);
Cell.setcellvalue (List.get (i). Getperform_style ());

//Contract type
cell = Row.createcell (5, cell. cell_type_string);
Cell.setcellvalue (List.get (i). Getcon_type ());

//Start time
cell = Row.createcell (6, cell. cell_type_string);
Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (List.get (i). Getcon_start_time ()));

//End time
cell = Row.createcell (7, cell. cell_type_string);
Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (List.get (i). Getcon_end_time ()));

//Notes
cell = Row.createcell (8, cell. cell_type_string);
Cell.setcellvalue (List.get (i). Getremark ());
            }

//web Browse by MIME type to determine if a file is an Excel type
Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8");
response.setcharacterencoding ("Utf-8");

//processing the file name. Prevent garbled file names
String fileName = charencodingedit.processfilename (Request, "contract. xls");
///Content-disposition property is set to download as Attachment
Response.setheader ("Content-disposition", "attachment;filename=" + filename);
outputstream OS = Response.getoutputstream ();
workbook.write (OS);
Os.flush ();
os.close ();
        }
    }
}

JSP page code with the jQueryUI

<a href= "<%=basepath%>/aucontract/downloanexcel" class= "Easyui-linkbutton" data-options= "plain: ' true ', Iconcls: ' icon-down ' > Download excel</a>

You can directly connect to the URL map address in the action directly via the A-label href

Java Browser download file for Excel (SPRINGMVC mode)

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.