前台是JavaScript,后台是Java,中间是dwr+spring
调出Servlet/JSP容器了的对象,可以通过两个特殊类来获得,org.directwebremoting.WebContext和org.directwebremoting.WebContextFactory使用它里面的静态方法
org.directwebremoting.WebContext wc=org.directwebremoting.WebContextFactory.get();
javax.servlet.http.HttpServletResponse response=wc.getHttpServletResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");// 定义输出类型
response.setHeader("Content-disposition", "attachment; filename=\""+tmpFileName+"\"");// 设定输出文件头
byte[] tmpBytes = tmpContent.getBytes();
System.out.println(tmpBytes.length);
int len=tmpBytes.length-1;
byte[] buf=new byte[len];
System.out.println(buf.length);
ServletOutputStream out = response.getOutputStream();// 取得输出流对象
out.write(tmpBytes, 0, len);
但是客户端一点反应都没有,好像这个response没起作用,文件没有传到客户端。
小弟不才,请牛人们赐教啊!
导出分TXT,ELS
package com.xxx.xxxx.extinterface;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.util.*;
...
import com.kingter.common.util.StringUtil;
import org.apache.poi.hssf.usermodel.*;
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
public class MarkPlanPhoneOutServlet
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//获取基础参数
String saleProgramID = StringUtil.replaceNull(request.getParameter(
"saleProgramID"));
String condition = StringUtil.replaceNull(request.getParameter(
"condition"));
String ext = StringUtil.replaceNull(request.getParameter("ext"));
//验证用户的下载是否合法
//查找相应的数据
//数据展现
//saleProID,条件,方式
Employee employee = (Employee) request.getSession().getAttribute("employeeBean");
//验证用户的下载是否合法
//String companyID = employee.getCompanyID();
MarkPlanPhoneBean markPlanPhoneBean = new MarkPlanPhoneBean();
// boolean flag = markPlanPhoneBean.validateUser(companyID, saleProgramID);
boolean flag = true;
StringBuffer mobile_string = null;
FileManager fileManager = new FileManager();
if (flag) { //验证通过
String[] con = condition.split(",");
//01,02,03,04,05
//接收成功(有状态报告),接收失败(有状态,非1000),待状态报告,发送失败(端口拒绝),过滤
if (ext.equals("0")) { //txt
ext = ".txt";
}
else if (ext.equals("1")) { //excel
ext = ".xls";
}
//生成下载文件名
Random random = new Random();
int x = 1000 + random.nextInt(9000); //四位随机数
String fileName = saleProgramID + "_" + x + ext;
// System.out.println("fileName = " + fileName);
int count00 = 0; //导出单量
int count = 0; //导出总量
int num = 0;
SendCountService sendCountService = SendCountService.getInstance();
SaleProgram saleProgram = new SaleProgram();
SaleProgramService saleProgramService = SaleProgramService.
getInstance();
saleProgram = saleProgramService.getSalePrograByProgramID(
saleProgramID);
String copyMmsID = saleProgram.getCgroupID();
SendCount sendCount = new SendCount();
System.out.println("con.length = " + con.length);
mobile_string = new StringBuffer();
// mobile_string.append("号码" + "," + "状态" + "\r\n");
// out.print(mobile_string);
if (ext.equals(".txt")) { //下载文件是txt格式
for (int i = 0; i < con.length; i++) {
String conName = markPlanPhoneBean.idToName(con[i]);
PrintWriter out = response.getWriter();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition",
"attachment; filename=" + fileName);
if (con[i].equals("05")) { //过滤掉的数据
SendFilterService sendFilterService = SendFilterService.
getInstance();
count00 = sendFilterService.
getSendFilterCountByProgramID(saleProgramID);
count = +count00;
List list = sendFilterService.getSendFilterByProgramID(
saleProgramID);
SendFilter sendFilter = null;
mobile_string = new StringBuffer();
mobile_string.append("====被过滤的号码共" + list.size() +
"个====" + "\r\n");
out.print(mobile_string);
if (list.size() != 0) {
mobile_string = new StringBuffer();
for (int j = 0; j < list.size(); j++) {
sendFilter = new SendFilter();
sendFilter = (SendFilter) list.get(j);
mobile_string.append(sendFilter.getAcceptMobile() + "\r\n");
}
out.print(mobile_string);
}
}
else { //接收成功(有状态报告),接收失败(有状态,非1000),待状态报告,发送失败(端口拒绝)
count00 = sendCountService.get39SendCountNum(copyMmsID,
con[i]);
System.out.println("count00 = " + count00);
count = count + count00;
num = (count00 / 5000) + 1;
System.out.println("num = " + num);
List list = null;
int beginnum = 0;
int linenum = 5000;
mobile_string = new StringBuffer();
mobile_string.append("====" + conName + "共" + count00 +
"个====" + "\r\n");
out.print(mobile_string);
for (int j = 0; j < num; j++) {
beginnum = j * 5000;
list = new ArrayList();
list = sendCountService.get39SendCountNumList(
copyMmsID, con[i], beginnum, linenum);
System.out.println("list.size = " + list.size());
if (list.size() != 0) {
mobile_string = new StringBuffer();
for (int c = 0; c < list.size(); c++) {
sendCount = (SendCount) list.get(c);
mobile_string.append(sendCount.
getDestMobileNo() + "," +
"\r\n");
}
out.print(mobile_string);
}
}
}
}
}
else if (ext.equals(".xls")) { //下载文件是excel格式
try {
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + fileName +
"\"");
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
for (int i = 0; i < con.length; i++) {
String conName = markPlanPhoneBean.idToName(con[i]);
if (con[i].equals("05")) { //过滤掉的数据
SendFilterService sendFilterService =
SendFilterService.getInstance();
List list = sendFilterService.
getSendFilterByProgramID(saleProgramID);
conName = conName+"("+list.size()+")";
WritableSheet wsheet = wwb.createSheet(conName, i);
//设置表头
Label label = new Label(0, 0, "");
wsheet.addCell(label);
label = new Label(0, 0, "号码");
count00 = sendFilterService.
getSendFilterCountByProgramID(saleProgramID);
count = +count00;
SendFilter sendFilter = null;
if (list.size() != 0) {
mobile_string = new StringBuffer();
for (int j = 0; j < list.size(); j++) {
sendFilter = new SendFilter();
sendFilter = (SendFilter) list.get(j);
label = new Label(0, (j + 1),
sendFilter.getAcceptMobile());
wsheet.addCell(label);
}
}
}
else { //接收成功(有状态报告),接收失败(有状态,非1000),待状态报告,发送失败(端口拒绝)
System.out.println(
"===================================");
System.out.println(conName);
count00 = sendCountService.get39SendCountNum(
copyMmsID, con[i]);
conName = conName+"("+count00+")";
WritableSheet wsheet = wwb.createSheet(conName, i);
//设置表头
Label label = new Label(0, 0, "号码");
wsheet.addCell(label);
System.out.println("count00 = " + count00);
count = count + count00;
num = (count00 / 5000) + 1;
System.out.println("num = " + num);
List list = null;
int beginnum = 0;
int linenum = 5000;
for (int j = 0; j < num; j++) {
beginnum = j * 5000;
list = new ArrayList();
list = sendCountService.get39SendCountNumList(
copyMmsID, con[i], beginnum, linenum);
System.out.println("list.size = " + list.size());
if (list.size() != 0) {
mobile_string = new StringBuffer();
for (int c = 0; c < list.size(); c++) {
sendCount = (SendCount) list.get(c);
label = new Label(0, (beginnum + c + 1),
sendCount.getDestMobileNo());
wsheet.addCell(label);
}
}
}
System.out.println(
"|||||||||||||||||||||||||||||||||||||||||||||||||||");
}
}
wwb.write();
wwb.close();
os.close();
response.flushBuffer();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
mobile_string = new StringBuffer();
mobile_string.append("总导出量:" + count);
System.out.println(mobile_string);
}
else {
mobile_string = new StringBuffer();
mobile_string.append("非法下载,请登陆后操作!");
System.out.println(mobile_string);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
}
out.write(tmpBytes, 0, len);
在这后,加下一个out.flush();
你去我的博客看看,我那也有这EXCEL导出的!
如果DEBUG测试结果流没问题的话,还有可能是编码问题!
我也用过DWR,给下我的实现方式吧,不过也只是部分代码。
JS
function exportData(index,id,name) {
showDialog("任务数据导出",addhtmlStr,'success');
dwr.util.setValue("proName",name);
dwr.util.setValue("saleProID",id);
}
function expGo() {
if(!checkData()) {
return ;
}
var programID = $("saleProID").value;
var ext = $("way").value;
var condition = getvalue();
//alert("condition = "+condition);
window.location.href("/mas/servlet/markPlanPhoneOutServlet?saleProgramID="+programID+"&condition="+condition+"&ext="+ext);
hideDialog();
}
WEB.xml 配置servlet
markPlanPhoneOutServlet
com.xx.xx.extinterface.MarkPlanPhoneOutServlet