Thursday, August 06, 2009
If you are using client side reports - rdlc - in a WinForms application, you may wish to change the default location for the File "Save As" dialog that pops up when you press the Export button.
You'd hope that there was a property that you can set, but the solution is somewhat more involved.
Basically, you need to handle the ReportViewer's ReportExport event, and in the event handler, you need to cancel the event and roll your own File Save As code.

In the client side reportviewer, only pdf and xls files are handled.

Here is some sample code for the event handler:

        void reportViewer_ReportExport(object sender, ReportExportEventArgs e)
        {
            e.Cancel = true; // cancel the built in export and roll your own

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = Helper.GetFullFileName(FrmMain.ReportsFolder, projectLocation);
            dlg.RestoreDirectory = true;
            dlg.FilterIndex = 1;

            if (e.Extension.Name == "PDF")
                dlg.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
            else if (e.Extension.Name == "Excel")
                dlg.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
            else
                throw new NotImplementedException();

            DialogResult res = dlg.ShowDialog();
            if (res != DialogResult.OK)
                return;
            string reportFilename = dlg.FileName;

            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;
            byte[] bytes = reportViewer.LocalReport.Render(
               e.Extension.Name, null, out mimeType, out encoding, out extension,
               out streamids, out warnings);
            FileStream fs = new FileStream(reportFilename, FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

        }



Thursday, August 06, 2009 12:05:46 AM (GMT Standard Time, UTC+00:00) | Comments [2] | C##
Thursday, November 26, 2009 3:44:33 PM (GMT Standard Time, UTC+00:00)
Thanks - saved me a major headache
Ali
Thursday, December 17, 2009 10:39:04 PM (GMT Standard Time, UTC+00:00)
This works well, but the menu item when of the report when you select excel or pdf stays open/visible when the dialog comes up.
name
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: