请教table 导出到Excel的问题
aspx 页面
<asp:Table ID="MainTable" runat="server" GridLines="Both" CellPadding="0" CellSpacing="0" BorderColor="Black" BorderStyle="Solid" Font-Size="10">
<asp:TableRow ID="TableRow93" runat="server" Height="30px">
<asp:TableCell ID="TableCell482" ColumnSpan="15" runat="server" HorizontalAlign="Center" BorderColor="Black">水井积压关井情况汇总</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow56" runat="server" Height="25px">
<asp:TableCell ID="TableCell366" RowSpan="2" runat="server" BorderColor="Black" Width="40px">序号</asp:TableCell>
<asp:TableCell ID="TableCell375" RowSpan="2" runat="server" BorderColor="Black" Width="160px">所属矿队</asp:TableCell>
<asp:TableCell ID="TableCell380" RowSpan="2" runat="server" BorderColor="Black" Width="160px">井号</asp:TableCell>
<asp:TableCell ID="TableCell381" ColumnSpan="2" runat="server" BorderColor="Black" Width="320px">注水情况</asp:TableCell>
<asp:TableCell ID="TableCell382" RowSpan="2" runat="server" BorderColor="Black" Width="160px">关井原因</asp:TableCell>
<asp:TableCell ID="TableCell1" RowSpan="2" runat="server" BorderColor="Black" Width="160px">关井日期</asp:TableCell>
<asp:TableCell ID="TableCell2" RowSpan="2" runat="server" BorderColor="Black" Width="160px">关井天数</asp:TableCell>
<asp:TableCell ID="TableCell3" RowSpan="2" runat="server" BorderColor="Black" Width="160px">备注</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow57" runat="server" Height="25px">
<asp:TableCell ID="TableCell384" runat="server" BorderColor="Black">稳定日注</asp:TableCell>
<asp:TableCell ID="TableCell385" runat="server" BorderColor="Black">当日注水</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="Button1" runat="server" Text="导出" OnClick="export_Click" />
cs
protected void export_Click(object sender, EventArgs e)
{
ToExcel(MainTable);
}
//导入到excel
private void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=wlpx.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
//计算生成表
public void Computegxsc(DataTable Dt)
{
TableRow r;
TableCell c;
for (int i = 0; i < Dt.Rows.Count; i++)
{
r = new TableRow();
Dt.Rows[i][0] = Convert.ToString(i + 1);
//for (int j = 0; j < 7; j++)
for (int j = 0; j < 9; j++)
{
c = new TableCell();
if (i % 2 == 0)
{
//c.BackColor = Color.Gray;
c.BackColor = Color.Silver;
}
if (j < 3 || j>=5)
{
c.HorizontalAlign = HorizontalAlign.Left;
}
else
{
c.HorizontalAlign = HorizontalAlign.Right;
}
c.BorderStyle = BorderStyle.Solid;
c.Height = 30;
c.Wrap = false;
if (j < 3 || j == 7)
{
c.Text = Dt.Rows[i][j].ToString();
}
else
{
c.Text = String.Format("{0:F2}", Dt.Rows[i][j]);
}
r.Cells.Add(c);
}
MainTable.Rows.Add(r);
}
}
问题:表头在aspx生成,内容通过后台调用Computegxsc生成表格内容,想导出整个表,但只是导出了表头,请问如何解决?