c# - 使用 c# 将纯文本(标题)导出到 excel

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

使用 C#、Visual Studio 2008 和 .NET Framework 3.5。我可以直接从DataGridView导出记录到excel,问题是我需要导出其他数据(纯文本)。

现在这是我的 excel:

Excel table without header

这就是我需要显示标题(表格顶部的行)的方式:

excel with more info

这就是我现在的做法:

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

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

最佳答案

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

关于c# - 使用 c# 将纯文本(标题)导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12412680/

相关文章:

c# - 如果我有两个时间段,我如何确定它们是否重叠?

c# - 路由中带有 id 的 ASP.NET Core 表单

c# - 是否可以在 Dynamics CRM (2013/2016) 中以编程方式禁用审计?

c# - c#中字符串中最后一个逗号之前的所有元素

sql-server - 返回日期为 null 作为 t-sql 中的最大值

sql - 为什么 DROP TABLE 然后重新创建 TABLE?

python - 对 pandas 数据框使用 to_excel(...) 函数时如何区分空字符串、np.nan 和 None?

java - 将数据库架构与 SVN 存储库中的数据库架构进行比较

ruby-on-rails - 直接下载一个xls文件而不用Spreadsheet gem写入目录

c# - 在多线程C#应用程序中使用互操作的Excel文件操作失败