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##
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: