Here this article I am explaining how to generate PDF reports directly on specific shared folder using report viewer control of asp.net.
Step 1 : Create Dataset in App_Code folder
Step 2 : Create rdlc report and use that Dataset in RDLC report
Step 3 : Create ASPX page use Report Viewer control and Object DataSource for Bind rdlc report with Report Viewer control
Step 4 : paste below code for Generate PDF report using report viewer
private void GeneratePDF() { //ReportViewer1.Visible is set to false in design mode //ReportViewer1.Visible = true; string _connConnString = ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(_connConnString); DataSet thisDataSet = new DataSet(); SqlCommand cmd = new SqlCommand("SELECT * FROM [TestDB].[dbo].ProductInfo", conn); cmd.CommandType = CommandType.Text; SqlDataAdapter adpt = new SqlDataAdapter(cmd); adpt.Fill(thisDataSet); LocalReport report = new LocalReport(); string reportName = "CompletionCertificate"; string deviceInfo = "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>" + " <PageWidth>8.5in</PageWidth>" + " <PageHeight>11in</PageHeight>" + " <MarginTop>0.25in</MarginTop>" + " <MarginLeft>0.25in</MarginLeft>" + " <MarginRight>0.25in</MarginRight>" + " <MarginBottom>0.25in</MarginBottom>" + "</DeviceInfo>"; Warning[] warnings; string[] streamids; string mimeType; string encoding; string extension; report.EnableExternalImages = true; report.ReportPath = Server.MapPath("Report1.rdlc"); ReportDataSource datasource = new ReportDataSource("DataSet1_ProductInfo", thisDataSet.Tables[0]); Random randNo = new Random(); //Generate PDF string pdfName = "Test_" + randNo.Next(50000).ToString() + ".pdf"; report.DataSources.Add(datasource); byte[] bytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings); FileStream fs = new FileStream(@"" + "c:\\" + "" + pdfName, FileMode.Create); fs.Write(bytes, 0, bytes.Length); fs.Close(); //Generate Excel string excelName = "Test_" + randNo.Next(50000).ToString() + ".xls"; report.DataSources.Add(datasource); byte[] bytesExcel = report.Render("Excel", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings); FileStream fsExcel = new FileStream(@"" + "c:\\" + "" + excelName, FileMode.Create); fsExcel.Write(bytesExcel, 0, bytesExcel.Length); fsExcel.Close(); }
Step 5 : PDF Report will generate Automatically on shared folder
Step 6 : That's all
No comments:
Post a Comment