radgrid export to exce,word,pdf

16 posts, 0 answers
  1. yogesh
    yogesh avatar
    16 posts
    Member since:
    Feb 2011

    Posted 28 Feb 2011 Link to this post

    Hi..
         I am Yogesh. I am new to asp.net with c#. I am working with asp.net ajax radgrid control. i want to export grid data to excel, word,pdf.
         Could anyone help me out by posting code for that plz...its urgent.

    regards,
    yogesh
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 28 Feb 2011 Link to this post

    hi yogesh,

    http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/exporting/defaultcs.aspx

    let me know if u have any other issue.

    thanx,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. yogesh
    yogesh avatar
    16 posts
    Member since:
    Feb 2011

    Posted 28 Feb 2011 Link to this post

    Hi jayesh,
                   Thanks for your code..Its working for me but  the values in grid view are scattered and are not in a format. Plz help me out.
                   .Awaiting your reply...
  5. Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 28 Feb 2011 Link to this post

    Hello Yogesh,

    Could you please provide some more information about the problem? This way we will be able to provide an appropriate answer.

    Best regards,
    Daniel
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  6. yogesh
    yogesh avatar
    16 posts
    Member since:
    Feb 2011

    Posted 01 Mar 2011 Link to this post

    Hi,,
         1.) after exporting data to word and excel, My column header and column values are not  in a proper order. header is to the left side and values are to the right side of column header.
    2.) other problem is grid layout is also getting displayed along with data. i want only data  to be exported.
    3.) could you plz tell me how to display page title after exporting to word,pdf,excel,csv.    

    plz help me out....
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 01 Mar 2011 Link to this post

    Hello Yogesh,

    If you want to change the alignment of columns when exporting, try the following code snippet.
    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExportToPdfCommandName)
            {
                GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
                foreach (TableCell cell in headerItem.Cells)
                {
                    cell.Style["text-align"] = "right";//change style accordingly
                }
                foreach (GridDataItem item in RadGrid1.Items)
                {
                    foreach (TableCell cell in item.Cells)
                    {
                        cell.Style["text-align"] = "right";//change style accordingly
                    }
                }
            }
        }

    And for your second requirement, you can set ExportOnlyData="true".

    ASPX:
    <ExportSettings ExportOnlyData="true">
    </ExportSettings>

    I guess your third requirement is to show heading for the exported file. If so please refer the following forum.
    RadGrid Export Header and Footer

    Thanks,
    Princy.
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 01 Mar 2011 Link to this post

    Hi,
    for page Title

    protected void rgvAssignments_GridExporting(object source, GridExportingArgs e)
        {
            //string customHTML = "<div width=\"100%\" style=\"text-align:center;font-size:12px;font-family:Verdana;\">Your page title here </div>";
            
     
            e.ExportOutput = e.ExportOutput.Replace("<body>", String.Format("<body>{0}", customHTML));
        }

    thanks,
    Jayesh Goyani
  9. yogesh
    yogesh avatar
    16 posts
    Member since:
    Feb 2011

    Posted 01 Mar 2011 Link to this post

    hi,
       i am able to export data to word and excel but not in a formatted way. i have used the code to format which is sent by sent by princy but not able to get exact output.. plz look into it and help me out. 
  10. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 01 Mar 2011 Link to this post

    hi yogesh,

    plz see check with below link..
    http://www.telerik.com/help/aspnet-ajax/grid-html-export.html

    if u still not able to solve your problem than send code or give information of column's datatype.

    thanks,
    Jayesh Goyani
  11. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 10 Apr 2011 Link to this post

    Cell alignment does not work for PDF exported grid. All cells are aligned to the left always. Bug?
    dataItem[column.UniqueName].Style["text-align"] = "right";

    - does not influence

    Add-on: maybe it's due to latest version update, but now it's ok
  12. Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 13 Apr 2011 Link to this post

    Hello Alexander,

    I recommend that you set these styles to the header cells and not to the whole header row. This is needed because the header cells are TH elements (not TD as in the other rows).

    Best regards,
    Daniel
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  13. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 14 Apr 2011 Link to this post

    Thanks, I've already fixed the problem.
    Here is approach for setting proper width and also proper alignment:
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
     
                if (IsExport)
                {
                    const int charWidth = 10;
     
                    foreach (GridColumn column in MasterTableView.Columns)
                    {
                        if (column is GridEditableColumn)
                        {
                            var columnWidth = 0;
                            var currentlyExportingTypes = column.CurrentFilterValue.GetTypesFromSeparatedString<DSExportType>();
     
                            if (currentlyExportingTypes.Count > 0 && !currentlyExportingTypes.Contains(_currentlyExportingType.Value) || column.HeaderText == "#" || column.UniqueName == "checker")
                            {
                                column.Visible = false;
                            }
     
                            if (column.Visible)
                            {
                                var alignmentCssClassPattern = new Regex("\\b(?<alignment>ca|la|ra)\\b");
                                var alignmentCssClassPatternMatch = alignmentCssClassPattern.Match(column.HeaderStyle.CssClass);
                                foreach (GridDataItem dataItem in MasterTableView.Items)
                                {
                                    var labelLengths = 0;
                                    if (column is GridTemplateColumn)
                                    {
                                        var labelTexts = (from label in dataItem[column.UniqueName].Controls.OfType<WebControl>().OfType<Label>() select label.Text);
                                        labelLengths += labelTexts.Sum(labelText => labelText.Length);
                                    }
                                    else
                                    {
                                        labelLengths = dataItem[column.UniqueName].Text.Length;
                                    }
                                    var currentColumnWidth = labelLengths * charWidth;
                                    if (columnWidth < currentColumnWidth)
                                    {
                                        columnWidth = currentColumnWidth;
                                    }
                                    if (alignmentCssClassPatternMatch.Success)
                                    {
                                        switch (alignmentCssClassPatternMatch.Groups["alignment"].Value)
                                        {
                                            case "ca":
                                                dataItem[column.UniqueName].Style["text-align"] = "center";
                                                break;
                                            case "ra":
                                                dataItem[column.UniqueName].Style["text-align"] = "right";
                                                break;
                                            default:
                                                dataItem[column.UniqueName].Style["text-align"] = "left";
                                                break;
                                        }
                                    }
                                }
                                foreach (GridHeaderItem headerItem in MasterTableView.GetItems(GridItemType.Header))
                                {
                                    if (alignmentCssClassPatternMatch.Success)
                                    {
                                        switch (alignmentCssClassPatternMatch.Groups["alignment"].Value)
                                        {
                                            case "ca":
                                                headerItem[column.UniqueName].Style["text-align"] = "center";
                                                break;
                                            case "ra":
                                                headerItem[column.UniqueName].Style["text-align"] = "right";
                                                break;
                                            default:
                                                headerItem[column.UniqueName].Style["text-align"] = "left";
                                                break;
                                        }
                                    }
                                }
                                column.HeaderStyle.Width = Unit.Pixel(Math.Max(columnWidth, column.HeaderText.Length * charWidth));
                            }
                        }
                    }
                    _currentlyExportingType = null;
                }
    }


  14. Rohan
    Rohan avatar
    94 posts
    Member since:
    Jul 2012

    Posted 13 Oct 2012 Link to this post

    Hi all ,
    When i am exporting the rad Grid data to PDF some of columns data are overwrite to each other and second is when i am resizing the grid columns and exporting to PDF then columns lost the size .... how can do this ......
  15. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 17 Oct 2012 Link to this post

    Hi Rohan,

    I guess you are talking about overlapping the content when the column width is smaller then the content itself. If that is what you are asking for, you could check out the following forum post.
    As to the resizing you could take a look the following help topic and check out the "Resizing Column" section.

    Kind regards,
    Kostadin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  16. Ranjith
    Ranjith avatar
    6 posts
    Member since:
    Jan 2014

    Posted 01 Jan 2014 Link to this post

    Hi,

    I am trying to export PDF from my RadGrid and its exporting successfully. But i am facing some design issues in the exported PDF file.

    1. All the text are coming with Hyperlinks in the exported PDF.
    2. Set the Forecolor and width, which are not getting reflected in the exported PDF.

    Below is my c# code 

    protected void btnExportToPDF_Click(object sender, ImageClickEventArgs e)
            {
                ApplyStylesToPDFExport(RadGrid1.MasterTableView);
                RadGrid1.ExportSettings.OpenInNewWindow = true;
                RadGrid1.ExportSettings.IgnorePaging = true;
                RadGrid1.ExportSettings.FileName = "Test";
                RadGrid1.MasterTableView.ExportToPdf();
            }
    private void ApplyStylesToPDFExport(GridTableView view)
            {
                GridItem headerItem = view.GetItems(GridItemType.Header)[0];
                foreach (TableCell cell in headerItem.Cells)
                {
    switch (cell.Text)
                    {
                        case "A":
                            cell.Width = Unit.Pixel(5); // Tried like this. Not reflecting
                            break;
                        case "B":
                            cell.Style["width"] = "15px"; // Tried like this. Not reflecting
                            break;
    }
                    cell.Style["font-family"] = "Verdana";
                    cell.Style["font-bold"] = "true";
                    cell.Style["text-align"] = "left";
                    cell.Style["vertical-align"] = "middle";
                    cell.Style["font-size"] = "8px";
                    cell.ForeColor = System.Drawing.Color.Black; // Tried like this. Not reflecting

                }
                GridItem[] dataItems = view.GetItems(GridItemType.Item);
                foreach (GridItem item in dataItems)
                {
                    foreach (TableCell cell in item.Cells)
                    {
                        cell.Style["font-family"] = "Verdana";
                        cell.Style["text-align"] = "left";
                        cell.Style["vertical-align"] = "left";
                        cell.Style["font-size"] = "6px";
                        cell.Style["text-decoration"] = "none"; // Tried like this. Not reflecting

                        cell.Style["ForeColor"] = "#000";
                    }
                }
                dataItems = view.GetItems(GridItemType.AlternatingItem);
                foreach (GridItem item in dataItems)
                {
                    foreach (TableCell cell in item.Cells)
                    {
                        cell.Style["font-family"] = "Verdana";
                        cell.Style["text-align"] = "left";
                        cell.Style["vertical-align"] = "middle";
                        cell.Style["font-size"] = "6px";
                        cell.Style["text-decoration"] = "none";
                        cell.ForeColor = System.Drawing.Color.Black;
                    }
                }
            }

    Please advise.

    Thanks
    Ranjith
  17. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 06 Jan 2014 Link to this post

    Hi Ranjith,

    I have already answered you in the following forum thread. I would ask you to continue our conversation there and close this one.

    Regards,
    Kostadin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017