Java Export Excel Report

Source: Internet
Author: User

1. Using the SSM framework, MAVEN builds the project

2. Using Jxls

3. Maven dependency:

<dependency>

<groupId>net.sf.jxls</groupId>

<artifactid>jxls-core</artifactid>

<version>1.0.5</version>

</dependency>

<dependency>

<groupId>net.sf.jxls</groupId>

<artifactid>jxls-reader</artifactid>

<version>1.0.5</version>

</dependency>

4. Code implementation

@RequestMapping("/alarmtopdf")

@ResponseBody

Public ajaxresult alarmtopdf (httpservletrequest req,httpservletresponse response) throws Oprexception, parseexception{

ajaxresult ajaxresult = new ajaxresult ();

list<realdata> List = (list<realdata>) Securityutils.getsubject (). GetSession (). GetAttribute ("LM");

HashMap map = (HashMap) Securityutils.getsubject (). GetSession (). getattribute ("Pmap");

HashMap beans = new HashMap ();

String fileflag = string.valueof (System.currenttimemillis ());

String datainfo = "";

String path = req.getsession (). Getservletcontext (). Getrealpath ("/");

String userInfo = null;

String templatedir = path+ "/excel-template/historyalarm.xls"; //

simpledateformat SF = new SimpleDateFormat ("Yyyymmddhhmmss");

fileflag = Sf.format (new Date ());

String outPutDir = path+ "/outfile/report alarm data"+Fileflag+ ". xls";

inputstream inputstream = null;

xlstransformer xls = new Xlstransformer ();

beans. Put ("list", list);

beans. Put ("map", map);

try {

xls. Transformxls (templatedir, Beans, OutPutDir);

userInfo = "/outfile/report Alarm data"+Fileflag+ ". xls";

} catch (Parsepropertyexception e) {

e.printstacktrace ();

} catch (Invalidformatexception e) {

e.printstacktrace ();

} catch (IOException e) {

e.printstacktrace ();

}

ajaxresult result = Ajaxresult. Success (userInfo);

return result;

}

Analysis

When doing this encounter a pit: must have this, otherwise it will not appear in Excel file

Excel Template: Jstl expression implementation

Java Export Excel Report

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.