使用 C#、Visual Studio 2008 和 .NET Framework 3.5。我可以直接从DataGridView导出记录到excel,问题是我需要导出其他数据(纯文本)。
这是我现在的excel:
这就是我需要显示标题(表格顶部的行)的方式:
这就是我现在的做法:
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=InformeEstudios.xls");
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
myGridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
//MY METHOD
private void BinData()
{
SqlConnection conex = new SqlConnection("Data Source=xx.xxx.x.xx;Initial
Catalog=MYDATABASE;User ID=MYUSER;Password=MYPASS");
SqlDataAdapter ad = new SqlDataAdapter("select ROW1, ROW2 from MYTABLE", conex);
DataSet ds = new DataSet();
ad.Fill(ds);
myGridView1.DataSource = ds;
myGridView1.DataBind();
}
我怎样才能做到这一点,我怎样才能手动编写这两个标题行。提前致谢!