freeMark导出报表可以通过office制作模板,然后就可以根据模板格式导出文件了,灰常的简单方便。适合做单条或少数几条的数据展示,如果数据多的话像列表形式可以考虑用POI,如果需要生成动态表格或者导出图表之类的可以试试jasper。我以后也会慢慢更新这些内容的~~~
模板示例:我这里导出的excel文件,所以用excel画的模板,需要赋值的地方用EL表达式的方式赋值,前面的test是我的实体对象,后面对应其相应的属性。没有test的地方是直接传的参数,例如第3行的年月。
画完模板后点击文件---另存为,保存为XML 点击表格 2003(*.xml)格式。然后找到你保存的xml文件,直接修改后缀名为.ftl,至此你的freeMark报表的模板就制作完成了~~(注意:必须是这个格式 用wps画模板的话没有2003的xml,导出后格式会有问题)。
接下来就是写js页面:
function exportExcel() {
var dataid = {"id" : $("#dataid").val()};
$.ajax({
url: "http://localhost:9090/SSH/index/exportExcel.action", //后台地址
data: dataid,
success: function(res) {
var data = eval(res);
var path = data.path; //文件路径
var showA = '<a style="display:none" id="downloadB" download="导出文件名.xls" href="'+path+'">下载</a>';
$(showA)[0].click();
setTimeout(function() { //删除临时文件
delTempFile(path);
}}, 2000)
}
});
}
//删除临时文件
function delTempFile(path) {
var paths = {"path":path};
$.ajax({
url: "http://localhost:9090/SSH/index/delTempFile.action",
data:paths,
});
}
Java后台:
package com.jialin.action;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.jialin.entity.User;
import com.jialin.service.IUserManage;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/index")
@ParentPackage("struts-default")
public class IndexAction extends ActionSupport{
/**
* 导出文件
*/
@Action(value = "exportExcel", results = {@Result(name = "input", location = "/success.jsp") })
public String top() {
Map<String, Object> map = new HashMap<String, Object>();
//获取前台传来的参数 查询相应的数据
String param = ServletActionContext.getParameter("dataid");
JSONObject json = JSONObject .fromObject(param);
String id = json.id;
Session session = sessionFactory.getCurrentSession();
List<Object> ob= session.createSQLQuery("select * from p_user where id = "+id).list();
CustomerBean test = new CustomerBean ();
test.setSettlementCompany(ob[0]==null ? "" : ob[0].toString());
test.setMeterAccountName(ob[1]==null ? "" : ob[1].toString());
test.setMeterAccountN(ob[2]==null ? "" : ob[2].toString());
test.setBank(ob[3]==null ? "" : ob[3].toString());
test.setAddress(ob[4]==null ? "" : ob[4].toString());
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String curDate = timeFormat.format(new Date());
String year = curDate.substring(0,4);
String month = curDate.substring(5,7);
//把实体和额外的参数放在map里
map.put("test",test);
map.put("year",year);
map.pur("month",month);
File file = createExcel(map,"fsfphcl.ftl",filename);
}
/**
* 创建文件
*/
public File createExcel(Map<?, ?> dataMap, String templateName,String filename) throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
Configuration configuration = null;
Map<String, Template> allTemplates =null;
String realPath = request.getRealPath("/").replace("\\","/");
String path = (realPath+"download\\").replace("\\","/"); //文件存放地址
try {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
//加载模板目录
configuration.setDirectoryForTemplateLoading(new File(realPath+"demon/template"));
} catch (Exception ex) {
ex.printStackTrace();
}
File file = new File(path+filename);
Template template = configuration.getTemplate(templateName,"utf-8");//防止模板中文乱码
try {
Writer w = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
template.process(dataMap, w);
w.flush();
w.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return file;
}
/**
* 删除临时文件
*/
@Action(value = "delTempFile", results = {@Result(name = "input", location = "/success.jsp") })
public void delTempFile() {
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getParameter("path");
File file = new File(path.path);
file.delete();
}
}