Struts2 uses stream to directly output Excel

Source: Internet
Author: User

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>

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.