3

いくつかのSQLServerデータベースがあり、次のメソッドでデータを返します。

protected DataTable GetProductsData()
{
            if (ddTipRaport.SelectedItem.Text == "Toate Cerintele")
            { DataTable dt = new DataTable();
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
             "Data Source=BOGDAN-PC\\BOGDAN;Initial Catalog=ePlanning;Integrated Security=SSPI;Connect Timeout=10;TrustServerCertificate=True ");
                System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand();
                comm.CommandText = "Select PS.titlu_cerinta as 'Categorie Cerinta', P.id_subcerinta as 'ID',p.titlu_subcerinta as 'Titlu Cerinta',p.data_crearii as 'Data Crearii',p.autor as 'Autor',p.revizuita as'Revizuita',p.revizuitor as'Revizuitor',p.prioritate as 'Prioritate(Importanta)',p.acoperire as 'Stare Acoperire' from subCerinteProiect P inner join cerinteProiect PS on P.id_cerinta=PS.id_cerinta where PS.id_proiect = " + (Request.QueryString["proiect"]).ToString() + " order by PS.id_cerinta desc";
                comm.Connection = con;
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(comm);

                da.Fill(dt);
                con.Close();
                return dt;
            }

そして、私はこの方法ですべてをドキュメントに書き込みます:

  protected void ExportDataTableToWord()
  {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-word";
            Response.AddHeader("content-disposition", "attachment;filename=Raport_"+ DateTime.Now.ToShortDateString()+"_"+DateTime.Now.ToShortTimeString()+".doc");

            StringWriter sWriter = new StringWriter();

            HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);

            GridView GridView1 = new GridView();
            GridView1.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            GridView1.DataSource = GetProductsData();
            GridView1.DataBind();
            GridView1.RenderControl(hWriter);

            Response.Write(sWriter.ToString());

            Response.End();
 }

私の質問は、エクスポートされたグリッドビューの上にタイトルを追加する方法です、ありがとう!

4

2 に答える 2

2

探しているプロパティは、キャプションhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.caption.aspxと呼ばれます。

例えば

GridView1.Caption = "StackOverFlow Grid...";
于 2012-07-16T21:57:27.430 に答える
0

私はこれを使用し、それは機能しました、それがあなたが望むものであるかどうかを確認してみてください

    string datestyle = @"<style>.date { mso-number-format:'Short Date'; }</style>";
    foreach (GridViewRow oItem in gvEdit.Rows)
        oItem.Cells[4].Attributes.Add("class", "date");
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=SupplierList.xls");
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter WriteItem = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlText = new HtmlTextWriter(WriteItem);
    Response.Write(datestyle);
    gvEdit.RenderControl(htmlText);
    Response.Write(WriteItem.ToString());
    Response.End();

私は私を助けたリンクを残します

http://forums.asp.net/t/1666667.aspx

http://shawpnendu.blogspot.com/2009/03/export-gridview-data-to-excelword-text.html

于 2012-07-16T22:02:07.540 に答える