reportviewer導出格式控制


現在集成在vs(我的是VS2012)中的報表工具reportviewer,在導出office文檔是,默認導出的格式是最新的xlsx或者docx格式,但是客戶很多用的還是古老的office2003,這就必須的控制輸出2003格式的文檔。

通過下面的viewer.ServerReport.Render控制生成文件,然后將文件發送個客戶端的方式,可以解決,但是總歸需要去自己寫太多的代碼,並且會影響畫面的原有布局方式。

public bool Export(ReportViewer viewer, string exportType)
  {
      Warning[] warnings = null;
      string[] streamIds = null;
      string mimeType = string.Empty;
      string encoding = string.Empty;
      string extension = string.Empty;
      string deviceInfo = string.Empty;
      string filetype = string.Empty;
      string reportsTitle = GetReportTitle(viewer); // just gets the Report title... make your own method
 
      //ReportViewer needs a specific type (not always the same as the extension)
      if (exportType == "XLS")
      {
          filetype = "Excel";
      }
      else
      {
          filetype = exportType;
      }
 
      byte[] bytes = viewer.ServerReport.Render(filetype, null, // deviceinfo not needed for csv
          out mimeType, out encoding, out extension, out streamIds, out warnings);
 
      System.Web.HttpContext.Current.Response.Buffer = true;
      System.Web.HttpContext.Current.Response.Clear();
 
      System.Web.HttpContext.Current.Response.ContentType = mimeType;
      System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + reportsTitle + "." + exportType);
 
      System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
 
      System.Web.HttpContext.Current.Response.Flush();
      System.Web.HttpContext.Current.Response.End();
 
      return true;
  }

 如果不想去通過這個方法來實現,那么可以通過控制張票的導出格式來控制。這樣就可以簡單的保持原有的reportviewer風格,自由的控制處理的格式,也只需要少量的代碼來控制。

代碼如下:

 protected void rptViewer_PreRender(object sender, EventArgs e)
        {
            RenderingExtension[] resut = rptViewer.LocalReport.ListRenderingExtensions();
            foreach (RenderingExtension item in rptViewer.LocalReport.ListRenderingExtensions())
            {
// item 具有Visable屬性,但是這個屬性是只讀屬性,不能修改,所以通過反射進行了修改
if (item.Name.ToUpper() == "EXCEL") { // 顯示excel2003格式導出按鈕 FieldInfo fi = item.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(item, true); } if (item.Name.ToUpper() == "WORD") { // 顯示word2003導出按鈕 FieldInfo fi = item.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(item, true); } if (item.Name.ToUpper() == "EXCELOPENXML") { // 不顯示excel2003以上版本的格式 FieldInfo fi = item.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(item, false); } if (item.Name.ToUpper() == "WORDOPENXML") { // 不顯示word以上版本的格式 FieldInfo fi = item.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(item, false); } } }

PS:通過這種方式能夠簡單的控制本身支持的處理格式,如果只要處理pdf,可以把其他格式的m_isVisible全部設置成false,只把pdf的m_isVisible設置成true。

rdlc報表雖然不是很靈活,但是要做簡單表報還是能夠勝任的,而且操作簡單,與vs很好的整和在了一起,用起來還是蠻方便的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM