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

radGrid ExportToExcel styles

8 Answers 538 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dima
Top achievements
Rank 1
dima asked on 15 Jan 2009, 09:53 AM
Hello,
When I export radGrid  to excel file I got a table with different text-align styles in each row. !!!!!
I'm trying set style: 

radGrid1.MasterTableView.Style[

"text-align"] = "left";
but this don't solve my problem. This style for a table and not for a table cells.
how can I set cells styles?

Thanks a lot

 

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Jan 2009, 12:00 PM
Hi Dima,

I tried exporting to excel in my end and I am getting all the cells as left aligned. You can try aligning the cells by using following code snippets and see if it working fine for you.

CS:
bool isExport = false
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    if (isExport && e.Item is GridDataItem) 
    { 
        GridDataItem Item = (GridDataItem)e.Item; 
        foreach (TableCell cell in Item.Cells) 
        { 
            cell.Style["text-align"] = "left"
        } 
    } 
 
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == RadGrid.ExportToExcelCommandName) 
    { 
        isExport = true
        RadGrid1.ExportSettings.IgnorePaging = true
    } 

Thanks,
Princy.
0
dima
Top achievements
Rank 1
answered on 15 Jan 2009, 03:21 PM
Hi, Princy

Thanks for you solution. I'll try it
0
Shinu
Top achievements
Rank 2
answered on 16 Jan 2009, 03:55 AM
Hi Dima,

You can also have a look at the following help article which give more tips and tricks in exporting.
Exporting tips and tricks

Shinu
0
Georgi Krustev
Telerik team
answered on 16 Jan 2009, 12:16 PM
Hi guys,

The suggested approach by Princy is perfectly right when the type of the exported grid is Html.
My suggestion is when you exporting grid in html format to wire the ExcelExportCellFormatting event. This event is raised for every cell which is created. In this event handler just set horizontal align of the cell to System.Web.UI.WebControls.HorizontalAlign.Left.

Here is a code snippet showing how to achieve this:
    protected void RadGrid1_ExcelExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e) 
    { 
        e.Cell.HorizontalAlign = HorizontalAlign.Left; 
    } 

If you export the grid in ExcelML format, you have to wire other two events - ExcelMLExportStylesCreated and ExcelMLExportRowCreated. When ExcelMLExportStylesCreated event is raised you can create the style which you want to apply to the cells and add it to the style collection of the grid. Then in the RadGrid1_ExcelMLExportRowCreated event handler merely assign your custom style as a cell style.

Below is a code excerpts which describes this approach:
    void RadGrid1_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e) 
    { 
        Telerik.Web.UI.GridExcelBuilder.StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle"); 
        myStyle.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.HorizontalAlignmentType.Left; 
        e.Styles.Add(myStyle); //adding your custom style. 
    } 
 
    void RadGrid1_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e) 
    { 
        if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow) 
        { 
            foreach (Telerik.Web.UI.GridExcelBuilder.CellElement cell in e.Row.Cells) 
            { 
                cell.StyleValue = "MyCustomStyle"
            } 
        } 
    } 

Best regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 15 Mar 2012, 02:07 PM
Hello,
I tried the solution Georgi mentioned using the ExcelMLExportStylesCreated and ExcelMLExportRowCreated events, but
when i try to open the exported Excel file an error occurs and the workbook can not be opened. Is there anything else
I need to consider when I use these two events?
0
Daniel
Telerik team
answered on 19 Mar 2012, 10:02 AM
Hi Laura,

Please post your code here. Also sharing the error message could help.
In the meantime you could examine our online examples where the you can find a fully runnable ExcelML demo.
ExcelML demo
ExcelML topic

Kind regards,
Daniel
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
Laura
Top achievements
Rank 1
answered on 19 Mar 2012, 12:19 PM
Hi Daniel,

thanks for your reply. Here is my code:

Codebehind:
  public void TeamAdm_ExcelMLExportStylesCreated(object sender, GridExportExcelMLStyleCreatedArgs e)
       {
           StyleElement gHeaderStyle = new StyleElement("gridHeaderStyle");     
           gHeaderStyle.FontStyle.Bold = true;
           gHeaderStyle.FontStyle.Size = 10.0;
           e.Styles.Add(gHeaderStyle);  
       }
public void TeamAdm_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
       {        
          if (e.RowType == GridExportExcelMLRowType.HeaderRow) 
        {     
               foreach (Telerik.Web.UI.GridExcelBuilder.CellElement c in e.Row.Cells)
               {
                   c.StyleValue = "gridHeaderStyle";
               
       }

Aspx:
<telerik:RadGrid ID="gv_Team_Overview" runat="server" OnPreRender="gv_Team_Overview_PreRender"
              OnItemCommand="gv_Team_Overview_ItemCommand"   
              OnItemDataBound="gv_Team_Overview_ItemDataBound"
              OnColumnsReorder="gv_Team_Overview_ColumnsReorder"                                      
              AutoGenerateColumns="True" AllowSorting="true"
              AllowPaging="True" AllowMultiRowSelection="true"                          
              ExcelMLExportStylesCreated="TeamAdm_ExcelMLExportStylesCreated"
              OnExcelMLExportRowCreated="TeamAdm_ExcelMLExportRowCreated">
        <ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True"
            HideStructureColumns="True" FileName="Team Overview">
          <Excel Format="ExcelML" />
         </ExportSettings>
         <ClientSettings EnableRowHoverStyle="True" AllowColumnsReorder="True" 
            <Selecting AllowRowSelect="true" />
            <Scrolling AllowScroll="True" SaveScrollPosition="True" 
             UseStaticHeaders="True" />
         </ClientSettings>
         <MasterTableView ShowFooter="False" CommandItemDisplay="Top" 
             ExpandCollapseColumn-Display="true">
             <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
             </RowIndicatorColumn>
             <CommandItemSettings ShowAddNewRecordButton="false" 
              ShowExportToExcelButton="true"  ShowRefreshButton="true">
             </CommandItemSettings>
             <Columns>
             <telerik:GridBoundColumn DataField="NR" HeaderText="NR" UniqueName="NR">
             </telerik:GridBoundColumn>
             <telerik:GridBoundColumn DataField="TEAM" HeaderText="TEAM" UniqueName="TEAM">
             </telerik:GridBoundColumn>
             <telerik:GridBoundColumn DataField="NO_OF_WORKGROUPS" 
               HeaderText="NO_OF_WORKGROUPS" UniqueName="NO_OF_WORKGROUPS">
             </telerik:GridBoundColumn>
             </Columns>
            <PagerStyle AlwaysVisible="True" Mode="NextPrevNumericAndAdvanced" />
         </MasterTableView>
         <PagerStyle Mode="NextPrevNumericAndAdvanced" />
         <FilterMenu EnableImageSprites="False"></FilterMenu>
         <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
         </HeaderContextMenu>
</telerik:RadGrid>

I attached the error message. It refers to a log file, which I also attached. The log file says there is an xml error in the table because of an invalid value.

Thanks,
Laura
0
Laura
Top achievements
Rank 1
answered on 21 Mar 2012, 09:03 AM

I was able to solve the problem in the meantime. Thanks anyway!

Tags
Grid
Asked by
dima
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
dima
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Georgi Krustev
Telerik team
Laura
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or