IE中的MVC中的Excel导出不允许打开多个窗口
|
这是继承自ActionResult的ExcelExport操作:
public class ExcelResult<Model> : ActionResult
{
string _fileName;
string _viewPath;
Model _model;
ControllerContext _context;
public ExcelResult(ControllerContext context, string viewPath, string fileName, Model model)
{
this._context = context;
this._fileName = fileName;
this._viewPath = viewPath;
this._model = model;
}
protected string RenderViewToString()
{
if (!_viewPath.EndsWith(\".aspx\"))
{
return _viewPath;
}
using (var writer = new StringWriter())
{
var view = new WebFormView(_context, _viewPath);
var vdd = new ViewDataDictionary<Model>(_model);
var viewCxt = new ViewContext(_context, view, vdd, new TempDataDictionary(), writer);
viewCxt.View.Render(viewCxt, writer);
return writer.ToString();
}
}
void WriteFile(string content)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.AddHeader(\"content-disposition\", \"attachment;filename=\\\"\" + _fileName + \"\\\"\");
context.Response.Charset = \"\";
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = \"application/ms-excel\";
context.Response.Write(RemoveImages(content));
context.Response.End();
}
public override void ExecuteResult(ControllerContext context)
{
string content = this.RenderViewToString();
this.WriteFile(content);
}
public static string RemoveImages(string html)
{
StringBuilder retval = new StringBuilder();
using (StringReader reader = new StringReader(html))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
if (!line.StartsWith(\"<img\"))
{
retval.Append(line);
}
}
} while (line != null);
}
return retval.ToString();
}
}
导出工作正常,但仅在IE中(在FF中有效),如果导出,则选择打开文件而不是保存文件,然后立即再次单击“导出”,它将尝试打开具有相同名称的另一个文件,因此在关闭工作文档之前,Excel不允许您这样做。
但是,在FF中,名称仅添加一个整数,每次单击导出时该整数将递增1。
我必须怎么做才能在IE中实现相同的功能?
没有找到相关结果
已邀请:
1 个回复
翁茄口霉氖