【问题标题】:How to Display the Data have more than two spaces in Repeater?如何显示Repeater中有两个以上空格的数据?
【发布时间】:2012-09-12 07:22:16
【问题描述】:

我将数据库中的数据放入我的数据集中,然后我绑定到我的转发器控件,但它一直显示为“Prince Antony G”。它消除了空格。
我还将这些数据导出到 excel 中,在 excel 中也删除了空格。
我需要数据库中的任何显示数据。任何想法来解决这个问题。

在堆栈溢出问题框中还删除了两个单词之间的更多空格

我的 Excel 编码:

    DataTable dt = new DataTable("Report");
    DataRow dr;
    dt.Columns.Add("Name");   

    for (int i = 0; i < dt_Status.Rows.Count; i++)
    {
        dr = dt.NewRow();

        dr["Name"] ="<pre>"+ dt_Status.Rows[i]["NAME"].ToString() +"</pre>";
        dt.Rows.Add(dr);
    }

    HttpResponse response = HttpContext.Current.Response;

    // first let's clean up the response.object
    response.Clear();
    response.Charset = "";

    // set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader("Content-Disposition", "attachment;filename=Report.xls");

    // create a string writer
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            // instantiate a datagrid
            DataGrid dg = new DataGrid();
            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
    }

更新我的代码后,我得到的 excel 输出为:

我需要在单个单元格中显示。

【问题讨论】:

  • @Star:为什么要删除最后一行?
  • 我没有。当你编辑它时,你可以看到你 Q 的旧版本

标签: c# asp.net excel repeater


【解决方案1】:

html 不能识别多个空格。

你把你的数据放在&lt;pre&gt;&lt;/pre&gt;标签之间,说这是按格式设置的。

或者您可以将空格替换为&amp;nbsp;。这是non breaking space character

空格用于格式化你的 html 代码,因此如果你有多个空格,它会被包装成一个。

【讨论】:

  •  标签是在中继器控制中绑定的解决方案之一。谢谢.. 但是对于 excel 该怎么办?
  • 要写入 excel,请使用从数据库中获取的值。
  • 是的,我直接从数据库中获取
  • 你能贴出你的excel编写代码吗?如果您是从数据库中写入值,则应该没有问题
  • 您正在使用RenderControl 编写excel。这只会使用 DataGrid 的 html 输出。所以一旦 Grid 渲染的输出被上面的解决方案修复了,这也将解决
【解决方案2】:

一个简单好用的东西:

string value = String.Join(" ", "Prince Antony G".Split(new char[] {' '}, 
StringSplitOptions.RemoveEmptyEntries));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多