Articles in this section
Category / Section

How to add title for single exporting

1 min read

 

The following code example shows how to add the title for exporting in excel, word and pdf documents.

  1. Render the Grid control.

JS

<div id="Grid"></div>
<script type="text/javascript">
    $(function () {// Document is ready.        
        $("#Grid").ejGrid({
            dataSource: window.gridData, 
            allowPaging: true,
            toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems. .ExcelExport, ej.Grid.ToolBarItems. WordExport, ej.Grid.ToolBarItems. PdfExport] },
            columns: [
                      { field: "OrderID", headerText: "Order ID", isPrimaryKey: true, width: 100},
                      { field: "CustomerID", headerText: "Customer ID", width: 100 },
                      { field: "Freight", headerText: "Freight", width: 100, format: "{0:C}" },                      
                      { field: "ShipCountry", headerText: "Ship Country", width: 100,  }                      
            ],
  toolbarClick: function (e) {
                   this.exportGrid = this["export"];
                   if (e.itemName == "Excel Export") {
                       this.exportGrid('api/Grid/ExportToExcel ')
                       e.cancel = true;
                   }
                   else if (e.itemName == "Word Export") {
                this.exportGrid('api/Grid/ExportToWord ')
                       e.cancel = true;
                   }
                   else if (e.itemName == "PDF Export") {
                       this.exportGrid('api/Grid/ExportToPdf ')
                       e.cancel = true;
                   }
               },
    });
    });
</script>

 

 

MVC

[In View]
@(Html.EJ().Grid<object>("Grid")            
            .Datasource((IEnumerable<object>)ViewBag.data))
            .AllowPaging()            
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems. .ExcelExport);
                    items.AddTool(ToolBarItems. WordExport);
                    items.AddTool(ToolBarItems. PdfExport);
                });
            })
            .Columns(col =>
            {                        col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).Width(100).Add();
                col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add();                
                col.Field("Freight").Width(100).Format("{0:C}").Width(100).Add();                
                col.Field("ShipCountry").HeaderText("Ship Country").Width(100).Add();                
            })
)
[Controller]
namespace EJGrid.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var DataSource = OrderRepository.GetAllRecords();
            ViewBag.data = DataSource;
            return View();
        }        
    }
}

 

 

 

 

ASP.NET

 

[aspx]
<ej:Grid ID="Grid" runat="server" AllowPaging="True"       OnServerWordExporting="Grid_ServerWordExporting" OnServerPdfExporting="Grid_ServerPdfExporting" OnServerExcelExporting="Grid_ServerExcelExporting">  
                        <ToolbarSettings ShowToolbar="True" ToolbarItems="excelexport,wordexport,pdfexport"></ToolbarSettings>           
            <Columns>
               <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True"  Width="100" />                
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100"/>
                <ej:Column Field="Freight" HeaderText="Freight" Format="{0:C}" Width="100"/> 
                <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="100"/>                
            </Columns>
</ej:Grid>  
[CS]
public partial class _Default : Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
            this.Grid.DataSource = new NorthwindDataContext().Orders;
            this.Grid.DataBind();
    }
}

 

2. In excel exporting, you have to add the title for the exported file by using the ActiveSheet” property of the “IWorkBook” class and then set the Range for the title using “Range” property and then added the title using “Text” property of the Range.

MVC

public void ExportToExcel(string GridModel)
        {
            ExcelExport exp = new ExcelExport();
            ExcelEngine excel = new ExcelEngine();
            IApplication application = excel.Excel;
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
           IWorkbook workbook = application.Workbooks.Create(2);
            //Setting multiExport as true and export the document and assigned to workbook
            workbook = exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2013,false, false, "default-theme",true);
            //Inserted new row for adding title
            workbook.ActiveSheet.InsertRow(1);
            //Merging the sheet from Range A1 to D1 for adding title space
            workbook.ActiveSheet.Range["A1:D1"].Merge();
            //Adding the title using Text property
            workbook.ActiveSheet.Range["A1"].Text = "Excel Grid";
            //Downloading the document
            workbook.SaveAs("Export.xls", HttpContext.ApplicationInstance.Response,ExcelDownloadType.PromptDialog,ExcelHttpContentType.Excel2013);
                  }
 

 

ASP.NET

 Protected void  FlatGrid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
        {
             ExcelExport exp = new ExcelExport();
             ExcelEngine excel = new ExcelEngine();
            IApplication application = excel.Excel;
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
           IWorkbook workbook = application.Workbooks.Create(2);
            //Setting multiExport as true and export the document and assigned to workbook
            workbook = exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2013,false, false, "default-theme",true);
            //Inserted new row for adding title
            workbook.ActiveSheet.InsertRow(1);
            //Merging the sheet from Range A1 to D1 for adding title space
            workbook.ActiveSheet.Range["A1:D1"].Merge();
            //Adding the title using Text property
            
            workbook.ActiveSheet.Range["A1"].Text = "Excel Grid";
            //Downloading the document
            workbook.SaveAs("Export.xls", HttpContext.ApplicationInstance.Response,ExcelDownloadType.PromptDialog,ExcelHttpContentType.Excel2013);
          
        }
 

 

3. For word document you have to add the header using “Header” property of the “HeadersFooters” and append the text to the header using “AppendText” property.

MVC

public void ExportToWord(string GridModel)
        {
            WordExport exp = new WordExport();
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
           IWordDocument document = exp.Export(obj, DataSource, "Export.docx", false, false, "flat-lime", true);
            
            WParagraph para = document.Sections[0].HeadersFooters.Header.AddParagraph() as WParagraph;
//Styling the header format
            para.ApplyStyle(BuiltinStyle.Heading3);
//Append text to the header
            para.AppendText("Grid Word");
//Downloading the document
document.Save("Export.docx",Syncfusion.DocIO.FormatType.Docx,HttpContext.ApplicationInstance.Response,Syncfusion.DocIO.HttpContentDisposition.Attachment);        }
 

 

 

 

ASP.NET

Protected void  Grid_ServerWordExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
{
            WordExport exp = new WordExport();
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
           IWordDocument document = exp.Export(obj, DataSource, "Export.docx", false, false, "flat-lime", true);
            
            WParagraph para = document.Sections[0].HeadersFooters.Header.AddParagraph() as WParagraph;
//Styling the header format
            para.ApplyStyle(BuiltinStyle.Heading3);
//Append text to the header
            para.AppendText("Grid Word");
//Downloading the document
document.Save("Export.docx",Syncfusion.DocIO.FormatType.Docx,HttpContext.ApplicationInstance.Response,Syncfusion.DocIO.HttpContentDisposition.Attachment);        }
 

 

4. For pdf document we have added header by adding the template using “PdfPageTemplateElement” class and added the title for the template using “DrawString” method.

MVC

public void ExportToPdf(string GridModel)
        {
            PdfExport exp = new PdfExport();
           var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
               PdfDocument pdfdocument = exp.Export(obj,     DataSource, "Export.pdf", false,false, "flat-lime",true,true);
            RectangleF rect = new RectangleF(0, 0, pdfdocument.PageSettings.Width,50);
  //Create a page template
           PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
           Font f = new Font("Helvetica", 8, System.Drawing.FontStyle.Regular);
           PdfFont font = new PdfTrueTypeFont(f, true);
          PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
           PdfPen pen = new PdfPen(Color.DarkBlue, 3f);
                f = new Font("Helvetica", 10, System.Drawing.FontStyle.Bold);
                font = new PdfTrueTypeFont(f, true);
               PdfStringFormat format = new PdfStringFormat();
               format.Alignment = PdfTextAlignment.Center;
              format.LineAlignment = PdfVerticalAlignment.Top;
               header.Graphics.DrawString("GridPdf", font, brush, rect, format); //Add custom text to the page template
            pdfdocument.Template.Top = header; //Append custom template to the document
pdfdocument.Save("Export.pdf",HttpContext.ApplicationInstance.Response,HttpReadType.Save);
 
        }        
 

ASP.NET

Protected void  Grid_ServerPdfExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e){
PdfExport exp = new PdfExport();
         var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
               PdfDocument pdfdocument = exp.Export(obj,     DataSource, "Export.pdf", false,false, "flat-lime",true,true);
            RectangleF rect = new RectangleF(0, 0, pdfdocument.PageSettings.Width,50);
  //Create a page template
           PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
           Font f = new Font("Helvetica", 8, System.Drawing.FontStyle.Regular);
           PdfFont font = new PdfTrueTypeFont(f, true);
          PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
           PdfPen pen = new PdfPen(Color.DarkBlue, 3f);
                f = new Font("Helvetica", 10, System.Drawing.FontStyle.Bold);
                font = new PdfTrueTypeFont(f, true);
               PdfStringFormat format = new PdfStringFormat();
               format.Alignment = PdfTextAlignment.Center;
              format.LineAlignment = PdfVerticalAlignment.Top;
               header.Graphics.DrawString("GridPdf", font, brush, rect, format); //Add custom text to the page template
            pdfdocument.Template.Top = header; //Append custom template to the document
pdfdocument.Save("Export.pdf",HttpContext.ApplicationInstance.Response,HttpReadType.Save);
        }
 
 
 

 

The following output is displayed as a result of the above code example.

Figure: how to add the title for exporting

 

C:\Users\farveen.thameeztheen\Desktop\Capture1.PNG

Figure: Word Exporting with Title

 

 

C:\Users\farveen.thameeztheen\Desktop\Capture3.PNG

Figure: Pdf Exporting with Title

 

Figure: Excel Exporting with Title

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied