【问题标题】:Export plain text (a header) to excel with c#使用c#将纯文本(标题)导出到excel
【发布时间】:2012-09-14 02:50:25
【问题描述】:

使用 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();
}

我怎样才能做到这一点,我怎样才能手动编写这两个标题行。提前致谢!

【问题讨论】:

    标签: c# sql-server-2008 export-to-excel


    【解决方案1】:

    control.RenderControl(...) 只是输出 html。所以先输出自己的html..再调用rendercontrol。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多