2015-02-08 120 views
0

在rdlc報告中,我想導出rdlc報告爲excel格式2007,即.xlsx格式。我寫了下面的代碼來獲得這樣的輸出。但系統產生.xls格式。在這一點上請幫助我。如何在asp.net中將RDLC報告導出爲.xlsx格式?

private void PopulateReport(List<OrderDetail> objectList, string datasetName, string reportPath, out string mimeType, out byte[] renderedBytes, decimal fileWidth, decimal fileHeight) 
     { 
       LocalReport localReport = new LocalReport(); 
      localReport.ReportPath = Server.MapPath(reportPath); 
       ReportDataSource reportDataSource = new ReportDataSource(datasetName, objectList); 
      localReport.DataSources.Add(reportDataSource); 

      //localReport.SetParameters(new ReportParameter("pm", "", false));  
      string reportType = "excel"; 
      mimeType = string.Empty; 
      string encoding = string.Empty; 
      string fileNameExtension = string.Empty; 
      //The DeviceInfo settings should be changed based on the reportType 
      string deviceInfo = 

      "<DeviceInfo>" + 

      " <OutputFormat>PDF</OutputFormat>" + 

      " <PageWidth>" + fileWidth + "in</PageWidth>" + 

      " <PageHeight>" + fileHeight + "in</PageHeight>" + 

      " <MarginTop>0.5in</MarginTop>" + 

      " <MarginLeft>1in</MarginLeft>" + 

      " <MarginRight>1in</MarginRight>" + 

      " <MarginBottom>0.5in</MarginBottom>" + 
      "</DeviceInfo>"; 
      Warning[] warnings; 
      string[] streams; 
      //Render the report 
      renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); 
      //Clear the response stream and write the bytes to the outputstream 
      //Set content-disposition to "attachment" so that user is prompted to take an action 
      //on the file (open or save) 
      Response.Clear(); 
      Response.ContentType = mimeType; 
      Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension); 
      Response.BinaryWrite(renderedBytes); 
      Response.End(); 
     } 

回答

1

你需要指定一個不同的MIME類型,例如,你可以在web配置設置此:

<configuration> 
    <system.webServer> 
     <staticContent> 
      <mimeMap fileExtension=".xslx" mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> 
     </staticContent> 
    </system.webServer> 
</configuration> 

或者只是指定此MIME類型在你的代碼。

+0

我已添加,但它沒有工作 – 2015-02-08 17:37:06

3

我知道它已經有一段時間了,但這裏是答案。更新您的以下代碼

string reportType = "excel"; 

string reportType = "EXCELOPENXML"; 

這可能幫助別人。

+0

這取決於程序集的版本。 您可以使用LocalReport.ListRenderingExtensions來打印哪些支持的格式。 https://social.msdn.microsoft.com/Forums/zh-CN/4515d853-216f-4896-a170-f431a004e786/render-to-xlsx -format-for-localreport-using-microsoft-reportviewer-dll-version-110336616?forum = vsreportcontrols – Lanfeust 2016-01-25 10:12:01

+0

EXCELOPENXML爲我工作。 – 2017-01-11 16:47:02