标题属性不适用于asp.net中的gridview

时间:2013-01-21 08:14:14

标签: asp.net-mvc gridview pdf-generation caption

我试图从asp.net中的数据库导出pdf文件。在这里,我点了这个链接:http://www.aspsnippets.com/Articles/Export-DataSet-or-DataTable-to-Word-Excel-PDF-and-CSV-Formats.aspx

导出.doc文件:

string strQuery = "select CustomerID, ContactName, City, PostalCode" +
                      " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
    DataTable dt = GetData(cmd);

    //Create a dummy GridView
    GridView GridView1 = new GridView();
    GridView1.AllowPaging = false;
    GridView1.DataSource = dt;
    GridView1.DataBind();

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
        "attachment;filename=DataTable.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    GridView1.Caption = "<br><br><br>" + "<h3>" + "Register Users Table" + "</h3>" + "<br>";
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

此处标题显示在.doc文件中GridView的顶部

对于导出.pdf文件:

string strQuery = "select CustomerID, ContactName, City, PostalCode" +
        " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
    DataTable dt = GetData(cmd);

    //Create a dummy GridView
    GridView GridView1 = new GridView();
    GridView1.AllowPaging = false;
    GridView1.DataSource = dt;
    GridView1.DataBind();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition",
        "attachment;filename=DataTable.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    GridView1.Caption = "<br><br><br>" + "<h3>" + "Register Users Table" + "</h3>" + "<br>";
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End(); 

这里没有显示GridView的标题......如何在C#asp.net中导出pdf文件时显示Gridview的标题

如何做到这一点..我的代码有什么问题吗? 提前谢谢..

0 个答案:

没有答案