jquery ajax调用后台导出excel,后台顺利跑完无效?
functionexceldownload(_name){varurl=basehref+'basicexport/exceldownload';$.ajax({url:...
function exceldownload(_name){
var url = basehref + 'basicexport/exceldownload';
$.ajax({
url : url,
data : {
path:'/resources/excel/' + _name + '.xls'
},
type : 'post',
context: document.body,
contentType : 'application/x-www-form-urlencoded',
dataType : 'text',
success : function(data) {
}
});
} 展开
var url = basehref + 'basicexport/exceldownload';
$.ajax({
url : url,
data : {
path:'/resources/excel/' + _name + '.xls'
},
type : 'post',
context: document.body,
contentType : 'application/x-www-form-urlencoded',
dataType : 'text',
success : function(data) {
}
});
} 展开
5个回答
展开全部
public static void queryexcel(String filename,HttpServletRequest request,HttpServletResponse response) throws Exception {
HSSFWorkbook workbook = null;
//读取表头(表字段名)
String[] titles = (String[]) request.getAttribute("titles");
//读取数据库数据
String [] info=(String[]) request.getAttribute("info");
// 创建工作簿实例
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
workbook.getSheet(null);
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)14);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
style.setWrapText(true);
//加载表头数据
HSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = titleRow.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(titles[i].toString());
}
if (info != null) {
HSSFRow dataRow = sheet.createRow(1);
for (int i = 0; i < info.length; i++) {
HSSFCell cell = dataRow.createCell((short)i);
sheet.setColumnWidth((short)i, (short)3500);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(info[i]);
}
}
//通过数据流下载工作簿
try {
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition",
"attachment;filename="+toUtf8String(filename+".xls"));
response.setContentType("text ml;charset=utf-8");
request.setCharacterEncoding("UTF-8");
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//中文名
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("UTF-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
HSSFWorkbook workbook = null;
//读取表头(表字段名)
String[] titles = (String[]) request.getAttribute("titles");
//读取数据库数据
String [] info=(String[]) request.getAttribute("info");
// 创建工作簿实例
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
workbook.getSheet(null);
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)14);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
style.setWrapText(true);
//加载表头数据
HSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = titleRow.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(titles[i].toString());
}
if (info != null) {
HSSFRow dataRow = sheet.createRow(1);
for (int i = 0; i < info.length; i++) {
HSSFCell cell = dataRow.createCell((short)i);
sheet.setColumnWidth((short)i, (short)3500);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(info[i]);
}
}
//通过数据流下载工作簿
try {
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition",
"attachment;filename="+toUtf8String(filename+".xls"));
response.setContentType("text ml;charset=utf-8");
request.setCharacterEncoding("UTF-8");
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//中文名
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("UTF-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
展开全部
去掉location.href跳转语句看下。这种跳转了和后续代码执行有冲突,是否执行得看浏览器怎么控制跳转的了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不用ajax请求, 用传统的方式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、猎人去打猎,打中了大雁,最后没得到它,不在于大雁本身中没中枪,在于它到猎人之间的路太曲折,没找到。 2、对于前台来说,只要后台报错,它就执行fail分支。而不管,后台干了什么。好比只要找不到猎物,猎人就算失败,而不管猎物死没死。 3...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
debug看后台excel工作表sheet有数据?
更多追问追答
追问
我只想完成服务器上excel的下载,不对excel做任何处理。
debug服务端的java代码跑完没有报错,
但是前台没有弹出下载框?
追答
为什么不在服务器端设置输出头呢。这样用流输出就能弹出下载框了。。
就像如下:
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();
// 设定输出文件头
response.setHeader("Content-disposition", "attachment; filename="+new String((yourFileName).getBytes("GB2312"),"8859_1")+".xls");
// 定义输出类型
response.setContentType("application/msexcel");
// 写入文件
wbook.write();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询