コード例 #1
0
        private void btnTUtilityDownload_Click(object sender, EventArgs e)
        {
            try
            {
                Warning[] warnings;
                string[]  streamids;
                string    mimeType;
                string    encoding;

                string   filenameExtension;
                int      FirmId    = Convert.ToInt32(comboUFirm.SelectedValue);
                int      AccountId = Convert.ToInt32(comboUAccount.SelectedValue);
                DateTime fromDate  = dtFromDate.Value;
                DateTime toDate    = dtToDate.Value;
                accountTransacionService = new AccountTransactionService();
                List <Service.Model.AccountTransactionReportModel> dataSource = accountTransacionService.GetTransactionReportData(FirmId, AccountId, toDate, fromDate);
                var accountData = accountTransacionService.GetOpeningBalance(FirmId, AccountId);
                if (dataSource.Count > 0)
                {
                    using (SaveFileDialog dlgSaveAs = new SaveFileDialog())
                    {
                        dlgSaveAs.InitialDirectory = @"C:\";
                        dlgSaveAs.Title            = "Save PDF Files";
                        dlgSaveAs.CheckFileExists  = false;
                        dlgSaveAs.CheckPathExists  = false;
                        dlgSaveAs.DefaultExt       = "pdf";
                        dlgSaveAs.Filter           = "Pdf files (*.pdf)|*.pdf";
                        dlgSaveAs.FilterIndex      = 2;
                        dlgSaveAs.RestoreDirectory = true;
                        if (dlgSaveAs.ShowDialog() == DialogResult.OK)
                        {
                            using (FileStream stream = new FileStream(dlgSaveAs.FileName, FileMode.OpenOrCreate))
                            {
                                using (ReportViewer ReportViewer1 = new ReportViewer())
                                {
                                    ReportViewer1.ProcessingMode = ProcessingMode.Local;
                                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("AccountTransactionDataSet", dataSource));
                                    ReportViewer1.LocalReport.ReportPath = Path.GetFullPath(Path.Combine(ConfigurationManager.AppSettings["BaseDirectoryPath"], "Reports\\AccountTransactions.rdlc"));
                                    ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("FromDate", fromDate.ToString()));
                                    ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("ToDate", toDate.ToString()));
                                    ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("OpeningDebit", accountData.OpeningDebit.ToString()));
                                    ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("OpeningCredit", accountData.OpeningCredit.ToString()));
                                    byte[] bytes = ReportViewer1.LocalReport.Render(
                                        "PDF", null, out mimeType, out encoding, out filenameExtension,
                                        out streamids, out warnings);

                                    stream.Write(bytes, 0, bytes.Length);
                                }
                            }

                            System.Diagnostics.Process.Start(dlgSaveAs.FileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Comman.Product_Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }