0
votes

I am trying to export RDLC report to Excel without ReportViewer. I am using Microsoft Visual Studio 2010 and Microsoft SQL Server 2008 R2. Below are my Steps:

  1. Add New Item from Solution Explorer -- Reporting -- Report Wizard -- myReport.rdlc
  2. Follow the wizard steps -- ConnectionString from web.config -- stored procedure as the source to create the report. I made 2 parameters, which is StartDate and EndDate.

    This is my stored procedure:

    CREATE PROCEDURE mySP
        (@StartDate datetime, @EndDate datetime)
    AS
    BEGIN
        SELECT * 
        FROM myTable 
        WHERE TheDate >= @StartDate AND TheDate <= @EndDate
    END
    
  3. Add some fields for report

  4. On the report designer -- Add 2 parameter, StartDate and EndDate.
  5. Right Click on the designer -- Select Report Properties
  6. On Variables section -- Add 2 parameters by Clicking on 'fx' button
  7. Go to xsd file (myApp.xsd) -- on the tableadapter, right click then configure
  8. Configuration of TableAdapter -- No change, I just click Next and Finish

Those are my steps to create the RDLC Report. I took the source from Create RDLC Report in VS 2010 and SQL Server 2008

The question is: How can I export the RDLC Report that I just design without using ReportViewer? I have one button on my webpage, let's say: 'Export to Excel'.

Thank you.

2

2 Answers

1
votes

Here is the code:

        Warning[] warnings;
        string[] streamids;
        string mimeType, encoding, extension;
        string filenameToSave = "test.xlsx";

        LocalReport report = new LocalReport();
        /*
         define here everything about your report: rdlc file, parameters, options etc.
        */ 

        FileStream newFile = new FileStream(filenameToSave, FileMode.Create);

        string renderFormat = (filenameToSave.EndsWith(".xlsx") ? "EXCELOPENXML" : "Excel");
        byte[] bytes = report.Render(renderFormat, null, out mimeType, out encoding, out extension, out streamids, out warnings);
        newFile.Write(bytes, 0, bytes.Length);
        newFile.Close();
0
votes

Use LocalReport.Render(), here's how

note

  • add assembly reference: Microsoft.ReportViewer.WebForms (or install from nuget)
  • then create a a report viewer instance:

var viewer = new Microsoft.Reporting.WebForms.ReportViewer( ... );