找回密码
 立即注册

QQ登录

只需一步,快速开始

KinnSoft

高级会员

108

主题

494

帖子

1999

积分

高级会员

积分
1999

活字格认证微信认证勋章元老葡萄

[已处理] 导出Excel报错

KinnSoft
高级会员   /  发表于:2018-1-9 09:37  /   查看:2752  /  回复:1
我这边报表原来是先进行预览在进行另存为Excel的,现在我直接不预览直接另存为excel报错

  1. NameValueCollection filter = paras.ToNameValueCollection();
  2.             ActiveReportClass AReportViewer = new ActiveReportClass();
  3.             AReportViewer.isFormarPrint = false;
  4.             AReportViewer.Dr = CacheManager.GetCache<DataRow>(filter["drKey"].Kstring());
  5.             AReportViewer.ReportPath = AReportViewer.Dr["reportpath"].Kstring();
  6.             AReportViewer.ReportName = AReportViewer.Dr["bbname"].Kstring();
  7.             AReportViewer.Filter = System.Web.HttpUtility.ParseQueryString(paras);
  8.             //AReportViewer.Filter = CacheManager.GetCache<string>(paras).ToNameValueCollection();
  9.             //区域报表显示
  10.             if (AReportViewer.ReportPath.Contains("rpx"))
  11.             {
  12.                 AReportViewer.ReportViewer = AReportViewer.GetSectionReportView(this.ItemData, AReportViewer.ReportPath);
  13.                 AReportViewer.ReportType = "rpx";
  14.             }
  15.             else
  16.             {
  17.                 //获取页面报表对象
  18.                 AReportViewer.ReportViewer = AReportViewer.GetPageReportView_NForPrint(this.ItemData, AReportViewer.ReportPath);
  19.                 AReportViewer.ReportType = "rdlx";
  20.                 if (AReportViewer.isFormarPrint)
  21.                 {
  22.                     //获取套打时页面报表对象,即没有底图的情况,用于套打按钮使用
  23.                     AReportViewer.ForPrintViewer = AReportViewer.GetPageReportView_ForPrint(this.ItemData, AReportViewer.ReportPath);
  24.                 }
  25.             }
  26.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  27.             ms = AReportViewer.GetExportReportStream("type=" + filter["type"].Kstring(), AReportViewer.ReportViewer, AReportViewer.ReportType);
  28.             DoSaveFile(AReportViewer.ContentType, AReportViewer.FileName, ms);
复制代码
  1. #region 获取excel数据流
  2.         /// <summary>
  3.         /// 获取excel文件数据流
  4.         /// </summary>
  5.         /// <param name="excelType">excel类型,xls或者xlsx</param>
  6.         /// <returns>返回excel文件数据流</returns>
  7.         private MemoryStream GetExcleMemoryStream(string excelType,Boolean excelmethod=false)
  8.         {
  9.             Boolean newexeclmethod = excelmethod;
  10.             if (this.reportType == "rpx")
  11.                 newexeclmethod = false;

  12.             MemoryStream excelMemoryStream = new MemoryStream();

  13.             if (!newexeclmethod)
  14.             {
  15.                 GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
  16.                 XlsExport.UseCellMerging = true;
  17.                 #region excel类型
  18.                 if (excelType == "XLS")
  19.                 {
  20.                     XlsExport.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xls97Plus;
  21.                 }
  22.                 else
  23.                 {
  24.                     XlsExport.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;

  25.                 }
  26.                 #endregion

  27.             #region 导出数据流
  28.             if (this.ReportType == "rpx")
  29.             {
  30.                 SectionReport sectionReport = (SectionReport)this.ReportViewer;
  31.                 sectionReport.Run();
  32.                 XlsExport.Export(sectionReport.Document, excelMemoryStream);
  33.                 //转为文件的方法为pdfExport.Export(sectionReport.Document, 地址路径);
  34.             }
  35.             else
  36.             {
  37.                 PageReport pagereport = (PageReport)this.ReportViewer;
  38.                 //GrapeCity.ActiveReports.Document.PageDocument pageDocument = new GrapeCity.ActiveReports.Document.PageDocument(pagereport);
  39.                 //XlsExport.Export(pageDocument, excelMemoryStream);
  40.                 XlsExport.Export(pagereport.Document, excelMemoryStream);
  41.                 }
  42.                 #endregion
  43.             }
  44.             else
  45.             {
  46.                 PageReport pagereport = (PageReport)this.ReportViewer;                                             
  47.                 //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(pagereport);
  48.                 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = pagereport.Document;
  49.                 GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings XlsExport = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
  50.                 #region excel类型
  51.                 if (excelType == "XLS")
  52.                 {
  53.                     XlsExport.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
  54.                 }
  55.                 else
  56.                 {
  57.                     XlsExport.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx;

  58.                 }
  59.                 #endregion
  60.                 XlsExport.MultiSheet = false;
  61.                 XlsExport.Pagination = false;
  62.                 GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = XlsExport;

  63.                 GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
  64.                 GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
  65.                
  66.                 _reportRuntime.Render(excelRenderingExtension, outputProvider, XlsExport.GetSettings());               
  67.                 outputProvider.GetPrimaryStream().OpenStream().CopyTo(excelMemoryStream);               
  68.             }
  69.             return excelMemoryStream;
  70.         }
复制代码

运行到这一行就抛异常

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

1 个回复

倒序浏览
KearneyKang讲师达人认证 悬赏达人认证
超级版主   /  发表于:2018-1-9 10:23:42
沙发
您的项目是C/s端的项目是吧!你能提供给我一个能反馈您问题的demo 嘛。我给验证下看看是什么问题导致的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部