【问题标题】:Export Excel With GridLines使用网格线导出 Excel
【发布时间】:2013-11-08 03:56:03
【问题描述】:

我在下面提到了将数据导出到 excel 的代码

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

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

        tb.GridLines = gv.GridLines;

        tb.BackColor = System.Drawing.Color.Transparent;
        TableRow tr1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Controls.Add(BalanceSheetListControl2);
        tr1.Cells.Add(cell1);
        tb.Rows.Add(tr1);
        tb.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; backgroud-color:transparent !important; } </style>";
        Response.Write(style);


        System.IO.StringReader writer = new System.IO.StringReader(sw.ToString());
       string s=string.Empty;
       string readString = string.Empty;
       bool StartFlag = false;
       StringBuilder build = new StringBuilder();
        while((readString=writer.ReadLine())!=null)
        {
           // readString = writer.ReadLine().Trim();
            s = readString;
            Regex search_string = new Regex("<div.*?>([^<]+)</div>");
            Match match = search_string.Match(s);
            string section = match.Groups[1].Value.ToString();
            if (section.Trim()=="-")
            {
                s = s.Replace(">-<", "><div align='right'>-</div><");
               // s = s.Replace("-", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;");
            }

            if (readString.Trim().Contains("main") || readString.Trim().Contains("column c"))
            {

                s=s+"</td><td>";
            }

            build.Append(s);

        }

        Response.Output.Write(build.ToString());
        Response.Flush();
        Response.End();

我必须在 Excel 中显示网格线,因为我使用了以下代码

        GridView gv=new GridView();
        tb.GridLines = gv.GridLines;

使用上面的代码excel后只会生成垂直网格线,我也需要水平线,请谁能帮我实现这个? 提前致谢

作为参考,我附上了我当前 Excel 的图片

【问题讨论】:

    标签: c# asp.net export-to-excel


    【解决方案1】:

    检查此代码是否适合我。

     public static void ExportWithBorder(string fileName, GridView gv)
       {
           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))
               {
                   //  Create a form to contain the grid
                   Table table = new Table();
    
                   //  add the header row to the table
                   if (gv.HeaderRow != null)
                   {
                       GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                       gv.HeaderRow.Style.Add("border", "solid 1px #c1d8f1");
                       table.Rows.Add(gv.HeaderRow);
                   }
    
                   //  add each of the data rows to the table
                   foreach (GridViewRow row in gv.Rows)
                   {
                       GridViewExportUtil.PrepareControlForExport(row);
                       row.Style.Add("border", "solid 1px #c1d8f1");
                       table.Rows.Add(row);
                   }
    
                   //  add the footer row to the table
                   if (gv.FooterRow != null)
                   {
                       GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                       gv.FooterRow.Style.Add("border", "solid 1px #c1d8f1");
                       table.Rows.Add(gv.FooterRow);
                   }
    
                   //  render the table into the htmlwriter                    
                   table.RenderControl(htw);
    
                   //  render the htmlwriter into the response
                   HttpContext.Current.Response.Write(sw.ToString());
                   HttpContext.Current.Response.End();
               }
           }
       }
    
        /// <summary>
        /// Replace any of the contained controls with literals
        /// </summary>
        /// <param name="control"></param>
        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : ""));
                }
                else if (current is TextBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as TextBox).Text));
                }
                if (current.HasControls())
                {
                    GridViewExportUtil.PrepareControlForExport(current);
                }
            }
        }
    

    【讨论】:

    • @DhavalPatel 不要忘记接受正确的答案,这样对他人也有帮助
    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 2023-03-30
    相关资源
    最近更新 更多