The focus of Excel Export

Source: Internet
Author: User

JSP page:

Export button:

<form id= "Excel" Name= "Exportform" method= "POST" action= "" style= "display:inline-block; height:18px; " ><!--padding-left:15px; -
<input type= "hidden" name= "Exceltext" id= "Exceltext"/>
<p align= "center" ><input type= "button" class= "btn Btn-xs" name= "exportbtn" onclick= "Excel ()" Value= "Export" /c0>/></p>
</form>

Export Button JS:

$ ("#excel"). Bind ("click", Function () {

if (Confirm ("Confirm export?") ")){
Window.location.href= "${contextpath}/activecode/toexcel";
}else{
Alert ("Export failed! ");
}
});

Code in Java background:

Export
@RequestMapping (value= "/toexceL ")
Public Modelandview Exportexcel () {
Modelandview mv = This.getmodelandview ();
Pagedata PD = new Pagedata ();
PD = This.getpagedata ();
try{
map<string,object> DataMap = new hashmap<string,object> ();
List<string> titles = new arraylist<string> ();
Titles.add ("ID");
Titles.add ("Activation Code"); 1
Titles.add ("category"); 2
Titles.add ("state"); 3
Titles.add ("Creation Time"); 4

Datamap.put ("titles", titles);

@SuppressWarnings ("unchecked")//I now have some problems with the value here, so I did a few settings until the for loop
list<pagedata> codeList = activecodeservice.getactivecodelist (PD);
int count = Codelist.size ();
String slist=json.tojsonstring (codeList);
Slist = slist.substring (1, Slist.length ()-1); Remove the first and last one
System.out.println ("Value of String type Slist:" +slist);
string[] arr = Slist.split (",");

list<string> list = Java.util.Arrays.asList (arr);
list<pagedata> varlist = new arraylist<pagedata> ();
System.out.println ("For list length:" +list.size ()); 15
for (int j=0;j<count;j++) {
Pagedata VPD = new Pagedata ();
int i=5; Data has several field names
Vpd.put ("Var1", List.get (j*i). substring (1, list.get (j*i). Length ()));
Vpd.put ("Var1", List.get (j*i+1). substring (1, list.get (j*i+1). Length ()-1);
Vpd.put ("Var2", List.get (j*i+2). substring (1, list.get (j*i+2). Length ()-1);
Vpd.put ("Var3", List.get (j*i+3). substring (1, list.get (j*i+3). Length ()-1);
Vpd.put ("Var4", List.get (j*i+4). substring (0, List.get (j*i+4). Length ()-1);

Varlist.add (VPD);
System.out.println ("varlist" +varlist);
}

Datamap.put ("Varlist", varlist);
System.out.println ("DataMap" +datamap);
Objectexcelview Erv = new Objectexcelview (); Perform Excel operations
MV = new Modelandview (ERV,DATAMAP);
SYSTEM.OUT.PRINTLN ("MV" +MV);
} catch (Exception e) {
E.printstacktrace ();
}
return MV;
}

Of course, the necessary classes and packages are still needed,

This is the class library file that operates on the export all Excel table:

@SuppressWarnings ("deprecation")
public class Objectexcelview extends abstractexcelview{

@Override
protected void Buildexceldocument (map<string, object> model,
Hssfworkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
TODO auto-generated Method Stub
Date date = new Date ();
String filename = tools.date2str (date, "Yyyymmddhhmmss");
Hssfsheet sheet;
Hssfcell cell;
Response.setcontenttype ("Application/octet-stream");
Response.setheader ("Content-disposition", "Attachment;filename=" +filename+ ". xls");
Sheet = Workbook.createsheet ("Sheet1");

List<string> titles = (list<string>) model.get ("titles");
int len = Titles.size ();
Hssfcellstyle HeaderStyle = Workbook.createcellstyle (); Heading styles
Headerstyle.setalignment (Hssfcellstyle.align_center);
Headerstyle.setverticalalignment (Hssfcellstyle.vertical_center);
Hssffont HeaderFont = Workbook.createfont (); Title Font
Headerfont.setboldweight (Hssffont.boldweight_bold);
Headerfont.setfontheightinpoints ((short) 11);
Headerstyle.setfont (HeaderFont);
Short width = 20,height=25*20;
Sheet.setdefaultcolumnwidth (width);
for (int i=0; i<len; i++) {//Set caption
String title = Titles.get (i);
Cell = Getcell (sheet, 0, i);
Cell.setcellstyle (HeaderStyle);
SetText (Cell,title);
}
Sheet.getrow (0). setheight (height);

Hssfcellstyle Contentstyle = Workbook.createcellstyle (); Content Style
Contentstyle.setalignment (Hssfcellstyle.align_center);
list<pagedata> varlist = (list<pagedata>) model.get ("varlist");
list<pagedata> varlist = (list<pagedata>) model.get ("varlist");
int varcount = Varlist.size ();
for (int i=0; i<varcount; i++) {
Pagedata VPD = Varlist.get (i);
for (int j=0;j<len;j++) {
String varstr = vpd.getstring ("var" + (j+1))! = null? Vpd.getstring ("var" + (j+1)): "";
Cell = Getcell (sheet, i+1, j);
Cell.setcellstyle (Contentstyle);
SetText (CELL,VARSTR);
}

}

}

This pagedata, is to use the data, the channel data through the Pagedata connection Objectexcelview class for export operations

public class Pagedata extends HashMap implements map{

Private static final long serialversionuid = 1L;

Map map = null;
HttpServletRequest request;

Public Pagedata (HttpServletRequest request) {
This.request = Request;
Map properties = Request.getparametermap ();
Map Returnmap = new HashMap ();
Iterator entries = Properties.entryset (). Iterator ();
Map.entry Entry;
String name = "";
String value = "";
while (Entries.hasnext ()) {
Entry = (map.entry) entries.next ();
Name = (String) entry.getkey ();
Object valueobj = Entry.getvalue ();
if (null = = Valueobj) {
Value = "";
}else if (valueobj instanceof string[]) {
String[] values = (string[]) valueobj;
for (int i=0;i<values.length;i++) {
Value = Values[i] + ",";
}
Value = value.substring (0, Value.length ()-1);
}else{
Value = Valueobj.tostring ();
}
Returnmap.put (name, value);
}
Map = Returnmap;
}

Public Pagedata () {
Map = new HashMap ();
}

@Override
public object get (object key) {
Object obj = null;
if (Map.get (key) instanceof object[]) {
Object[] arr = (object[]) map.get (key);
obj = Request = = null? Arr: (Request.getparameter (String) key) = = null? Arr:arr[0]);
} else {
obj = Map.get (key);
}
return obj;
}

Public String getString (Object key) {
Return (String) get (key);
}

@SuppressWarnings ("Unchecked")
@Override
public object put (object key, object value) {
Return Map.put (key, value);
}

@Override
public object Remove (object key) {
return Map.Remove (key);
}

public void Clear () {
Map.clear ();
}

public boolean ContainsKey (Object key) {
TODO auto-generated Method Stub
return Map.containskey (key);
}

public boolean Containsvalue (Object value) {
TODO auto-generated Method Stub
return Map.containsvalue (value);
}

Public Set EntrySet () {
TODO auto-generated Method Stub
return Map.entryset ();
}

public Boolean isEmpty () {
TODO auto-generated Method Stub
return Map.isempty ();
}

Public Set KeySet () {
TODO auto-generated Method Stub
return Map.keyset ();
}

@SuppressWarnings ("Unchecked")
public void Putall (Map t) {
TODO auto-generated Method Stub
Map.putall (t);
}

public int size () {
TODO auto-generated Method Stub
return Map.size ();
}

Public Collection values () {
TODO auto-generated Method Stub
return Map.values ();
}

The last is the tool class, in the file I submitted.

The focus of Excel Export

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.