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

Export to Excel

16 Answers 358 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Asra
Top achievements
Rank 1
Asra asked on 02 Jun 2009, 08:53 AM
Hi ALL,
                Iam using Export to Excel for the RadGrid. in code behind iam writing like this

RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.

GridExcelExportFormat.ExcelML;

 

RadGrid1.ExportSettings.ExportOnlyData =

true;

 

RadGrid1.MasterTableView.ExportToExcel();

On  RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML; it is giving me error as object Reference is not set.

Can any one pls let me know why it is giving that error.

Regards,
asra

16 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 02 Jun 2009, 04:20 PM
Hello Asra,

I followed your scenario in order to replicate the issue but to no avail.
Please find attached a simple runnable application that demonstrates the needed approach. Give it a try and let me know if it works for you.

Best wishes,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
towpse
Top achievements
Rank 2
answered on 06 Apr 2010, 06:49 PM
i'm not able to get this export to excel to work.
i'm not getting expected behaviour. what is happening however (and this is the same behaviour in all browsers) is the grid is simply reloaded or rerendered in the same browser window but all command item icons are gone, all icons i use in my grid are gone and replaced by their tooltip text, and paging functionality is gone and only one page of data is visible.

I am using version 2009.3.1314.35.

This is the code in my ItemCommand event for exporting to excel:

                    case "ExportToExcel":
                        this.InactiveUnitGrid.ExportSettings.OpenInNewWindow = true;
                        this.InactiveUnitGrid.ExportSettings.ExportOnlyData = true;
                        this.InactiveUnitGrid.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML;
                        this.InactiveUnitGrid.MasterTableView.ExportToExcel();
                        break;

Update. I just tried to handle these grid events but they never fired. Breakpoints were never hit.

                                                                            OnExcelMLExportStylesCreated="InactiveUnitGrid_ExcelMLExportStylesCreated"
                                                                            OnExcelMLExportRowCreated="InactiveUnitGrid_ExcelMLExportRowCreated"

        protected void mygridd_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
        {
            if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)
            {
                if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))
                {
                    e.Row.Cells[0].StyleValue = "MyCustomStyle";
                }
            }
        }

        protected void mygrid_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)
        {
            foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)
            {
                if (style.Id == "headerStyle")
                {
                    style.FontStyle.Bold = true;
                    style.FontStyle.Color = System.Drawing.Color.Gainsboro;
                    style.InteriorStyle.Color = System.Drawing.Color.Wheat;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
                else if (style.Id == "itemStyle")
                {
                    style.InteriorStyle.Color = System.Drawing.Color.WhiteSmoke;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
                else if (style.Id == "alternatingItemStyle")
                {
                    style.InteriorStyle.Color = System.Drawing.Color.LightGray;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
            }

            Telerik.Web.UI.GridExcelBuilder.StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle");
            myStyle.FontStyle.Bold = true;
            myStyle.FontStyle.Italic = true;
            myStyle.InteriorStyle.Color = System.Drawing.Color.Gray;
            myStyle.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
            e.Styles.Add(myStyle);
        }
0
towpse
Top achievements
Rank 2
answered on 06 Apr 2010, 07:43 PM
I have a column that is used only to display a command button used for editing details.
How do I exclude this column from exporting to excel?
0
Pavlina
Telerik team
answered on 08 Apr 2010, 01:31 PM
Hello,

Please, refer to the help article below, for more information about how to achieve your goal.
ExcelML export

Regards,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
towpse
Top achievements
Rank 2
answered on 08 Apr 2010, 02:05 PM
What might it mean then if the ExcelMLExportRowCreated or ExcelMLExportStylesCreated never get fired?
0
Bruno
Top achievements
Rank 2
answered on 08 Apr 2010, 05:31 PM
Hi

this is Bruno amd oing project with Telerik control

"i'm not able to get this export to excel to work.
i'm not getting expected behaviour. what is happening however (and this is the same behaviour in all browsers) is the grid is simply reloaded or rerendered in the same browser window but all command item icons are gone, all icons i use in my grid are gone and replaced by their tooltip text, and paging functionality is gone and only one page of data is visible."
i was get the same behaving with ajax enable - stop ajaxing and try again

What might it mean then if the ExcelMLExportRowCreated or ExcelMLExportStylesCreated never get fired?
wire the gridexporting event and checking the export type - am bet that you are use the mistake type

exsample:
    void BrunoGridView_GridExporting(object source, GridExportingArgs e)
    {
        //check e.ExportType
    }

sorry for my bed english

Bruno
0
towpse
Top achievements
Rank 2
answered on 08 Apr 2010, 05:41 PM
Bruno, thanks for the reply!
Right the ajaxing! oi.
It must be that because Grid_GridExporting isn`t firing either. Is there a way to turn off ajaxing only when I want to export to Excel then.... hmmmmmmmmmmmm.

argh....
I`m using Update Panels!
0
Bruno
Top achievements
Rank 2
answered on 08 Apr 2010, 05:47 PM
Hi

this is Bruno and am happy that help

you can use client site code as catch on request start event
function ajax_manager_requeststart(sender, args)
{
   if (args.get_eventTarget().indexOf("myexportbutonid") >= 0)
{
       args.set_enableAjax(false); }
   }

i attach this cod on ajaxmanager client start request event

Bruno
0
towpse
Top achievements
Rank 2
answered on 08 Apr 2010, 06:23 PM
that might work with ajax manager.

the UI i`m working on is update panel driven.

i doubt there`s anything i can do with update panels and i don`t really want to refactor to use an ajax manager for this particular UI at this point....

any suggestions from the telerik folké

0
towpse
Top achievements
Rank 2
answered on 08 Apr 2010, 08:02 PM
Okay, cool http://www.telerik.com/help/aspnet-ajax/grdexportwithajax.html
I have export working. But I'm still having some issues.
The excel file gets saved but I only get one page of data. And the column that contains a button is being included as text that says something like "Click to Edit" for each row....

I configure the export thus:
            UnitsGrid.ExportSettings.ExportOnlyData = true;
            UnitsGrid.ExportSettings.IgnorePaging = false;
            UnitsGrid.ExportSettings.OpenInNewWindow = true;

I try hiding the edit icon button column 
 this.InactiveUnitGrid.MasterTableView.Columns[0].Visible = false;

And try hiding command item headers
            foreach (GridItem commandItem in this.InactiveUnitGrid.MasterTableView.GetItems(GridItemType.CommandItem))
            {
                commandItem.Visible = false;
            }

I've set the excel format to ExcelML as opposed to HTML. That caused the InactiveUnitGrid_ExcelMLExportRowCreated and InactiveUnitGrid_ExcelMLExportStylesCreated to fire.

I can catch a breakpoint in those events. The code is changing style attributes which don't get reflected in the excel output.
The grid does get rebound but paging is not ignored (only the first 50 rows of the 1st page are exported)...
0
towpse
Top achievements
Rank 2
answered on 08 Apr 2010, 08:40 PM
arhg. changing pages breaks the export functionality. it puts me back in the state i was before i solved the ajax problem....
the export only works on page one! anyone else run into this?
0
Bruno
Top achievements
Rank 2
answered on 14 Apr 2010, 12:04 PM
hi

this is Bruno

i never see that problem in normal condition
 do yo use custom pagings?

i will ask my colleage he may know this ....
0
towpse
Top achievements
Rank 2
answered on 14 Apr 2010, 12:31 PM
just using regular standard pagings.

thanks
0
Bruno
Top achievements
Rank 2
answered on 19 Apr 2010, 01:00 PM
Bruno's colleague here

I'm not quite familiar with this particular problem. Looking at the description of the problem I think it might be a databinding issue. One of the most useful extras of RadGrid is the needatasource binding - you will hardly have any databinding problems using it.

Sorry for the delay but we are busy working on a nasty internal project right now
0
Zinith Patel
Top achievements
Rank 1
answered on 12 Aug 2010, 09:48 AM
Hello,

I am also having such kind of issue. 
As per my requirement, I have to show hide column at runtime. So, while exporting if that column is visible then it should be exported.
This is working fine if I select dgUnPart.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Html. But, when I select dgUnPart.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML then it does not export that column.

Kindly do needful.

0
Pavlina
Telerik team
answered on 12 Aug 2010, 05:01 PM
Hello Zinith,

To export in ExcelML format the ExportOnlyData property should be set to true. Review this help article for more information:
http://www.telerik.com/help/aspnet-ajax/grid-excelml-export.html

All the best,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Asra
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
towpse
Top achievements
Rank 2
Bruno
Top achievements
Rank 2
Zinith Patel
Top achievements
Rank 1
Share this question
or