2

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

どうすればこれを達成できますか、これらの2つのヘッダー行を手動で書くにはどうすればよいですか. 前もって感謝します!

4

1 に答える 1

0

control.RenderControl(...) は html を出力しているだけです。したがって、最初に独自の html を出力してから、rendercontrol を呼び出します。

于 2012-09-13T18:50:25.210 に答える