ASP.NET 将转发器导出到 excel,excel 文件打开时出现错误“'RepeaterExport.xls' 的文件格式和扩展名不匹配

ASP.NET Export repeater to excel, excel file open with error "The file format and extension of 'RepeaterExport.xls' don't match

我正在进行导出工作,以提高性能,因此我将转发器数据导出到 excel。

这是我的转发器aspx

 <asp:Repeater runat="server" ID="rpEmployee" DataSourceID="dsEmployee" OnItemDataBound="rpemployee_ItemDataBound">
    <HeaderTemplate>
        <table id="EmployeeTbl" class="display">
            <thead>
                <tr>
                    <th><input type="checkbox" onclick="SelectAll(this)" /></th>
                    <th align="left">Name</th>
                    <th>Origin Ctry</th>
                    <th>Passport No</th>
                    <th>Passport Expired On</th>
                    <th>Permit Expired On</th>
                    <th align="left">Operating Centre</th>
                    <th align="left">Kdn File</th>
                    <th>Submission Batch</th>
                    <th>Submitted To MyEG On</th>
                    <th>Status</th>
                    <th>Last Updated On</th>
                    <th>Payment Ref</th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td class="cmd">
                <asp:CheckBox runat="server" ID="chkSelect" />
                <asp:HiddenField runat="server" ID="EmployeeId" Value='<%#Eval("EmployeeId") %>' />
                <asp:HiddenField runat="server" ID="PPBatchEmployeeId" Value='<%#Eval("PPBatchEmployeeId") %>' />
            </td>
            <td><a href='../workers/FWBiodata.aspx?Id=<%#Eval("EmpRowGuid") %>'><%#Eval("EmployeeName")%> <span style="color: red"><%#(bool)(Eval("Employee.SLTag"))?"(SL)":"" %></span></a></td>
            <td class="cmd"><%#Eval("OriginCtry")%></td>
            <td class="cmd"><%#Eval("PassportNo")%></td>
            <td class="cmd"><%#AppLib.Format.Date(Eval("PassportExpireOn"))%></td>
            <td class="cmd"><%#AppLib.Format.Date(Eval("PermitExpireOn"))%></td>
            <td><%#Eval("OCName")%></td>
            <td><%#Eval("KdnFileNo") %></td>
            <td class="cmd"><%#Eval("PPDeliveryBatchId.ReferenceNo")%></td>
            <td class="cmd"><%#AppLib.Format.AppDateTime(Eval("SubmitToMyEGOn"))%></td>
            <td class="cmd"><%#Eval("PPStatus.Status") %></td>
            <td class="cmd"><%#AppLib.Format.AppDateTime(Eval("LastUpdatedOn"))%></td>
            <td><asp:Label ID="lblPymtReferenceNo" runat="server" Text="" /></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody>
        </table>
    </FooterTemplate>
</asp:Repeater>

下面是我的代码。

protected void btnDownload_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    foreach (RepeaterItem i in rpEmployee.Items)
    {
        HiddenField EmployeeId = (HiddenField)i.FindControl("EmployeeId");
        EmployeeId.Visible = false;
        HiddenField PPBatchEmployeeId = (HiddenField)i.FindControl("PPBatchEmployeeId");
        PPBatchEmployeeId.Visible = false;
        CheckBox chkitem = (CheckBox)i.FindControl("chkSelect");
        chkitem.Visible = false;
    }

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls");
    //Response.Charset = "UTF-8";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    rpEmployee.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

一切似乎都很好,但是当我打开excel。它在我单击“是”后显示此 ,然后它起作用了。当我转到我朋友的笔记本电脑上并尝试打开它时,它又出现了另一个错误

我不知道为什么。谁能帮帮我?

您确定它是有效的 excel 文件吗? 对我来说它看起来 html 文件... 带有 xls 扩展名!

要么创建一个 real/valid xml 文件,要么从 ms

查看这篇文章

XLS file will not open in Excel 2016, only gray view