/*Word先保存再输出-下载*/strReportFilePath = Server.MapPath("~") + strReportFilePath; doc.Save(strReportFilePath); if (!string.IsNullOrEmpty(strReportFilePath)) { string NewFile = strReportFilePath;// Server.MapPath(strReportFilePath); //如果文件不存在,可能需要执行重新生成 FileStream fs = new FileStream(NewFile, FileMode.Open); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string strFileName = strReportName + ".docx"; //判断浏览器类型 如果是IE文件名转编码 string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower(); //IE浏览器 if (curBrowser.IndexOf("explorer", StringComparison.Ordinal) >= 0 || curBrowser.IndexOf("ie", StringComparison.Ordinal) >= 0) { strFileName = HttpUtility.UrlEncode(strFileName, Encoding.UTF8); } Response.ContentType = "application/ms-word"; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" }); Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName); Response.OutputStream.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); Response.Flush(); return; }
/*Word直接输出*/ MemoryStream mStream = new MemoryStream(); doc.Save(mStream, Aspose.Words.SaveFormat.Doc);HttpContext.Current.Response.BinaryWrite(mStream.ToArray());
/*Excel导出*/Workbook workbook = new Workbook();/****/ HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "utf-8"; Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" }); //判断浏览器类型 如果是IE文件名转编码 string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower(); //IE浏览器 if (curBrowser.IndexOf("explorer", StringComparison.Ordinal) >= 0 || curBrowser.IndexOf("ie", StringComparison.Ordinal) >= 0) { strName = HttpUtility.UrlEncode(strName, Encoding.UTF8); } HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", strName)); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.BinaryWrite(workbook.SaveToStream().ToArray()); HttpContext.Current.Response.End();