将选中的Gridview行导出为pdf [英] Export checked Gridview rows to pdf

查看:72
本文介绍了将选中的Gridview行导出为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于将已检查的gridview行导出到excel的代码,但无法修改代码以便以相同的方式导出pdf文件。

这是导出excel的代码。我一直在尝试修改,但pdf没有像excel那样显示girdview中的所有数据。任何建议!?



I have code for exporting checked gridview rows to excel, but cannot modify the code to work the same way for exporting pdf file.
This is the code for export excel. I've been trying to modify but the pdf is not displaying all the data from the girdview like in excel. Any suggestions !?

protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;

            Response.AddHeader("content-disposition",
             "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            GridView1.AllowPaging = false;
            GridView1.DataBind();

            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
            GridView1.HeaderRow.Cells[0].Visible = false;
            GridView1.HeaderRow.Cells[4].Visible = false;
            GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#285D8B");
            GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#285D8B");
            GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#285D8B");
            GridView1.HeaderRow.Cells[5].Style.Add("background-color", "#285D8B");
            if (ViewState["CheckBoxArray"] != null)
            {
                ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
                string checkAllIndex = "chkAll-" + GridView1.PageIndex;
                int rowIdx = 0;
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow row = GridView1.Rows[i];
                    row.Visible = false;

                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        if (CheckBoxArray.IndexOf(i + 1) != -1)
                        {
                            row.Visible = true;
                            row.BackColor = System.Drawing.Color.White;
                            row.Cells[0].Visible = false;
                            row.Cells[4].Visible = false;
                            row.Attributes.Add("class", "textmode");
                            if (rowIdx % 2 != 0)
                            {
                                row.Cells[1].Style.Add("background-color", "#cedae4");
                                row.Cells[2].Style.Add("background-color", "#cedae4");
                                row.Cells[3].Style.Add("background-color", "#cedae4");
                                row.Cells[5].Style.Add("background-color", "#cedae4");
                            }
                            rowIdx++;
                        }
                    }
                }
            }
            GridView1.RenderControl(hw);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.End();
        }

推荐答案

试试这个



try this

Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=GridViewExport.pdf");


这篇关于将选中的Gridview行导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆