radgrid export to excel not working(the file is corrupt and cannot be opened)

4 posts, 0 answers
  1. Vitaly
    Vitaly  avatar
    68 posts
    Member since:
    Aug 2011

    Posted 16 Jul 2014 Link to this post

    Good afternoon,
    I have following RadGrid in my aspx page:

    <telerik:RadGrid ID="EmplReqGrid" runat="server" Skin="Office2010Blue"
    GridLines="None" AllowPaging="true" AllowSorting="true"
    onexcelmlexportrowcreated="EmplReqGrid_ExcelMLExportRowCreated"
    onexcelmlexportstylescreated="EmplReqGrid_ExcelMLExportStylesCreated"
    onitemcommand="EmplReqGrid_ItemCommand" onitemcreated="EmplReqGrid_ItemCreated"
    onitemdatabound="EmplReqGrid_ItemDataBound"
    onneeddatasource="EmplReqGrid_NeedDataSource"
    onpageindexchanged="EmplReqGrid_PageIndexChanged"
    onpagesizechanged="EmplReqGrid_PageSizeChanged"
    onpdfexporting="EmplReqGrid_PdfExporting"
    onsortcommand="EmplReqGrid_SortCommand">
    <ExportSettings FileName="EmplReqRpt" OpenInNewWindow="true" IgnorePaging="true" ExportOnlyData="true">
    <Excel Format="ExcelML" FileExtension="xls"/>
    <Pdf FontType="Subset" PaperSize="letter" />
    </ExportSettings>
    <MasterTableView AutoGenerateColumns="false" AllowMultiColumnSorting="true" >

    <Columns>

    <telerik:GridBoundColumn HeaderText="Eis-Id" DataField="PRI" UniqueName="PRI" ReadOnly="True" SortExpression="PRI" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>

    <telerik:GridBoundColumn HeaderText="ReqNo" DataField="TRKNO" UniqueName="TRKNO" ReadOnly="True" SortExpression="TRKNO" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>

    <telerik:GridBoundColumn HeaderText="Term-Id" DataField="TERMID" UniqueName="TERMID" ReadOnly="True" SortExpression="TERMID" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn HeaderText="Start Date" DataField="TERMST" UniqueName="TERMST" ReadOnly="true" DataFormatString="{0:MM/dd/yyyy}" SortExpression="TERMST" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn HeaderText="End Date" DataField="TERMEND" UniqueName="TERMEND" ReadOnly="true" DataFormatString="{0:MM/dd/yyyy}" SortExpression="TERMEND" HeaderButtonType="TextButton">
    </telerik:GridBoundColumn>

    <telerik:GridBoundColumn HeaderText="No of Periods" DataField="NUMP" UniqueName="NUMP" ReadOnly="True" SortExpression="NUMP" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>


    <telerik:GridBoundColumn HeaderText="Req Status" DataField="APPRFLAG" UniqueName="APPRFLAG" ReadOnly="True" SortExpression="APPRFLAG" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>


    <telerik:GridBoundColumn HeaderText="Rec Status" DataField="STATUS" UniqueName="STATUS" ReadOnly="True" SortExpression="STATUS" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn HeaderText="Req Status Date" DataField="FORMDATE" UniqueName="FORMDATE" ReadOnly="True" DataFormatString="{0:MM/dd/yyyy}" SortExpression="FORMDATE" HeaderButtonType="TextButton" >
    </telerik:GridBoundColumn>

    <telerik:GridTemplateColumn HeaderText="Link to Form Button" UniqueName="ButtonColumn">
    <ItemTemplate>
    <asp:ImageButton ID="ReqFormBtn" ImageUrl="~/Image/update.jpg"
    BackColor="transparent" ImageAlign ="Top"
    ToolTip="update,re-submit or print the request" runat="server" CommandName="Redirect" />

    </ItemTemplate>
    </telerik:GridTemplateColumn>

    </Columns>
    </MasterTableView>
    <ClientSettings EnableRowHoverStyle="True" EnablePostBackOnRowClick = "false">
    <Selecting AllowRowSelect="false" />
    </ClientSettings>
    </telerik:RadGrid>

    In code behind I press on Export to Excel button:

    protected void ExporttoExcel_Click1(object sender, ImageClickEventArgs e)
    {
    try
    {
    if (!IsPostBack)
    return;
    RadGrid ReqInfoGrid = (RadGrid)EmpReqReport.FindItemByValue("EmployeeData").FindControl("EmplReqGrid");
    ReqInfoGrid.DataSource = Session["dtGrid"];
    ReqInfoGrid.DataBind();
    ReqInfoGrid.MasterTableView.ExportToExcel();
    }
    catch (Exception ex)
    {
    throw;
    }
    }

    For some reason excel spreadsheet not open instead I am getting error window:
    'The file is corrupt and cannot be opened'.

    Please help me to resolve this issue.
    Thanks so much.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 Jul 2014 in reply to Vitaly Link to this post

    Hi Vitaly,

    Please take a look at the following documentation which discuss about this error. It is expected to receive this warning when opening the generated xls file in Microsoft Office 2007.
    ExcelML basics

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vitaly
    Vitaly  avatar
    68 posts
    Member since:
    Aug 2011

    Posted 17 Jul 2014 in reply to Princy Link to this post

    Hi Princy,
    thanks for your quick respond.Yes first I am getting this warning message and I click ok and after that I am receiving
    error saying "The file is corrupt and cannot be opened".
    here is my code in c#:

    protected void ExporttoExcel_Click1(object sender, ImageClickEventArgs e)
    {
    try
    {
    if (!IsPostBack)
    return;
    RadGrid ReqInfoGrid = (RadGrid)EmpReqReport.FindItemByValue("EmployeeData").FindControl("EmplReqGrid");
    ReqInfoGrid.DataSource = Session["dtGrid"];
    ReqInfoGrid.DataBind();
    ReqInfoGrid.MasterTableView.ExportToExcel();
    }
    catch (Exception ex)
    {
    throw;
    }
    }
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2014 in reply to Vitaly Link to this post

    Hi Vitaly,

    The cause of this warning message is explained in details in the following blog posts:
    MSDN blogs: Excel 2007 Extension Warning On Opening Excel Workbook from a Web Site
    Grinn blog: "The file you are trying to open, '[filename]', is in a different format".

    Hope this helps.
    Thanks,
    Princy
Back to Top