大家好,又见面了,我是你们的朋友全栈君。
把页面的数据导出excel 数据
然后进入 struts2 action
<action name="generateExcel" class="com.xx.emidas.activity.activity.ajax.XLSTransformerGenerateExcelAction">
</action>
package com.xx.emidas.activity.activity.ajax;
import com.xx.combiz.spring.util.LionConfigUtils;
import com.xx.emidas.activity.external.util.XLSTransformerGenerateExcelUtil;
import com.xx.emidas.activity.picture.BaseAction;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
public class XLSTransformerGenerateExcelAction extends BaseAction {
private String url;
private String name;
/**活动ID*/
private int activityId;
/**查询起始日期*/
private String queryStart;
/**查询结束日期*/
private String queryend;
/**
*
*/
private static final long serialVersionUID = 2058361031969467470L;
@Override
public String execute() throws Exception {
try {
//url="http://localhost:8080/activities/activity/newClickdate"+"?activityId="+activityId+"&queryStart="+queryStart+"&queryend="+queryend;
url = LionConfigUtils.getProperty("emidas-event.activity.data.excelExport", "0");
url+="?activityId="+activityId+"&queryStart="+queryStart+"&queryend="+queryend;
XLSTransformerGenerateExcelUtil.ExportExcel(url, name);
} catch (Exception e) {
}
return null;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getActivityId() {
return activityId;
}
public void setActivityId(int activityId) {
this.activityId = activityId;
}
public String getQueryStart() {
return queryStart;
}
public void setQueryStart(String queryStart) {
this.queryStart = queryStart;
}
public String getQueryend() {
return queryend;
}
public void setQueryend(String queryend) {
this.queryend = queryend;
}
}
这里的 url 是你的 需要数据的页面 或者 action 图片也可以显示的
这里是我自己封装的XLSTransformer的工具类
package com.xx.emidas.activity.external.util;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;
public class XLSTransformerGenerateExcelUtil {
/**
* 导出页面数据生成 xlsx
* @author hongwang.zhang
* @date 20150826
* @param path url路径
* @param name excel名称
*/
public static void ExportExcel( String path,String name){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httppost = new HttpGet(path);
try {
// 发送请求,返回响应
HttpResponse response = httpClient.execute(httppost);
// 打印响应信息
HttpEntity entity = response.getEntity();
HttpServletResponse x = ServletActionContext.getResponse();
x.setContentType("application/vnd.ms-excel");
String titlename="data";
if(null!=name){
titlename=name;
}
x.setHeader("Content-Disposition", "attachment;filename="+new String((titlename).getBytes("gbk"),"iso8859-1")+".xlsx");
Writer w = null;
w = x.getWriter();
w.write(EntityUtils.toString(entity));
w.flush();
EntityUtils.consume(entity);
//
} catch (ClientProtocolException e) {
// 协议错误
e.printStackTrace();
} catch (IOException e) {
// 网络异常
e.printStackTrace();
}
}
}
导出来的基本会和页面上的数据 样式一样的,这里我自己做了一个独立的页面 对样式做了一些修改 导出的excel 和页面上的一模一样
是不是很吊呢 这里就是大概的代码了,如果有什么 不懂的可以在留言
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163869.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...