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

[Solved] Export MS Excel/MS Word/CSV (peculiar)

1 Answer 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
li bin
Top achievements
Rank 1
li bin asked on 24 Jul 2009, 02:32 AM
Hello all,
I have done an example like the demo Grid / MS Excel/MS Word/CSV.
I only choose the checkbox of Export only data ,it's OK.
I choose both of checkboxes of Export only data and Ignore paging (exports all pages)  ,it's OK.
I choose none of the checkbox ,the word only one column of PermissionId and fulled the whole word,the permissionid'data all appeared and then the current page's permissionid'data ; the CSV's columns all fulled in the first column; the Excel has all the data (not one to one)and then the current page.
I only choose the Ignore paging (exports all pages) ,like i choose none,but the data more.


all of this ,the export to ExcelML doesn't relize. when i debug
if (e.Row.Cells.Count > 0)  
                {  
                  if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))  
                    {  
                        e.Row.Cells[0].StyleValue = "MyCustomStyle";  
                    }                      
                } 

it can't come into it.

here is my code:
<head runat="server">  
 
    <title>Untitled Page</title> 
</head> 
<body > 
    <form id="form1" runat="server" > 
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">  
                <AjaxSettings> 
                    <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">  
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting>   
                    <telerik:AjaxSetting AjaxControlID="RadGrid1">  
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting>   
                </AjaxSettings> 
            </telerik:RadAjaxManager> 
    <asp:ScriptManager ID="ScriptManager" runat="server" /> 
     
            <asp:CheckBox ID="CheckBox1" Text="Export only data" runat="server"/>  
            <br /> 
            <asp:CheckBox ID="CheckBox2" Text="Ignore paging (exports all pages)" runat="server"/>  
            <br /> 
            <br />   
             
    <div> 
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
            <script type="text/javascript">  
 
              
            function ShowEditForm(id, rowIndex)  
            {  
                var grid = $find("<%= RadGrid1.ClientID %>");  
                  
                var rowControl = grid.get_masterTableView().get_dataItems()[rowIndex].get_element();   
                grid.get_masterTableView().selectItem(rowControl, true);  
                          
                window.radopen("WebForm2.aspx?PermissionsId=" + id, "UserListDialog");  
                return false;   
            }  
            function ShowInsertForm()  
            {  
               window.radopen("WebForm2.aspx", "UserListDialog");  
               return false;  
            }  
            function refreshGrid(arg)  
            {  
             if(!arg)  
             {  
             $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");               
                }  
                else  
                {  
             $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindAndNavigate");               
                }  
            }  
            function RowDblClick(sender, eventArgs)  
            {  
             window.radopen("WebForm2.aspx?PermissionsId=" + eventArgs.getDataKeyValue("PermissionsId"), "UserListDialog");  
            }  
            </script> 
        </telerik:RadCodeBlock>   
          
     
    <asp:Button ID="Button1" runat="server" Text="Export to Excel" Width="150px"   
            onclick="Button1_Click"  /> 
    <asp:Button ID="Button2"  Width="150px" Text="Export to Word"  runat="server"   
            onclick="Button2_Click"/>  
    <asp:Button ID="Button3"  Width="150px" Text="Export to CSV" runat="server"   
            onclick="Button3_Click"/>  
    <asp:Button ID="Button4"  Width="150px" Text="Export to ExcelML" runat="server"   
            onclick="Button4_Click"/>  
     
        <telerik:RadGrid   
              
                ID="RadGrid1"   
                runat="server"   
                AllowPaging="True" AllowFilteringByColumn="True" 
                Width="97%"   
               OnNeedDataSource="RadGrid1_NeedDataSource"   
               OnRowDataBound="GridView1_RowDataBound" 
               OnItemCreated="RadGrid1_ItemCreated"              
              OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated" 
          
            GridLines="None">  
            <ExportSettings IgnorePaging="False" Pdf-AllowAdd="False" Pdf-AllowPrinting="True" ExportOnlyData="False"  > 
            <Excel Format="Html"  /> 
              
      
            <Csv ColumnDelimiter="Colon" RowDelimiter="NewLine"  /> 
             <Pdf PageHeight="297mm" PageWidth="210mm" PageTitle="Permissions menu"  /> 
            </ExportSettings> 
                <PagerStyle Mode="NumericPages" /> 
                <MasterTableView   
                    AutoGenerateColumns="False"   
                    DataKeyNames="PermissionsId"   
                    ClientDataKeyNames="PermissionsId" 
                    Width="100%" 
                    CommandItemDisplay="Top"   
                    PageSize="5">  
                    <Columns> 
                        <telerik:GridBoundColumn   
                            DataField="PermissionsId"   
                            HeaderText="PermissionsId" 
                            ReadOnly="True"   
                            SortExpression="PermissionsId"   
                            UniqueName="PermissionsId" > 
                         
                        <FilterTemplate> 
                            <telerik:RadComboBox   
                            ID="RadComboBox1" DataTextField="PermissionsId" DataValueField="PermissionsId" DataSource="<%# DataBindPermissionsId() %>" 
                                AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("PermissionsId").CurrentFilterValue %>' 
                                runat="server" OnClientSelectedIndexChanged="PermissionsIdIndexChanged">  
                                <Items> 
                                    <telerik:RadComboBoxItem Text="All" /> 
                                </Items> 
                            </telerik:RadComboBox> 
                               <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
                                <script type="text/javascript">  
                                    function PermissionsIdIndexChanged(sender,args) {  
                       
                                        var tableView=$find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");  
                                        tableView.filter("PermissionsId",args.get_item().get_value(),"EqualTo");  
                                    }  
                                </script> 
                            </telerik:RadScriptBlock> 
                        </FilterTemplate> 
                         </telerik:GridBoundColumn> 
                          
                        <telerik:GridBoundColumn   
                            DataField="PermissionsName"   
                            HeaderText="PermissionsName"   
                            SortExpression="PermissionsName" 
                            UniqueName="PermissionsName">  
                       <FilterTemplate> 
                            <telerik:RadComboBox   
                            ID="RadComboBox2" DataTextField="PermissionsName" DataValueField="PermissionsName" DataSource="<%# DataBindPermissionsName() %>" 
                                AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("PermissionsName").CurrentFilterValue %>' 
                                runat="server" OnClientSelectedIndexChanged="PermissionsNameIndexChanged" EnableLoadOnDemand="True" Filter="StartsWith">  
                                <Items> 
                                    <telerik:RadComboBoxItem Text="All" /> 
                                </Items> 
                            </telerik:RadComboBox> 
                               <telerik:RadScriptBlock ID="RadScriptBlock2" runat="server">  
                                <script type="text/javascript">  
                                    function PermissionsNameIndexChanged(sender,args) {  
                                        var tableView=$find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");  
                                        tableView.filter("PermissionsName",args.get_item().get_value(),"EqualTo");  
                                    }  
                                </script> 
                            </telerik:RadScriptBlock> 
                        </FilterTemplate>       
                              
                              
                        </telerik:GridBoundColumn>   
                            
                 <telerik:GridBoundColumn   
                         DataField="PermissionsNo" HeaderText="PermissionsNo"   
                            SortExpression="PermissionsNo"    UniqueName="PermissionsNo" > 
                             <FilterTemplate> 
                            <telerik:RadComboBox ID="RadComboBox3" DataTextField="PermissionsNo" DataValueField="PermissionsNo" DataSource="<%# DataBindPermissionsNo() %>" 
                                AppendDataBoundItems="true"  SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("PermissionsNo").CurrentFilterValue %>' 
                                runat="server" OnClientSelectedIndexChanged="PermissionsNoIndexChanged">  
                                <Items> 
                                    <telerik:RadComboBoxItem Text="All" /> 
                                </Items> 
                            </telerik:RadComboBox> 
                            <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">  
                                <script type="text/javascript">  
                                    function PermissionsNoIndexChanged(sender,args) {  
                                        var tableView=$find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");  
                                        tableView.filter("PermissionsNo",args.get_item().get_value(),"EqualTo");  
                                    }  
                                </script> 
                            </telerik:RadScriptBlock> 
                        </FilterTemplate>           
                           <%--   <ItemTemplate>     
                             <img src='Image/<%# Eval("PermissionsNo") %>.gif' alt="" style="vertical-align:middle;margin-right:7px;"/>  
                              </ItemTemplate>     --%>              
                    </telerik:GridBoundColumn>   
 
                      
       <%--   <telerik:GridDateTimeColumn   
                   FilterControlWidth="120px" DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate"   
                        UniqueName="CreateDate" PickerType="DatePicker" DataFormatString="{0:D}" CurrentFilterFunction="Contains">  
                        <HeaderStyle Width="160px" /> 
                    </telerik:GridDateTimeColumn>--%> 
                                         
               <telerik:GridTemplateColumn   
                            UniqueName="TemplateEditColumn">  
                             <ItemTemplate>                                                              
                                <asp:Image ID="EditLink" ImageUrl="../Images/Write2.gif"  runat="server" /> 
                                  <%--  <img src='Image/<%# Eval("PermissionsNo") %>.gif' alt="" style="vertical-align:middle;margin-right:7px;"/><%# Eval("PermissionsNo")%>  --%>     
                  </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                  
                    </Columns> 
                    <CommandItemTemplate> 
                        <href="#" onclick="return ShowInsertForm();">Add New Record</a> 
                    </CommandItemTemplate> 
                </MasterTableView> 
                <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">  
                    <Selecting AllowRowSelect="true" /> 
                    <ClientEvents OnRowDblClick="RowDblClick" /> 
                </ClientSettings> 
            </telerik:RadGrid> 
                           
            <telerik:RadWindowManager ID="RadWindowManager1" runat="server">  
                <Windows> 
                    <telerik:RadWindow ID="UserListDialog" runat="server" Title="Editing record" Height="400px" 
                        Width="300px" Left="150px" ReloadOnShow="true" ShowContentDuringLoad="false" Modal="true" /> 
                </Windows> 
            </telerik:RadWindowManager> 
 
    </div> 
    </form> 
</body> 
and .cs
   
    public partial class WebForm1 : System.Web.UI.Page  
    {  
    
    PermissionsService pre = new PermissionsService();  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            
        }  
 
        protected TList<Permissions> DataBindPermissionsId()  
        {  
            return pre.GetAll();  
        }  
 
        protected TList<Permissions> DataBindPermissionsName()  
        {  
            return pre.GetAll();  
        }  
 
        protected TList<Permissions> DataBindPermissionsNo()  
        {  
            return pre.GetAll();  
        }  
 
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            Siemens.BPM.Profile.Entities.TList<Permissions> PerEntities = new TList<Permissions>();  
           PerEntities = pre.GetAll();              
           this.RadGrid1.DataSource = pre.GetAll();  
             
        }       
 
   /*     protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                Image editLink = (Image)e.Item.FindControl("EditLink");  
                editLink.Attributes["href"] = "#";  
                editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PermissionsId"], e.Item.ItemIndex);  
            }  
        }  
    */  
 
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)  
        {  
            if (e.Argument == "Rebind")  
            {  
 
                RadGrid1.MasterTableView.SortExpressions.Clear();  
                RadGrid1.MasterTableView.GroupByExpressions.Clear();  
                RadGrid1.Rebind();  
            }  
            else if (e.Argument == "RebindAndNavigate")  
            {  
                RadGrid1.MasterTableView.SortExpressions.Clear();  
                RadGrid1.MasterTableView.GroupByExpressions.Clear();  
                RadGrid1RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;  
                RadGrid1.Rebind();  
            }  
        }  
 
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            ConfigureExport();  
            RadGrid1.MasterTableView.ExportToExcel();  
        }  
 
        protected void Button2_Click(object sender, EventArgs e)  
        {  
            ConfigureExport();  
     
            RadGrid1.MasterTableView.ExportToWord();  
           
 
        }  
 
        protected void Button3_Click(object sender, EventArgs e)  
        {  
            ConfigureExport();  
              
            RadGrid1.MasterTableView.ExportToCSV();  
        }  
 
        protected void Button4_Click(object sender, EventArgs e)  
        {  
          ConfigureExport();  
            RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML;  
            CheckBox1.Checked = true;  
            RadGrid1.ExportSettings.ExportOnlyData = true;  
            RadGrid1.MasterTableView.ExportToExcel();  
         
        }  
        bool isPdfExport = false;  
 
   /*   protected void RadGrid1_ExcelMLExportRowCreated(object source,Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)  
        {  
           if (e.RowType ==Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)  
            {  
                if (e.Row.Cells.Count > 0)  
                {  
                     
                    if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))  
                    {  
                        e.Row.Cells[0].StyleValue = "MyCustomStyle";  
 
                    }  
                       
                }  
            }  
        }  
    */  
             
        protected void RadGrid1_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);  
        }  
 
 
       public void ConfigureExport()                                                                                                                                 
       {  
            RadGrid1.ExportSettings.ExportOnlyData = CheckBox1.Checked;  
           RadGrid1.ExportSettings.IgnorePaging = CheckBox2.Checked;  
           
        }  
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        {  
 
            GridViewRow row = e.Row;  
        }  
 
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (isPdfExport && e.Item is GridDataItem)  
            {  
                GridDataItem Item = (GridDataItem)e.Item;  
                Item["PermissionsId"].Style["text-align"] = "center"; // Set the style for column with uniquename FirstName     
            }  
        }    
 
         
      
 
    } 

Thanks!!!

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 30 Jul 2009, 06:08 AM
Hello li bin,

I cannot see how the RadGrid1_ExcelMLExportRowCreated event handler has been attached to RadGrid's event. Check this out to make sure the event handler is properly attached.

Kind regards,
Veli
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.
Tags
Grid
Asked by
li bin
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or