This is a migrated thread and some comments may be shown as answers.

radgrid export to exce,word,pdf

15 Answers 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
yogesh
Top achievements
Rank 1
yogesh asked on 28 Feb 2011, 10:53 AM
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

15 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Feb 2011, 11:46 AM
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
0
yogesh
Top achievements
Rank 1
answered on 28 Feb 2011, 04:55 PM
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...
0
Daniel
Telerik team
answered on 28 Feb 2011, 10:55 PM
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!
0
yogesh
Top achievements
Rank 1
answered on 01 Mar 2011, 06:19 AM
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....
0
Princy
Top achievements
Rank 2
answered on 01 Mar 2011, 06:43 AM
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.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 01 Mar 2011, 08:39 AM
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
0
yogesh
Top achievements
Rank 1
answered on 01 Mar 2011, 08:59 AM
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. 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 01 Mar 2011, 10:30 AM
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
0
Alexander
Top achievements
Rank 1
answered on 10 Apr 2011, 08:50 AM
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
0
Daniel
Telerik team
answered on 13 Apr 2011, 09:35 PM
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.

0
Alexander
Top achievements
Rank 1
answered on 14 Apr 2011, 10:28 AM
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;
            }
}


0
Rohan
Top achievements
Rank 1
answered on 13 Oct 2012, 10:30 AM
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 ......
0
Kostadin
Telerik team
answered on 17 Oct 2012, 12:11 PM
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.
0
Ranjith
Top achievements
Rank 1
answered on 01 Jan 2014, 09:07 AM
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
0
Kostadin
Telerik team
answered on 06 Jan 2014, 09:42 AM
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.
Tags
Grid
Asked by
yogesh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
yogesh
Top achievements
Rank 1
Daniel
Telerik team
Princy
Top achievements
Rank 2
Alexander
Top achievements
Rank 1
Rohan
Top achievements
Rank 1
Kostadin
Telerik team
Ranjith
Top achievements
Rank 1
Share this question
or