Http://www.blogjava.net/usherlight/archive/2008/06/23/210135.html
When displaying query results on a webpage, you may need to export the results to an Excel file. In this way, you can customize the output format and content (cells and formulas cannot be merged) to generate an Excel file in a real Excel format (not a CSV file.
1. Struts. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Constant name = "struts. i18n. encoding" value = "UTF-8"/>
<Package name = "Demo" extends = "struts-Default">
<Action name = "Excel" method = "execute" class = "demo. excelaction">
<Result name = "Excel" type = "stream">
<Param name = "contenttype"> application/vnd. MS-Excel </param> <! -- Note the contenttype here -->
<Param name = "inputname"> excelstream </param> <! -- It must be the same as the variable name in action -->
<Param name = "contentdisposition"> filename = "standard.xls" </param>
<Param name = "buffersize"> 1024 </param>
</Result>
</Action>
</Package>
</Struts>
2. struts2 action
Package demo;
Public class excelaction {
Private inputstream excelstream; // getter and setter must be generated.
Public String execute () throws exception {
Stringbuffer excelbuf = new stringbuffer ();
Excelbuf. append ("bookname "). append ("\ t "). append ("year "). append ("\ t "). append ("author "). append ("\ n ");
Excelbuf. append ("thinking in Java "). append ("\ t "). append ("2001 "). append ("\ t "). append ("Eckel "). append ("\ n ");
Excelbuf. append ("Spring in action "). append ("\ t "). append ("2005 "). append ("\ t "). append ("Rod "). append ("\ n ");
String excelstring = excelbuf. tostring ();
Logger. debug ("result Excel String:" + excelstring );
Excelstream = new bytearrayinputstream (excelstring. getbytes (), 0, excelstring. Length ());
Return "Excel ";
}
// Getter and setter
...
}
Iii. jsp page
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<S: Head/>
</Head>
<Body>
<S: Form Action = "" method = "Post">
<S: Submit key = "button. Submit"/>
</S: Form>
</Body>
</Html>