ASP.net MVC RDLC Generate Report in PDF and Excel Format

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everybody welcome to digital tech join in this video i'm going to demonstrate to you how to download a report in pdf and excel format and also send that report as an attachment on email now before we proceed please note that i i had created a similar video uh which demonstrated how to generate a rdlc report now in that video i had shown you and demonstrated you how i've created an rdlc report design an rdlc report connect to connect it to an mssp database to fetch the record and display the report on the front end so i'll just run this report just to recap and then i am going to extend the same project to display the data in a view and provide the option to download the data in pdf in excel format so this was a report now i'm going to extend the existing project with this functionality second thing is that you need to add certain packages and which i'll show to you go to your package you get packages there is a package called closed xml i will install this package in this project with version 0.9 3.0 please install this package now let's proceed and include our data in the view so we use a default view which is there available in index i have already had the html code with me so i'm just going to use the html code over here i'll explain this code but we'll go to the controller and add the relevant methods so in the index method i am going to first reference my model and then i designer code so we we invoke the dial class and then we call the method get our customers so this will return a list object and then we will return this object to the view in the index what i have done is it has reference the model class and then i defined and give a table and i'm looping in through the model class which which i'm passing it in the view and displaying the record so let's run this okay so now we can see data is loaded in a view now we are going to add three buttons to download this data in pdf excel and also email so what i'm going to do is that i'm going to use this html please note that i am using the home controller i am using index to fetch the data now i am going to add three methods now if you notice there are three buttons over here of type submit our form action defines which method to invoke so there is download pdf download excel and send email okay so this is very important if you have a submit button and if you have multiple actions then you use form action to indicate which the view which controller needs to which controller method needs to invoke so here what i'm going to do is that first write a simple method to download this data data displayed in the pdf format remember this this method is going to indirectly call this list of you rdlc report load that report and then download it in the pdf format okay so so this is very important to understand i'll show the code so we will write a method probably file result and download pdf report okay now we define the file name this is a file name i'm giving a dynamic name to it this is a pdf format so this becomes my file name nice thing is that i'm i need to fetch the data so i use the same code to fetch the data in the in the variable in the list variable now once this is done i'm going to use this data in general report so what i'm going to do is so i need to reference the local report local report i have used this class microsoft reporting waveforms this will invoke this report and then report data what we are doing over here is we are passing in the data source this list object which has data over here and this data binding is already set over here so if you go to your report view and if you go to our data source already a data set is bind which is which is the list object like which has all the variables so this is we are doing it runtime so what we are doing is that we are declaring a local variable local report we are then passing that local report the data source which i have fetched over here and now we need to load this report so we say report path equals server.map map so we have passing this so we are calling this report this this report over here fine this is clear so we are declaring a local report we are passing in the dynamic data which is we have pre-populated already we are passing that variable and then we are telling the system the code that the report is available over here so now it has everything all the required parameters so now data is ready now whatever we are going to do is that we can we can load this report we load this report and now we need to convert that into pdf and download that in the browser for that we declare a byte variable and we call local report dot render in which format in pdf that's it so our this is rendered and then we return the file which will download the bytes on the browser we pass in the main type and the report file which we declare adf this is a few lines of code required to download the report now let us debug it so that i can explain to you runtime so we run the code data gets loaded we have three buttons over here and we click on pdf download okay so now we first define a dynamic file name then we load the data which is a list view then we load the rdlc report we provide the required data to our list report then we render that into a byte array yes it's done and then we download that into the browser yes you can see it's getting downloaded and here you have the report in a pdf format so now let's go to a second functionality of downloading the same report in excel format here we are using that package closed xml so let's write the code to download the report in excel format data set code will take it from here we need to we need the data so this will remain the same now we create a data table we are converting this list object into a data set we define that table structure runtime to the same for other columns mobile and email we have the data table defined we need to loop in and fill in populate the data label so for that we use a for each loop define a data row and we say dt dot new row and then we bind the record set mobile and email so we have done it and we add that row rows dot add and we add this row so we have actually created a dataset and then we have pre-populated record the last thing we need to do is that define the file name my customer report again v binder create time xml code xls excel workbook so let me say x so i guess we need to add the reference of closed xml using closed excellent okay book yes not able to find okay so i got it this type of error yes okay now dot worksheet add and we pass the data table having the records and then we use the memory string we need to add the reference i o reference save as steam and then we return the file object stream dot to array and then we type pass in the main type now i found this main type on the internet i'll show you this is the main type and then we pass in the file name and extension so if you need to find the main tab if you if you just type in or search this string you might get this is a url that i have founded where it mentioned all the different apps on you know extension and main types i picked up this from this side so i mentioned that so this our code is ready let's debug lc so this should download the report in excel format so we click on x amount okay so this is the data set we fill in the data set let's see what it is so this fetches all the record we create a data table runtime we fill in loop in all the records in the data table then we give it a file name we use wl workbook object of closed xml we pass in the data table we create a memory stream and we then return it to the browser let's see it's gets downloaded so let's open this sheet now so you can see it is downloaded in excel format so just few lines of code and we are able to achieve uh the generation of pdf and you know generating a pdf and excel report now the last part is that i will show you how do you write a code to send this report dynamically on email so i'll just show the code i have the code ready with me i'll just use the code over here i explain the code to you so while sending a report what i'm going to do is i just so first thing is that everything remains the same now if you look at the code over here in the pdf download we are rendering the byte so we load that report and we render the same in the pi day okay now once it is in the byte same way we get the data and we store the data in the right array now once you have the data in the binary you call the memory stream and you pass the byte array now after you pass this is the code for sending email so you can see this is a standard.net code the only difference is that here we are passing the memory stream with as the byte array and the file name as an attachment so this will go as an attachment to the email you specify over here so to email and you know this is a typical.net code we can find it on the net we have an attachment from two and then we define the smtp client and when we send the mail so this is the same code we have to use for sending this report dynamically on email so you pre-populate the record so for example let me just also show this to you i'll explain once again so in this i'll be sending a mail i'll debug it so that you know you understand it clearly now okay so we click on email okay so this is the same code we we define a file name then we fetch the data in the list object then we are loading the local report which is designed over here our dlc report we provide the data set now we render that into byte array which we did it over here now after that we pass it to the memory stream now here is a typical code for sending mail in dotnet where we are using the attachment property and we are passing in the memory stream and the file name so this gets attached to the mail object and then we send to we add an attachment from to subject email and the body and then we define the smp smtp clients with the host port and we send them so this is how you send a mail using the attachment and the report so all these three functionality have demonstrated it i hope you liked the video as i mentioned please do go ahead and watch the previous video which i which are based in the description link and i hope that the video was helpful and you were able to learn something new thanks for visiting and please do subscribe and like the video thanks a lot
Info
Channel: Digital TECHJOINT
Views: 850
Rating: undefined out of 5
Keywords: asp.net, asp.net mvc, asp.net rdlc, rdlc report, dotnet, dotnet rdlc, asp.net report pdf, asp.net report excel, closedxml, vs 2015, asp.net email, asp.net mvc email, Send email using asp.net
Id: NTgt9GwlHxU
Channel Id: undefined
Length: 24min 50sec (1490 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.