这是一个动态生成table 的程序我在aspx页面上是如下设置:
<asp:table id="aTable1" CssClass="datatab" runat="server" horizontalalign="Center" BorderStyle="Outset" CellPadding="0" CellSpacing="0" GridLines="Both">
</asp:table>在后台代码添加 具体的行列 TableRow m_row0 = new TableRow(); TableCell m_Celltmp0 = new TableCell();
m_Celltmp0.Text = "<font color=black>" + DP_Name + "</font> " + "考勤" + "日期 " + d_KaoQinDate.ToShortDateString() + "到" + d_KaoQinDate.AddMonths(1).AddDays(-1).ToShortDateString();
m_Celltmp0.ColumnSpan = 99;
m_Celltmp0.HorizontalAlign = HorizontalAlign.Center;
m_Celltmp0.Height = 40;
//m_Celltmp0.CssClass = "a5";
m_Celltmp0.Attributes["style"] = "font-size:18pt;color:blue;text-align:center;";
m_row0.Cells.Add(m_Celltmp0);
aTable1.Rows.Add(m_row0);
在导出到excel的时候,只有aspx页面的内容<asp:table id="aTable1" CssClass="datatab" runat="server" horizontalalign="Center" BorderStyle="Outset" CellPadding="0" CellSpacing="0" GridLines="Both">
</asp:table>
我是这么导出的 protected void btn_excel_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo cul;
StringWriter oStringWriter;
HtmlTextWriter oHtmlTextWriter;
string strFileName = DateTime.Now.ToString(); Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
//Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//Me.EnableViewState = True cul = new System.Globalization.CultureInfo("ZH-CN", true);
oStringWriter = new StringWriter(cul);
oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
aTable1.RenderControl(oHtmlTextWriter);//tblOrder是需要输出的表格的ID;如果是输出datagrid的内容那就替换成datagrid的ID就可以了.
Response.Write(oStringWriter.ToString());
Response.End();
}
<asp:table id="aTable1" CssClass="datatab" runat="server" horizontalalign="Center" BorderStyle="Outset" CellPadding="0" CellSpacing="0" GridLines="Both">
</asp:table>在后台代码添加 具体的行列 TableRow m_row0 = new TableRow(); TableCell m_Celltmp0 = new TableCell();
m_Celltmp0.Text = "<font color=black>" + DP_Name + "</font> " + "考勤" + "日期 " + d_KaoQinDate.ToShortDateString() + "到" + d_KaoQinDate.AddMonths(1).AddDays(-1).ToShortDateString();
m_Celltmp0.ColumnSpan = 99;
m_Celltmp0.HorizontalAlign = HorizontalAlign.Center;
m_Celltmp0.Height = 40;
//m_Celltmp0.CssClass = "a5";
m_Celltmp0.Attributes["style"] = "font-size:18pt;color:blue;text-align:center;";
m_row0.Cells.Add(m_Celltmp0);
aTable1.Rows.Add(m_row0);
在导出到excel的时候,只有aspx页面的内容<asp:table id="aTable1" CssClass="datatab" runat="server" horizontalalign="Center" BorderStyle="Outset" CellPadding="0" CellSpacing="0" GridLines="Both">
</asp:table>
我是这么导出的 protected void btn_excel_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo cul;
StringWriter oStringWriter;
HtmlTextWriter oHtmlTextWriter;
string strFileName = DateTime.Now.ToString(); Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
//Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//Me.EnableViewState = True cul = new System.Globalization.CultureInfo("ZH-CN", true);
oStringWriter = new StringWriter(cul);
oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
aTable1.RenderControl(oHtmlTextWriter);//tblOrder是需要输出的表格的ID;如果是输出datagrid的内容那就替换成datagrid的ID就可以了.
Response.Write(oStringWriter.ToString());
Response.End();
}
这一句改为:
Response.ContentType = "application/vnd.ms-excel";//设置输出文件类型为excel文件。 你试一下