【问题标题】:Exporting gridview to .xls将网格视图导出到 .xls
【发布时间】:2012-04-20 05:51:33
【问题描述】:

我正在开发一个带有 gridview 控件的网页。我需要将数据传输为 .xls 格式。我能够创建 .xls 文件并传输数据,但问题是我需要在 excel 表的背景中显示网格单元。现在,它只显示没有网格单元的空白背景。否则,gridview 会被很好地转移。由于这个问题,打印 .xls 文件是个问题。具有更多列的表格不会压缩,而是打印 2-3 页。我的代码如下:

public static void ExportToXLS(string fileName, GridView gv,string companyName,string reportTitle , string period)
    {
        //For writing to XLS file
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {

                Table tableReport = new Table();
                tableReport.GridLines = gv.GridLines;

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    ReportList.PrepareControlForExport(gv.HeaderRow);
                    tableReport.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    ReportList.PrepareControlForExport(row);
                    tableReport.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    ReportList.PrepareControlForExport(gv.FooterRow);
                    tableReport.Rows.Add(gv.FooterRow);
                }

                //Takes value of company name
                System.Web.UI.WebControls.Label labelCompany = new System.Web.UI.WebControls.Label();
                labelCompany.Text = companyName;
                labelCompany.Font.Bold = true;

                //Takes value of report title
                System.Web.UI.WebControls.Label labelReport = new System.Web.UI.WebControls.Label();
                labelReport.Text = reportTitle;
                labelReport.Font.Bold = true;

                //Takes value of report period
                System.Web.UI.WebControls.Label labelPeriod = new System.Web.UI.WebControls.Label();
                labelPeriod.Text = period;

                //  render the htmlwriter into the response
                htw.Write("<center>");
                labelCompany.RenderControl(htw);
                htw.Write("<br/>");
                labelReport.RenderControl(htw);
                htw.Write("<br/>");
                labelPeriod.RenderControl(htw);
                htw.Write("</center>");
                htw.Write("<br/>");
                tableReport.RenderControl(htw);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

有什么建议吗?

【问题讨论】:

  • 您想在 xls 中显示网格线吗?
  • 查看codesimplified.com的GridView Export to Excel
  • 请注意接受答案。它会让你很快得到答案。
  • 你好,bhavna,你得到那份工作了吗,因为我也面临同样的问题

标签: c# asp.net excel


【解决方案1】:

参考:Export grid

试试这个:

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);

        grdExport.AllowPaging = false;

        oMailing.GetData(out ODs);

        grdExport.DataSource = ODs;

        grdExport.DataBind();

        //Change the Header Row back to white color

        grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Apply style to Individual Cells

        grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green");

        grdExport.RenderControl(hw);

        //style to format numbers to string

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";

        Response.Write(style);

        Response.Output.Write(sw.ToString());

        Response.Flush();

        Response.End();

    }

【讨论】:

  • 嗨.. 有什么方法可以在 excel 中包装标题文本??。
【解决方案2】:

所有答案都会让你得到你想要的——你只是缺少了格式部分。

归根结底,您真正创建的是 Excel 可以读取的 HTML 文件(带有 HTML 表格)。 @BhaskarreddyMule 中的 RESPONSE 标头“强制”客户端将文件视为“xls”文件,如果它运行 Excel 并打开它(但底线是它的 不是真正的“本机” Excel文件

现在已经不碍事了,用 HTML 来思考。像在 HTML 中一样设置列、行和文本内容的样式。这就是您控制格式的方式(即老派“nowrap”以防止包装单元格内容、字体大小等)。

我已经有一段时间没有这样做了(当我需要这样做时,我已经转向 Excel XML 和 VB.Net XML 文字)所以我不确定您对 @987654322 的控制程度如何@...或者如果您必须更进一步“老派”并从头开始构建 HTML 表字符串.....

【讨论】:

  • 我被困在这一点上。您如何使用 Excel XML?我可以将它与 asp.net 框架一起使用吗??
  • RenderControl 将是您的首选 - 数据已经在页面中(无需进行其他数据调用)。我对此的评论是免责声明,让您了解RenderControl 可以走多远(因为我个人无法提供更多有用的信息)。也可以查看@NiranjanKala 的答案。是的,都是 .Net、C# 或 VB.Net。只是在 VB.net 中,XML Literals goodness 是它的天堂(视频中的魔法大约是 5:00)。如果您这样做,请注意上面的警告,并且您非常希望每个人都拥有 Excel。
【解决方案3】:

首先参考这个:How to export nested gridview to excel/word with gridlines on the child grid,希望它能帮助你解决你的问题。

参考:How to export gridview to excel in a console type application?

您可以参考以下链接中的“导出到 Excel”控件。这是一个自定义控件。希望对你有帮助。

http://exporttoexcel.codeplex.com/

Styling exported grid:

 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);

            grdExport.AllowPaging = false;
            oMailing.GetData(out ODs);
            grdExport.DataSource = ODs;
            grdExport.DataBind();

            //Change the Header Row back to white color
            grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF");

            //Apply style to Individual Cells
            grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green");

            for (int i = 0; i < grdExport.Rows.Count; i++)
            {
                GridViewRow row = grdExport.Rows;

                //Change Color back to white
                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row
                row.Attributes.Add("class", "textmode");

                //Apply style to Individual Cells of Alternating Row
                if (i % 2 != 0)
                {
                    row.Cells[0].Style.Add("background-color", "#C2D69B");
                    row.Cells[1].Style.Add("background-color", "#C2D69B");
                    row.Cells[2].Style.Add("background-color", "#C2D69B");
                    row.Cells[3].Style.Add("background-color", "#C2D69B");
                    row.Cells[4].Style.Add("background-color", "#C2D69B");
                }

            }
            grdExport.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }

【讨论】:

    【解决方案4】:
      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);
    

    【讨论】:

      【解决方案5】:

      我试过导出到excel,看来你首先需要创建一个使用部分Excel = using Microsoft.Office.Interop.Excel,当你点击按钮时,只需创建一个具有工作簿和工作表属性的excel类,然后使用@987654322的gridview属性@,您可以将gridview中的每个值动态存储到工作表中,然后将其写入FILE....

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多