我正在处理导出,以提高性能,因此我将转发器数据导出到 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 扩展名!
要么创建一个真实/有效的 xml 文件,要么从 ms 查看这篇文章