日期:2014-05-20  浏览次数:20796 次

复制网上代码,gridview导出PDF后打开错误?
C# code

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        // pdfDoc.Open();
        htmlparser.Parse(sr);
        // pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End(); 

如果我不把pdfDoc.Open()和pdfDoc.Close();注释掉,程序就会提示没有pages;可是当我注释掉后可以导出PDF,导出完后的PDF却打不开,请问是什么原因?

------解决方案--------------------
http://topic.csdn.net/u/20110413/13/75d910f5-6be7-44f2-9bcd-3e7452d52f18.html