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

Export to excel

2 Answers 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Akki
Top achievements
Rank 1
Akki asked on 09 Jan 2012, 01:48 PM
In RadGrid i was using GridTemplateColumn, inside that i was using link button. While exporting to excel, only those column values are not exported to the grid.
This is my code
.aspx
<telerik:RadGrid ID="RadGrid_Invbystatus" runat="server" AllowPaging="True" AllowSorting="True"
                        GridLines="Both" EnableLinqExpressions="false" Width="95%" AutoGenerateEditColumn="false"
                        AutoGenerateColumns="False" OnPageIndexChanged="RadGrid_Invbystatus_PageIndexChanged"
                        Visible="false" OnExcelExportCellFormatting="RadGrid_Invbystatus_ExportCellFormatting"
                        GroupHeaderItemStyle-HorizontalAlign="Left">
                        <PagerStyle Mode="NextPrevAndNumeric" />
                        <HeaderStyle BorderStyle="Solid" Font-Bold="true" HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Left" Font-Underline="false" />
                        <AlternatingItemStyle HorizontalAlign="Left" Font-Underline="false" />
                        <ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
                        </ExportSettings>
                        <MasterTableView AllowPaging="true" CommandItemDisplay="Top" GridLines="Both" ShowGroupFooter="true">
                            <GroupByExpressions>
                                <telerik:GridGroupByExpression>
                                    <SelectFields>
                                        <telerik:GridGroupByField FieldAlias="STATUS" FieldName="STATUS" HeaderText="Status">
                                        </telerik:GridGroupByField>
                                    </SelectFields>
                                    <GroupByFields>
                                        <telerik:GridGroupByField FieldName="STATUS" HeaderText="Status"></telerik:GridGroupByField>
                                    </GroupByFields>
                                </telerik:GridGroupByExpression>
                            </GroupByExpressions>
                            <CommandItemSettings ShowExportToPdfButton="true" ShowExportToWordButton="true" ShowExportToExcelButton="true"
                                ShowAddNewRecordButton="false" ShowRefreshButton="false" ShowExportToCsvButton="false" />
                            <Columns>
                                <telerik:GridTemplateColumn HeaderText="Product" UniqueName="Product" DataField="PROD_ID"
                                    ItemStyle-HorizontalAlign="Left" SortExpression="PROD_ID">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkprodNum" runat="server" OnClick="lnkprodNum_Click" ForeColor="DarkBlue"
                                            Text='<%# DataBinder.Eval (Container.DataItem, "PRODUCT_CODE") %>' ValidationGroup='<%# DataBinder.Eval (Container.DataItem, "PROD_ID") %>'>
                                        </asp:LinkButton>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn HeaderText="Description" UniqueName="Description" DataField="PROD_DESC"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemStyle Width="15%" />
                                    <HeaderStyle Width="15%" />
                                </telerik:GridBoundColumn>
                                <telerik:GridCalculatedColumn HeaderText="Qty" UniqueName="qty" ItemStyle-HorizontalAlign="Right"
                                    DataFields="QTTY, PACKSHORT_DESC" Expression='{0}+ " " +{1}'>
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridCalculatedColumn>
                                <telerik:GridBoundColumn HeaderText="Lot" UniqueName="lot" DataField="LOT_CODE" ItemStyle-HorizontalAlign="Left">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridBoundColumn>
                                <telerik:GridDateTimeColumn DataType="System.DateTime" HeaderText="Exp. Date" UniqueName="expDate"
                                    ItemStyle-HorizontalAlign="Left" DataField="EXP_DT" DataFormatString="<%$Appsettings:dateformat%>">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridDateTimeColumn>
                                <telerik:GridDateTimeColumn DataType="System.DateTime" HeaderText="Received" UniqueName="recvDate"
                                    ItemStyle-HorizontalAlign="Left" DataField="RECV_DATE" DataFormatString="<%$Appsettings:dateformat%>">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridDateTimeColumn>
                                <telerik:GridTemplateColumn HeaderText="Order No." UniqueName="Order_No." DataField="ORDER_NO"
                                    ItemStyle-HorizontalAlign="Left" SortExpression="ORDER_NO">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkOrdNum" runat="server" OnClick="lnkrefNum_Click" ForeColor="DarkBlue"
                                            CommandArgument='<%# DataBinder.Eval (Container.DataItem, "ORDER_NO2") %>' Text='<%# DataBinder.Eval (Container.DataItem, "ORDER_NO") %>'
                                            ValidationGroup='<%# DataBinder.Eval (Container.DataItem, "SRORDER_ID") %>'>
                                        </asp:LinkButton>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn HeaderText="Reference No" UniqueName="Reference_No" DataField="ORDER_NO2"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Warehouse" UniqueName="Warehouse" ItemStyle-HorizontalAlign="Left"
                                    DataField="WAREHOUSE_CODE">
                                    <ItemStyle Width="10%" />
                                    <HeaderStyle Width="10%" />
                                </telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
.cs
protected void RadGrid_InvAdjus_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||
                e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||
                e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
            {
                ConfigureFilter();
            }
            else if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToPdfCommandName)
            {                
                filter = "Owner:" + ddlOwner.SelectedItem.Text + "Warehouse:" + ddlwarehouse.SelectedItem.Text;
                if (!string.IsNullOrEmpty(txtSupplier.Text))
                {
                    filter += "Supplier:" + txtSupplier.Text + "Supplier Description:" + txtsupplierdesc.Text;
                }
                if (!string.IsNullOrEmpty(txtproductcode.Text))
                {
                    filter += "Product Code:" + txtproductcode.Text + "Product Description:" + txtproddesc.Text;
                }
                filter += "Product Type:" + ddlprodtype.SelectedItem.Text + "Start Date:" + rdpStartDate.SelectedDate + "End Date:" + rdpEndDate.SelectedDate;
                //RadGrid_InvAdjus.MasterTableView.Caption = filter;
                RadGrid_InvAdjus.ExportSettings.Pdf.PageTitle = filter;
            }

         
        }

        protected void RadGrid_InvAdjus_ExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
        {
            GridDataItem item = e.Cell.Parent as GridDataItem;

            if (e.FormattedColumn.UniqueName == "Date")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Product")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Description")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "qty")
            {
                e.Cell.Style["text-align"] = "right";
            }
            if (e.FormattedColumn.UniqueName == "lot")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Order_No")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Reference_No")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Warehouse")
            {
                e.Cell.Style["text-align"] = "left";
            }
            if (e.FormattedColumn.UniqueName == "Template1")
            {
                e.Cell.Style["text-align"] = "left";

            }

            GridHeaderItem HeaderItem = (GridHeaderItem)RadGrid_InvAdjus.MasterTableView.GetItems(GridItemType.Header)[0];
            foreach (TableCell cell in HeaderItem.Cells)
            {
                cell.Style["text-align"] = "left";
                cell.Style["color"] = "#ff0000";
                cell.Style["border"] = "thin solid black";
                cell.Style["background-color"] = "#cccccc";
                cell.Style["font-weight"] = "normal";               
            }  
        }

Add My excel will be like this(Product column is empty, and i want to left align the caption part)




2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 09 Jan 2012, 02:41 PM
Hello,

Try setting ExportOnlyData as false.

Thanks,
Princy.
0
Nithya Rani
Top achievements
Rank 1
answered on 10 Jan 2012, 05:43 AM
Thank you princy its working now.
Tags
Grid
Asked by
Akki
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Nithya Rani
Top achievements
Rank 1
Share this question
or