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