如何在asp.net中将RDLC报告导出为.xlsx格式?

时间:2023-01-15 13:18:17

In rdlc reporting, I want to export rdlc report as excel format 2007 i.e .xlsx format. I have written the following code to get such output. But system produce .xls format. Please help me in this point.

在rdlc报告中,我想将rdlc报告导出为ex​​cel格式2007,即.xlsx格式。我编写了以下代码来获得这样的输出。但系统产生.xls格式。请帮助我。

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();
        }

2 个解决方案

#1


4  

I know its been some time, but here is the answer. Update your following line of code

我知道已经有一段时间了,但答案就是这样。更新以下代码行

string reportType = "excel";

to

string reportType = "EXCELOPENXML";

This might help others.

这可能有助于其他人。

#2


1  

You need to specify a different mimetype, for example you could set this in the web config:

您需要指定不同的mimetype,例如您可以在Web配置中设置:

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

Or just specify this mimetype in your code as well.

或者只是在代码中指定这个mimetype。

#1


4  

I know its been some time, but here is the answer. Update your following line of code

我知道已经有一段时间了,但答案就是这样。更新以下代码行

string reportType = "excel";

to

string reportType = "EXCELOPENXML";

This might help others.

这可能有助于其他人。

#2


1  

You need to specify a different mimetype, for example you could set this in the web config:

您需要指定不同的mimetype,例如您可以在Web配置中设置:

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

Or just specify this mimetype in your code as well.

或者只是在代码中指定这个mimetype。