@RequestMapping(value =ActionUrlManage.ExportG1030Action, method = RequestMethod.POST)
public void exportExcelG1030Controller(HttpServletRequest request,HttpServletResponse response,
ModelMap modelMap) throws Exception {
List<String> list = new ArrayList<String>();
list.add("1"); //test
list.add("2");
HSSFWorkbook wb = gi.getUserExcel(list);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=student.xls");
OutputStream ouputStream = response.getOutputStream();//new FileOutputStream(new File(path+"a.xls"));
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();}
点按钮不弹出下载对话框是为毛呢! 谢谢!
FileInputStream input=new FileInputStream(file);
OutputStream output=response.getOutputStream();
//有的游览器对下面的可能不支持,就可以重新设置一个头response.setHeader("content-type","application/x-gzip");
//encode方法只对部分IE有效,想要对火狐等游览器有效,可以将filename转成ISO8859-1的编码之后在打给游览器
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realFilename,"UTF-8"));
byte[] b=new byte[1024];
int len=0;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
input.close();
output.close();
要改成wondow.location
String filename=request.getParameter("filename");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition" ,"inline; filename="+filename);
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in=null;
ServletOutputStream outexcel = null;
HSSFWorkbook wb = new HSSFWorkbook();
Complete comp=new Complete();
DutyTable dutytable=new DutyTable();
List <DutyTable> DTlist=new ArrayList<DutyTable>();
dutytable.setDate("9月16日");
dutytable.setRes("");
dutytable.setSign(" ");
dutytable.setWatcher("往哪");
dutytable.setWeekDay("星期一");
DTlist.add(dutytable);
int rownum = 0;
wb=comp.creat(rownum,wb,DTlist);
wb.write(out);
out.flush();
byte[] aa = out.toByteArray();
in = new ByteArrayInputStream(aa, 0, aa.length);
outexcel=response.getOutputStream();
outexcel.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
outexcel.write(aRead);
}
in.close();
out.close();
outexcel.close();
用的是ext,这样可以么tbar:[
{text:'导出',iconCls:'del',handler:function(){
var Form = Ext.create('Ext.form.Panel',{
url: './export'+record.raw.name+'.action',
onSubmit:Ext.emptyFn,//非AJAX提交
submit:function()
{
this.getForm().getEl().dom.submit();
}
});
Form.getForm().submit();
}
},
if ($('#downloadcsv').length <= 0)
$('body')
.append("<iframe id=\"downloadcsv\" style=\"display:none\"></iframe>");
$('#downloadcsv').attr('src', url);
用的是ext,这样可以么tbar:[
{text:'导出',iconCls:'del',handler:function(){
var Form = Ext.create('Ext.form.Panel',{
url: './export'+record.raw.name+'.action',
onSubmit:Ext.emptyFn,//非AJAX提交
submit:function()
{
this.getForm().getEl().dom.submit();
}
});
Form.getForm().submit();
}
},
哥们,我用ext做了一个,前台面板是这么设计的,供你参考下
var fpFileUpload=new Ext.FormPanel({
id:'fpFileUpload',
frame:true,
fileUpload:true,
//url:'Default.aspx',
items:[
{
xtype:'textfield',
allowBlank:false,
fieldLabel:'选择文件',
inputType:'file',
name:'excelFile'//后台封装名为excelFile属性
}
],
buttonAlign:'center',
buttons:[
{
text:'上传',
handler:function(){
if(fpFileUpload.form.isValid()){
fpFileUpload.form.submit({
url:'importFromExcel4Wage.action',//这里的名称需要修改,具体和陈巍讨论。
method:'post',
waitTitle:'请等待',
waitMsg:'文件上传中...',
success: function(form,action) {
Ext.Msg.alert("系统提示", action.result.msg);
winFielUpload.hide();
//grid.store.load();
},
failure: function(form,action) {
Ext.Msg.alert("系统提示",action.result.msg);
winFielUpload.hide();
}
});
}else{
Ext.Msg.alert("系统提示","请选择文件后再上传!");
}
}
},
{
text:'取消',
handler:function(){
winFielUpload.hide();
}
}
]
});
解决方式如下:使用GET提交,不用ajax提交,使用window.href提交。