【问题标题】:Exporting Telerik RadGrid data in zip format以 zip 格式导出 Telerik RadGrid 数据
【发布时间】:2014-02-18 20:15:15
【问题描述】:

我正在尝试以 zip 格式导出 RadGrid 数据。为此,首先我以 Excel 格式导出数据,然后尝试对其进行压缩。但是 RadGrid.MasterTableView.ExportToExcel() 方法的返回类型是 void 所以我不能存储这个 Excel 工作表的结果以便将它作为输入传递给 ZipPackage.

【问题讨论】:

  • 你不能从导出方法创建一个流然后使用telerik自己的zip控件抓取流并压缩它吗?
  • 您是说从 ExporttoExcel 方法创建流,但我无法从 ExporttoExcel 方法创建流

标签: c# asp.net excel telerik radgrid


【解决方案1】:

据我所知,Radgrid 没有 Zip 导出方法。 您可以做的是:在 excel 中导出文件,然后压缩 excel 文件(或您导出的任何其他类型)。 我会这样做:

 protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
    {
        //column you may like to excluded from the export process
        if (col.UniqueName.Contains("EditCommandColumn") ||
            col.UniqueName.Contains("attachments") ||
            col.UniqueName.Contains("Upload") || col.UniqueName.Contains("Delete_col"))
        {
            col.Display = false;
        }
        else
        {
            col.Display = true;
        }

    }
    foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
        item.Visible = false;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.ExportSettings.FileName = "YourDesiredFileName";
    RadGrid1.MasterTableView.ExportToExcel(); //this will be exported to the folder Download
    Zip();
}

private void Zip()
{
    //here your method with your preferred library to zip the file exported from the radgrid
    //save it where you need it, rename it as you like it and delete the original excel file
}

【讨论】:

  • 我知道了 Felice,但我想要做的是避免中间的 excel 文件。如果我们使用 RadGrid1.MasterTableView.ExportToExcel();将显示一个弹出窗口,提示您打开保存或取消。我不想保存并再次阅读此文件以对其执行 zip 操作。我的最终输出应该是 zip 文件,不应该有任何其他中间文件。
  • 我理解,但这就是为什么我通过按钮而不是使用 radgrid 的嵌入式功能来执行此操作。上面的代码在我的电脑上什么都没有弹出,中间文件可以在zip方法结束时删除。祝你好运!
  • 好主意 Felice - 虽然我认为 ExportToExcel() 会强制弹出?
  • @TheTiger 它不在我的 2013 年第三季度版本中。谢谢
  • 我从来没有看到保存确认弹出窗口。事实上, FileName 似乎被忽略了。不知道我做错了什么,但 ExportToExcel 和其他似乎在网络环境中行为不端。
【解决方案2】:

我相信您可以尝试挂接到 OnGridExporting 命令,然后执行类似的操作

   using (FileStream fs = File.Create("path you want to save the file")) {
            Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
            //save it on the server
            fs.Write(info, 0, info.Length);
        }

把所有这些都放在一个 try 中,catch 来观察文件权限问题和文件已经存在的问题。

【讨论】:

  • 你真的得到了导出命令(无论是什么类型)来写入文件吗?我无法让它识别文件名设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2012-06-16
  • 2019-05-14
相关资源
最近更新 更多