为什么当我在pdf文件中下载gridview时,它显示错误

时间:2014-06-02 04:34:20

标签: c# sql visual-studio-2010

我实际上想要下载包含pid的pdf文件,以及从1月到12月的月份。

月份包含浮点数,逗号后面有9个数字,因此它使gridview比它应该更宽。当我下载pdf文件时,它显示错误' System.Web.UI.HtmlTextWriter'

这里到底发生了什么?

这是我的代码

 connection.Open();

 SqlCommand dm = new SqlCommand("select id,sum (case when [Month] = 1 then demand else 0.0 end) January, sum(case when [Month] = 2 then demand  else 0.0 end) February,sum(case when [Month] = 3 then demand  else 0.0 end) March ,sum(case when [Month] = 4 then demand  else 0.0 end) April ,sum(case when [Month] = 5 then demand  else 0.0 end) May ,sum(case when [Month] = 6 then demand  else 0.0 end) June ,sum(case when [Month] = 7 then demand  else 0.0 end) July ,sum(case when [Month] = 8 then demand  else 0.0 end) August ,sum(case when [Month] = 9 then demand  else 0.0 end) September ,sum(case when [Month] = 10 then demand  else 0.0 end) October ,sum(case when [Month] = 11 then demand  else 0.0 end) November ,sum(case when [Month] = 12 then demand  else 0.0 end) December from reorder group by id", connection);
    SqlDataReader fdm = dm.ExecuteReader();

    if (fdm.HasRows)
    {
        fdm.Read();
        GridView3.DataSource = fdm;

    }
    connection.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter wsfdm = new StringWriter();
    HtmlTextWriter fdmws = new HtmlTextWriter(wsfdm);
    GridView3.AllowPaging = false;
    GridView3.RenderControl(fdmws);
    GridView3.HeaderRow.Style.Add("width", "20%");
    GridView3.HeaderRow.Style.Add("font-size", "8x");
    GridView3.Style.Add("text-decoration", "none");
    GridView3.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
    GridView3.Style.Add("font-size", "8px");

    StringReader fgdm = new StringReader(fdmws.ToString());
    Document documentpdf = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(documentpdf);
    PdfWriter.GetInstance(documentpdf, Response.OutputStream);
    documentpdf.Open();
    htmlparser.Parse(fgdm);
    documentpdf.Close();
    Response.Write(documentpdf);
    Response.End();
}

2 个答案:

答案 0 :(得分:0)

您必须使用StringWriter类中的StringReader对象而不是HtmlTextWriter

试试这个

    connection.Open();
    SqlCommand dm = new SqlCommand("select id,sum (case when [Month] = 1 then demand else 0.0 end) January, sum(case when [Month] = 2 then demand  else 0.0 end) February,sum(case when [Month] = 3 then demand  else 0.0 end) March ,sum(case when [Month] = 4 then demand  else 0.0 end) April ,sum(case when [Month] = 5 then demand  else 0.0 end) May ,sum(case when [Month] = 6 then demand  else 0.0 end) June ,sum(case when [Month] = 7 then demand  else 0.0 end) July ,sum(case when [Month] = 8 then demand  else 0.0 end) August ,sum(case when [Month] = 9 then demand  else 0.0 end) September ,sum(case when [Month] = 10 then demand  else 0.0 end) October ,sum(case when [Month] = 11 then demand  else 0.0 end) November ,sum(case when [Month] = 12 then demand  else 0.0 end) December from reorder group by id", connection);
    SqlDataReader fdm = dm.ExecuteReader();

    if (fdm.HasRows)
    {
        fdm.Read();
        GridView3.DataSource = fdm;

    }
    connection.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter wsfdm = new StringWriter();
    HtmlTextWriter fdmws = new HtmlTextWriter(wsfdm);
    GridView3.AllowPaging = false;
    GridView3.RenderControl(fdmws);
    GridView3.HeaderRow.Style.Add("width", "20%");
    GridView3.HeaderRow.Style.Add("font-size", "8x");
    GridView3.Style.Add("text-decoration", "none");
    GridView3.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
    GridView3.Style.Add("font-size", "8px");

    StringReader fgdm = new StringReader(wsfdm.ToString());
    Document documentpdf = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(documentpdf);
    PdfWriter.GetInstance(documentpdf, Response.OutputStream);
    documentpdf.Open();
    htmlparser.Parse(fgdm);
    documentpdf.Close();
    Response.Write(documentpdf);
    Response.End();
}

<强>来源:

请看这里export-gridview-to-pdf

答案 1 :(得分:0)

连接关闭后粘贴此代码:

using (StringWriter sw = new StringWriter())
{
    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
    {
        //To Export all pages
        GridView1.AllowPaging = false;           

        GridView3.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
    }
}

不要忘记包含iTextSharp DLL。