【问题标题】:How to limit text length in grid column while not limiting the same text in Excel Export如何限制网格列中的文本长度而不限制 Excel 导出中的相同文本
【发布时间】:2015-06-13 13:21:14
【问题描述】:

我目前正在构建一个在 Telerik RadGrid 中包含大量数据的 asp.net 应用程序。我的网格中的许多列包含大量文本。为了限制这些列中文本的大小(并保留网格的格式),我在以下代码段中截断了 ItemDataBound(...) 方法中的每个字段:

if (item["Title"].Text.Length > 21)
    item["Title"].Text = item["Title"].Text.Substring(0, 21);

if (item["Investigation_Results"].Text.Length > 30)
    item["Investigation_Results"].Text = item["Investigation_Results"].Text.Substring(0, 30);

if (item["Resolution_Steps"].Text.Length > 30)
    item["Resolution_Steps"].Text = item["Resolution_Steps"].Text.Substring(0, 30);

问题是当使用 RadGrid 的 Excel 导出功能时,被截断的字段在导出的 csv 中也会被截断。

问:如何在网格视图中截断数据,但在导出到 Excel 时完整?

其他信息:

在此按钮单击事件上调用导出:

protected void imgbtnexcel_Click(object sender, ImageClickEventArgs e)
{
    ConfigureExport();
    RadGridActionItem.MasterTableView.ExportToExcel();
}

protected void ConfigureExport()
{
    RadGridActionItem.ExportSettings.ExportOnlyData = true;
    RadGridActionItem.ExportSettings.IgnorePaging = true;
    RadGridActionItem.ExportSettings.OpenInNewWindow = true;
}

【问题讨论】:

  • 在 FarPoint 中,我可以做一个从文本继承的自定义单元格类型,然后在不实际更改值的情况下覆盖它“显示”值的方式。

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


【解决方案1】:

一种方法是处理 NeedDataSource 事件。在这种情况下,根据您是否正在导出,为数据源提供完整或截断的文本。您尚未提供此代码,但您应该能够判断您是否使用 GridExporting 事件、按钮单击事件或类似事件进行导出。

【讨论】:

    【解决方案2】:

    radgrid 的 excel 导出会根据绑定到网格的数据生成一个工作表。

    如果您想在导出中显示全文而不是在屏幕上显示,则需要使用 css 和/或 javascript 隐藏文本。

    【讨论】:

      【解决方案3】:

      我能够通过在 ItemDataBound(...) 中的“截断代码”周围放置一个条件语句(导出/不导出)来获得我想要的功能。然后我使用了一个全局布尔变量,并在导出按钮单击事件中将其标记为 true。

      static bool exportBool;
      

      在 ItemDataBound(...) 中:

      if (ActionItem.exportBool != true) // MK CHANGE
      {
          if (item["Title"].Text.Length > 21)
             item["Title"].Text = item["Title"].Text.Substring(0, 21) + "...";
      
          if (item["Investigation_Results"].Text.Length > 30)
             item["Investigation_Results"].Text = item["Investigation_Results"].Text.Substring(0, 30) + "...";
      
          if (item["Resolution_Steps"].Text.Length > 30)
             item["Resolution_Steps"].Text = item["Resolution_Steps"].Text.Substring(0, 30) + "...";
      }
      

      导出按钮点击事件:

      protected void imgbtnexcel_Click(object sender, ImageClickEventArgs e)
      {
          ActionItem.exportBool = true;
          BindGrid();
          ConfigureExport();
          RadGridActionItem.MasterTableView.ExportToExcel();
      }
      

      请注意,布尔值在 Page_Load() 中设置为 false。

      谢谢!

      【讨论】:

        【解决方案4】:

        你能在 RadGrid 中设置 CSS 样式吗?是否可以将所有文本都放在那里,但通过 CSS 将其截断?

        在普通的table 中,您可以执行以下操作:

        在表格本身上设置此样式:

        table { table-layout: fixed; width: 100%; }
        

        在实际单元格中,您需要设置一个样式(以下示例中的“标题”):

        td.title { white-space:nowrap; overflow: hidden; width: 200px; }
        

        数据仍然存在,只是被截断了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-03
          • 2016-04-28
          • 1970-01-01
          • 2014-04-30
          • 2015-09-13
          • 1970-01-01
          相关资源
          最近更新 更多