2015-02-08 2 views
0

В отчетах rdlc я хочу экспортировать отчет rdlc как формат Excel формата i.e .xlsx. Для получения такого вывода я написал следующий код. Но система производит формат .xls. Пожалуйста, помогите мне в этом вопросе.Как экспортировать отчет RDLC в формате .xlsx в asp.net?

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

Вы должны указать другой MimeType, например, вы можете установить это в веб-конфигурации:

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

Или просто указать это MimeType в вашем коде, а также.

+0

Я добавил, но это не сработало –

3

Я знаю, что это было какое-то время, но вот ответ. Обновите свою следующую строку кода

string reportType = "excel"; 

в

string reportType = "EXCELOPENXML"; 

Это может помочь другим.

+0

Это зависит от версии сборки. Вы можете распечатать, которые являются поддерживаемыми форматами, с помощью «LocalReport.ListRenderingExtensions» https://social.msdn.microsoft.com/Forums/en-US/4515d853-216f-4896-a170-f431a004e786/render-to-xlsx -format-for-localreport-using-microsoft-reportviewer-dll-version-110336616? forum = vsreportcontrols – Lanfeust

+0

EXCELOPENXML работал для меня. –

Смежные вопросы