Link to home
Start Free TrialLog in
Avatar of edfreels
edfreelsFlag for United States of America

asked on

.rdlc report conversion to excel

I have a report I built using C#; the report generates the data fine. The export option on the reports toolbar has Excel or PDF. I can manually select Excel and it will save my document accordingly.
My question is, is there a way to programmatically  either, export the data as an excel document, or save the report, then convert it to an xls?
I know this is lame, but the only code I have so far for this is:

Form1 F = new Form1();
            //
            F.Show();
            //
** this where I want to try and save the result, and export it as an Excel doc.

Thanks ahead of time experts.
Avatar of MogalManic
MogalManic
Flag of United States of America image

You could use something like this library:
  http://www.carlosag.net/Tools/ExcelXmlWriter/

It uses Excel 2003's XML format to generate a spreadsheet.  Carlos even has a code generation tool that takes an existing spreadsheet and generates the code that would produce the spreadsheet.  All you need to do is replace the static content with loops that retrieves the content from your data.
Avatar of edfreels

ASKER

I am not trying to take an existing spread sheet and format it with this tool. I have a report already written, I simply want to automate the results to Excel where a macro will run against it. This example will not suffice in that I am not adding someone elses .dll to my code or network. I need to see some code that will allow me to re-create the process.  I did check the site, but there are no examples of taking an .rdlc and converting it to an Excel format that I had seen. It is part of the 'Export' function on the toolbar of the report, so I know it is do-able. I just don't see how the example from the link listed will do it.
Thank you for the response though MogulManic.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had to make some adjustments for my environment, but it worked nonetheless. This was my modified solution for Excel.

private void exportToExcel(string fileFolder)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(fileFolder);
            //
            if (fi.Exists)
                fi.Delete();
            Warning[] warnings;
            string[] streamids;
            string mimeType, encoding, filenameExtension;
            byte[] bytes = reportViewer1.LocalReport.Render("Excel",null,out mimeType, out encoding,out filenameExtension,out streamids, out warnings);
            System.IO.FileStream fs = System.IO.File.Create(fileFolder);
            fs.Write(bytes,0,bytes.Length);
            fs.Close();
        }
Avatar of dj_alik
dj_alik

see more clear code
Code to render the report in PDF format using C#
http://forums.asp.net/p/1549431/3795020.aspx
example
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = reportViewer.LocalReport.Render(    "PDF", null, out mimeType, out encoding, out filenameExtension,    out streamids, out warnings);
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{ 
   fs.Write(bytes, 0, bytes.Length);
}

Open in new window