/*Word、Excel先保存再输出-下载*/ 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();
//以html table 的方式输出Excel string strMNName = string.Format("{0}{1}({2}到{3})综合报表", listMN[0].UnitsName, listMN[0].MNName, DateTime.Parse(strST).ToString("yyyy年MM月dd日"), DateTime.Parse(strET).ToString("yyyy年MM月dd日")); stBuilder.Append("<style type='text/css'>tr,td{border:solid .5pt Black;}</style>"); stBuilder.Append(string.Format("<table><tr height=24 style='font-size:10pt;text-align: center;font-weight:bolder;'><td colspan={0}>{1}</td></tr>", iRow, strMNName)); stBuilder.Append(stTitle.); stBuilder.Append(stContent); stBuilder.Append("</table>"); System.IO.StringWriter sw = new System.IO.StringWriter(stBuilder); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strMNName, Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write(sw); HttpContext.Current.Response.End();
Excel xls最大行65536行,xlsx最大行1048576行;
Xlsx格式输出问题
因Aspose.Cells Workbook Response.BinaryWrite(workbook.SaveToStream().ToArray()); 输出流输出sheet页最大行65536行;可改为先存储文件,再以url连接下载或文件流的方式输出
//输出xlsx格式 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //"application/ms-excel";
//保存xlsx文件--可用上述方法(FileStream)输出 workbook.Save("C:\Test.xlsx", SaveFormat.Xlsx);
文件流输出方式参考GetExcelFile 或 DownLoadFile方法
Asp.Net输出Excel Xlsx
//Asp.Net输出Excel Xlsx HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.ExpCellNameToXLSX = true; saveOptions.SaveFormat = SaveFormat.Xlsx; //SaveOptions saveOptions1 = null; //saveOptions1.ExpCellNameToXLSX = true; //saveOptions1.SaveFormat = SaveFormat.Xlsx; //saveOptions.Encoding= Encoding.UTF8; //workbook.Save("D://1.xlsx", FileFormatType.Xlsx, SaveType.Default, Response); workbook.Save(Response, "1.xlsx", ContentDisposition.Inline, saveOptions); Response.Flush(); Response.Close(); Response.End();