将网格视图数据导出为pdf

时间:2014-01-27 10:50:48

标签: c# asp.net

我收到错误:'GridView'类型的控件'MainContent_dgvRpt'必须放在带有runat = server的表单标签内。

asp代码:

<asp:GridView ID="dgvRpt" runat="server" Width="900px" CssClass="Grid" OnRowDataBound="dgvShowRpt_RowDataBound">
<RowStyle HorizontalAlign="Left" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="black" Font-Names="Arial" />
<PagerStyle BackColor="#CDCDCD" ForeColor="White" HorizontalAlign="Center" Font-Names="Arial" />
<HeaderStyle BackColor="#CDCDCD" Font-Bold="True" ForeColor="black" Font-Names="Arial" />

<HeaderStyle CssClass="GridHeader" Font-Names="Arial"></HeaderStyle>
<AlternatingRowStyle CssClass="GridAtlItem" />
</asp:GridView>.

c#c​​ode:

GameLib.Utilities.ExportGrid(dgvRpt, "pdf", GameLib.Reports.ReportName);

     if (ExportType == "pdf")
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Buffer = true;
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + Filename + ".pdf");

                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter hw = new HtmlTextWriter(sw);


                    gv.AllowPaging = false;
                    gv.HeaderRow.ForeColor = System.Drawing.Color.Black;
                    gv.FooterRow.ForeColor = System.Drawing.Color.Black;

                    gv.HeaderRow.Style.Add("font-Color", "Black");
                    gv.HeaderRow.Style.Add("font-size", "13px");
                    gv.HeaderRow.Style.Add("text-decoration", "none");
                    gv.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

                    gv.Style.Add("font-Color", "Black");
                    gv.Style.Add("text-decoration", "none");
                    gv.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                    gv.Style.Add("font-size", "11px");
                    gv.ForeColor = System.Drawing.Color.Black;

                    gv.RenderControl(hw);
                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
                    pdfDoc.Open();

                    htmlparser.Parse(sr);
                    pdfDoc.Close();
                    HttpContext.Current.Response.Write(pdfDoc);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();

在gv.RenderControl(hw)中出错;但我在runat = server中插入代码。请帮我解决问题。提前谢谢

3 个答案:

答案 0 :(得分:0)

您必须告诉编译器通过重写VerifyRenderingInServerForm事件来显式地呈现控件。 像:

    public override void VerifyRenderingInServerForm(Control control)
    {
       //
    }

http://www.codeproject.com/Questions/381483/Control-GridView1-of-type-GridView-must-be-placed

答案 1 :(得分:0)

我将GridView导出到excel的逻辑相同,但gridview仅存在于代码隐藏中。这是我的代码:

protected void ExportToExcel(DataTable dt)
        {
            //Create a dummy GridView
            GridView GridView1 = new GridView();
            GridView1.ShowHeaderWhenEmpty = true;
            GridView1.AllowPaging = false;
            GridView1.DataSource = dt;
            GridView1.DataBind();            

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;

            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            List<int> decimalModeColumnsIndexes = new List<int>();

            // Set the column's header with the caption of the Column in the DataTable
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                GridView1.HeaderRow.Cells[i].Text = dt.Columns[i].Caption;                
            }

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                //Apply text style to each cell
                if (decimalModeColumnsIndexes.Count != 0)
                {
                    for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                    {
                        if (decimalModeColumnsIndexes.Count(colIndex => colIndex == j) == 1)
                        {
                            GridView1.Rows[i].Cells[j].Attributes.Add("class", "decimalmode");
                        }
                        else
                        {
                            GridView1.Rows[i].Cells[j].Attributes.Add("class", "textmode");
                        }
                    }
                }
                else
                {
                    GridView1.Rows[i].Attributes.Add("class", "textmode");
                }

            }

            String fileName = dt.TableName;
            fileName = String.Format("{0}_{1}", fileName, DateTime.UtcNow);

            fileName = fileName.Replace(':', '-');
            fileName = fileName.Replace(' ', '-');


            GridView1.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } .decimalmode { mso-number-format:""0\.00""; } </style>";


            HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", fileName));
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

        }

答案 2 :(得分:0)

你可以使用它:修改以前的代码。

protected void ExportToPdf(DataTable dt)
            {
                GridView GridView1 = new GridView();
                GridView1.ShowHeaderWhenEmpty = true;
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "ggggggggg" + ".pdf");

                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);


                GridView1.AllowPaging = false;
                GridView1.HeaderRow.ForeColor = System.Drawing.Color.Black;
                GridView1.FooterRow.ForeColor = System.Drawing.Color.Black;

                GridView1.HeaderRow.Style.Add("font-Color", "Black");
                GridView1.HeaderRow.Style.Add("font-size", "13px");
                GridView1.HeaderRow.Style.Add("text-decoration", "none");
                GridView1.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

                GridView1.Style.Add("font-Color", "Black");
                GridView1.Style.Add("text-decoration", "none");
                GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                GridView1.Style.Add("font-size", "11px");
                GridView1.ForeColor = System.Drawing.Color.Black;

                GridView1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
                pdfDoc.Open();

                htmlparser.Parse(sr);
                pdfDoc.Close();
                HttpContext.Current.Response.Write(pdfDoc);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }