Export grid from jobs

Hi,


I wanted to create jobs that send email with attachment (or email HTML body) of Syncfusion grid that i have in a page.

I`m wondering is there any way i can retrieve maybe the exported pdf or filestream to my jobs?
Just asking maybe there's documentation or feature for this in Syncfusion.

7 Replies

VN Vignesh Natarajan Syncfusion Team October 12, 2018 10:14 AM UTC

Hi Trinquier, 
 
 
Thanks for using Syncfusion products. 
 
 
From your query, we understand that you need to send the exported Pdf document as Attachment in an Email. We have achieved your requirement by exporting the Grid to PdfDocument and saving that document file into Stream. Later used that Stream to attach to Email and sent it.  
 
Refer the below code example 
 
[cshtml] 
 
@(Html.EJ().Grid<object>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource) 
                .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => 
                {                    
                    items.AddTool(ToolBarItems.PdfExport); 
                })) 
        .AllowPaging() 
        .AllowFiltering() 
        .AllowGrouping() 
        .AllowReordering() 
        .AllowSorting()   
        
        .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); 
            col.Field("OrderDate").HeaderText("Order Date").Width(80).TextAlign(TextAlign.Right).Priority(4).Format("{0:MM/dd/yyyy}").Add(); 
            col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Priority(3).Width(75).Format("{0:C}").Add(); 
            col.Field("ShipName").HeaderText("Ship Name").Width(110).Add(); 
            col.Field("ShipCity").HeaderText("Ship City").Width(90).Priority(2).Add(); 
            col.Field("ShipCountry").HeaderText("Ship Country").Width(90).Add(); 
        })) 
 
 
[C#] 
public void ExportToPdf(string GridModel) 
        { 
            PdfExport exp = new PdfExport(); 
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            PdfDocument document = exp.Export(obj, DataSource, "Export.pdf", false, false, "flat-saffron", true); 
            MemoryStream memoryStream = new MemoryStream(); 
            document.Save(memoryStream);         
            byte[] bytes = memoryStream.ToArray(); 
            memoryStream.Close(); 
            using (MailMessage mm = new MailMessage("[email protected]", "[email protected]")) 
            { 
                mm.Subject = "GridView Exported Excel"; 
                mm.Body = "GridView Exported Excel Attachment"; 
                //Add Byte array as Attachment. 
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridView.pdf")); 
                mm.IsBodyHtml = true; 
                SmtpClient smtp = new SmtpClient(); 
                smtp.Host = "smtp.gmail.com";  
                smtp.EnableSsl = true; 
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(); 
                credentials.UserName = "[email protected]"; 
                credentials.Password = "<password>"; 
                smtp.UseDefaultCredentials = true; 
                smtp.Credentials = credentials; 
                smtp.Port = 587; 
                smtp.Send(mm); 
            } 
        } 
        
On clicking the Pdf icon in the toolbar will export the Grid and send the mail to corresponding receivers. 
 
 
Refer the below screenshot for the output 
 
 
 
 
For your convenience we have prepared a sample which can be downloaded from below link 
 
 
 
 
Refer our UG documentation for your reference 
 
 
 
 
please get back to us if you have further queries.  
 
 
Regards, 
Vignesh Natarajan 



TV Trinquier Vannick October 17, 2018 01:36 AM UTC

So it means that user have to click in the grid to export and send by email ?


Is there anyway i can do this by calling syncfusion "function" in controller to get the Grid property string and run the export without clicking it in the view ?


VN Vignesh Natarajan Syncfusion Team October 17, 2018 05:37 AM UTC

Hi Trinquier, 
 
Thanks for the update. 
 
From your query, we understand that you need to export the ejGrid and send mail without interaction from the view page. To the export the Grid only the server side code block is enough, but you must have GridModel inform of string in server side. That action we have performed in the toolbarClick. Getting the grid model and submit the form with grid model in form of  string. 
 
Refer the below code example required to export the Grid 
 
public void ExportToPdf(string GridModel) 
        { 
            PdfExport exp = new PdfExport(); 
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            PdfDocument document = exp.Export(obj, DataSource, "Export.pdf", false, false, "flat-saffron", true); 
            MemoryStream memoryStream = new MemoryStream(); 
            document.Save(memoryStream);         
            byte[] bytes = memoryStream.ToArray(); 
            memoryStream.Close(); 
            using (MailMessage mm = new MailMessage("[email protected]", "[email protected]")) 
            { 
                mm.Subject = "GridView Exported Excel"; 
                mm.Body = "GridView Exported Excel Attachment"; 
                //Add Byte array as Attachment. 
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridView.pdf")); 
                mm.IsBodyHtml = true; 
                SmtpClient smtp = new SmtpClient(); 
                smtp.Host = "smtp.gmail.com";  
                smtp.EnableSsl = true; 
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(); 
                credentials.UserName = "[email protected]"; 
                credentials.Password = "<password>"; 
                smtp.UseDefaultCredentials = true; 
                smtp.Credentials = credentials; 
                smtp.Port = 587; 
                smtp.Send(mm); 
            } 
        } 
 
private GridProperties ConvertGridObject(string gridProperty) 
        { 
.                 .            .                .                  .  
            return gridProp; 
        } 
      
 
Note: you need to get the GridModel in server side to export the Grid. if you have save the Grid Model in the server side then you can export the Grid without external action in view page. 
 
If we misunderstood you query, please get back to us with following detail regarding your requirement. 
 
  1. Do you want to export the Grid on external button click?
  2. Or how will you call the function in the server side to export the grid.
  3. Share more details regarding your requirement  
 
Requested detail will be helpful for us to provide the solution to your requirement.  
 
 
Regards, 
Vignesh Natarajan 
  



TV Trinquier Vannick October 18, 2018 09:33 AM UTC

Hi Vignesh,


Apparently i want to retrieve the MemoryStream from ExportToPdf, as i will send as an attachment in email that will be send from Windows service jobs that's running.

I`m trying to find solution to send the Grid PDF that was generated from Syncfusion web, as an email attachment that is being send not by MVC project



VN Vignesh Natarajan Syncfusion Team October 18, 2018 12:47 PM UTC

Hi Trinquier, 
 
Thanks for the update. 
 
From your query, we understand that you need to export the grid without interaction from view page and also without display the grid. We have given to support to render the grid proeprtie in the server side using GridModel. You can use that model to generate the Grid Properties in server side and attach  the stream to mail. 
 
Refer the below code example 
 
 
public ActionResult GridFeatures() 
        {          
 
            List<Column> cols = new List<Column>(); 
            cols.Add(new Column() { Field = "OrderID" }); 
            cols.Add(new Column() { Field = "EmployeeID" }); 
            cols.Add(new Column() { Field = "ShipCity" }); 
            cols.Add(new Column() { Field = "ShipCountry" }); 
            cols.Add(new Column() { Field = "Freight" }); 
            GridProperties prop = new GridProperties(); 
            prop.Columns = cols; 
            PdfExport exp = new PdfExport(); 
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = prop; 
            PdfDocument document = exp.Export(obj, DataSource, "Export.pdf", false, false, "flat-saffron", true); 
            MemoryStream memoryStream = new MemoryStream(); 
            document.Save(memoryStream); 
            byte[] bytes = memoryStream.ToArray(); 
            memoryStream.Close(); 
            using (MailMessage mm = new MailMessage("[email protected]""[email protected]"))  
            {  
                mm.Subject = "GridView Exported Excel";  
                mm.Body = "GridView Exported Excel Attachment";  
                //Add Byte array as Attachment.  
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes),"GridView.pdf"));  
                mm.IsBodyHtml = true;  
                SmtpClient smtp = new SmtpClient();  
                smtp.Host = "smtp.gmail.com"  
                smtp.EnableSsl = true;  
                System.Net.NetworkCredential credentials = newSystem.Net.NetworkCredential();  
                credentials.UserName = "[email protected]";  
                credentials.Password = "<password>";  
                smtp.UseDefaultCredentials = true;  
                smtp.Credentials = credentials;  
                smtp.Port = 587;  
                smtp.Send(mm);  
            } 
            return null; 
        }    
 
For your convenience we have prepared a sample which can be downloaded from below link 
 
 
 
 
Note: we have prepared a sample to export the Grid,  save pdf document to stream and attach it to mail. 
 
 
Refer our help documentation for your reference      
 
 
 
 
If above solution does not resolve your query, please get back to us with more details 
 
 
 
Regards, 
Vignesh Natarajan 



TV Trinquier Vannick October 25, 2018 08:03 AM UTC

Thanks for the guidance Vignace,
I have further questions regarding this :

1. While exporting, what if i want to edit the result ?

I have experience with Excel export, i can use ServerExcelQueryCellInfo, and in QueryCellInfo method i can use IRange.

But what about Pdf ?


2. I tried to export, but the Grid result in pdf becomes 1 column per page :
Page 1:

Page 2:


I have 11 columns need to be displayed in 1 page.
Is there any settings that i need to know ?


VN Vignesh Natarajan Syncfusion Team October 25, 2018 09:24 AM UTC

Hi Trinquier, 
 
Thanks for the update. 
 
Query 1 : While exporting, what if i want to edit the result ?I have experience with Excel export, i can use ServerExcelQueryCellInfo, and in QueryCellInfo method i can use IRange.But what about Pdf ? 
 
From your query, we understand that you need to modify the value of cell while exporting Grid to PDF format. You can use the ServerPdfQueryCellInfo to modify the cell content while exporting to Pdf document.  
 
Please refer the below code example. 
 
 
public void ExportToPdf(string GridModel) 
 
        { 
 
            PdfExport exp = new PdfExport(); 
 
            var DataSource = new NorthwindDataContext().OrdersViews.ToList(); 
 
            GridProperties obj = (GridProperties)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(GridProperties), GridModel); 
 
            obj.ServerPdfQueryCellInfo = QueryCellInfo; 
 
            exp.Export(obj, DataSource, "Export.pdf", false, false, "flat-saffron"); 
 
        } 
 
        public void QueryCellInfo(object currentCell) 
        { 
            Syncfusion.Pdf.Grid.PdfGridCell range = (Syncfusion.Pdf.Grid.PdfGridCell)currentCell; 
 
            range.Style.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial Unicode MS", 8f), true); 
        } 
 
We have prepared a sample for your reference. Please refer the below link for the sample. 
 
 
Query 2 : I tried to export, but the Grid result in pdf becomes 1 column per page  
 
We are unable to reproduce the issue on our side. Please share the following details. 
 
  1. Please share the code example of the Pdfexport method in the controller side.
  2. Share the code example of the Grid.
  3. Share the Essential studio version details.
  4. If possible please try to reproduce the mentioned issue in the attached sample.
 
Above requested details will help us to validate the issue at our end and provide solution accordingly.   
 
Regards, 
Vignesh Natarajan  
 
 


Loader.
Up arrow icon