22

有没有办法可以将我的 Silverlight DataGrid 数据导出到 excel 或 csv?

我在网上搜索但找不到任何示例!

非常感谢

4

9 回答 9

21

Silverlight 3 改变了这个问题的答案,因为它使用户能够在用户桌面上他们指定的位置创建文件。我修改了 DaniCE 提交的代码,为了便于阅读,将内容拆分为几种方法,并使用 Excel 应该识别的松散定义的 CSV 格式。

private void exportHistoryButton_Click(object sender, RoutedEventArgs e) 
{
    string data = ExportDataGrid(true, historyDataGrid);
    SaveFileDialog sfd = new SaveFileDialog()
    {
    DefaultExt = "csv",
    Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
    FilterIndex = 1
    };
    if (sfd.ShowDialog() == true)
    {
    using (Stream stream = sfd.OpenFile())
    {
        using (StreamWriter writer = new StreamWriter(stream)) {
        writer.Write(data);
        writer.Close();
        }
        stream.Close();
    }
    }
}

private string FormatCSVField(string data) {
    return String.Format("\"{0}\"",
        data.Replace("\"", "\"\"\"")
        .Replace("\n", "")
        .Replace("\r", "")
        );
}

public string ExportDataGrid(bool withHeaders, DataGrid grid)
{
    string colPath;
    System.Reflection.PropertyInfo propInfo;
    System.Windows.Data.Binding binding;
    System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
    System.Collections.IList source = (grid.ItemsSource as System.Collections.IList);
    if (source == null)
    return "";

    List<string> headers = new List<string>();
    grid.Columns.ToList().ForEach(col => {
    if (col is DataGridBoundColumn){
        headers.Add(FormatCSVField(col.Header.ToString()));
    }
    });
    strBuilder
    .Append(String.Join(",", headers.ToArray()))
    .Append("\r\n");

    foreach (Object data in source)
    {
    List<string> csvRow = new List<string>();
    foreach (DataGridColumn col in grid.Columns)
    {
        if (col is DataGridBoundColumn)
        {
        binding = (col as DataGridBoundColumn).Binding;
        colPath = binding.Path.Path;
        propInfo = data.GetType().GetProperty(colPath);
        if (propInfo != null)
        {
            csvRow.Add(FormatCSVField(propInfo.GetValue(data, null).ToString()));
        }
        }
    }
    strBuilder
        .Append(String.Join(",", csvRow.ToArray()))
        .Append("\r\n");
    }


    return strBuilder.ToString();
}
于 2009-08-03T11:58:26.147 回答
4

我使用剪贴板找到了这个。

要使代码通用,您可以修改第一个示例以读取列绑定并使用反射将它们应用于数据:

public String ExportDataGrid(DataGrid grid)
{
    string colPath;
    System.Reflection.PropertyInfo propInfo;
    System.Windows.Data.Binding binding;
    System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
    System.Collections.IList source = (grid.DataContext as System.Collections.IList);
    if (source == null)
        return "";

    foreach (Object data in source)
    {
        foreach (DataGridColumn col in datagrid.Columns)
        {
            if (col is DataGridBoundColumn)
            {
                binding = (col as DataGridBoundColumn).Binding;
                colPath = binding.Path.Path;
                propInfo = data.GetType().GetProperty(colPath);
                if (propInfo != null)
                {
                    strBuilder.Append(propInfo.GetValue(data, null).ToString());
                    strBuilder.Append(",");
                }                        
            }

        }
        strBuilder.Append("\r\n");
    }


    return strBuilder.ToString();
}

当然,它只有在绑定的路径是属性名称时才有效。对于更高级的路径,您必须将绑定应用于数据(我想这将是一个更好的解决方案,但我现在不确定如何执行此操作)。

于 2008-12-09T18:02:15.307 回答
2

我不认为 Silverlight 提供下载文件的方法。您可以在应用程序中添加一个调用 URL 的按钮,即http://www.mysite.com/generateexcelfile.aspx。将用于生成 Silverlight 应用程序中显示的数据的参数作为 Querystring 值包括在内,运行您的查询并使用您最喜欢的 Excel 文件生成组件即时生成文件。重定向到它,它将下载到用户的系统。

于 2008-11-20T04:27:46.660 回答
2

我知道这是一个旧帖子,但它确实帮助了我。我进行了一些编辑以使其与 silverlight 4、converts 和 excel 一起使用。我想要快速导出,所以我首先使用 CSV,然后用 excel 打开它。此代码适用于 silverlight web 和 oob 提升信任。在 Web 中不会在 excel 中打开。

     private static void OpenExcelFile(string Path)
    {
        dynamic excelApp;
        excelApp = AutomationFactory.CreateObject("Excel.Application");
        dynamic workbook = excelApp.workbooks;
        object oMissing = Missing.Value;

        workbook = excelApp.Workbooks.Open(Path,

           oMissing, oMissing, oMissing, oMissing, oMissing,

           oMissing, oMissing, oMissing, oMissing, oMissing,

           oMissing, oMissing, oMissing, oMissing);



        dynamic sheet = excelApp.ActiveSheet;


        // open the existing sheet


        sheet.Cells.EntireColumn.AutoFit();
        excelApp.Visible = true;
    }
    private static string FormatCSVField(string data)
    {
        return String.Format("\"{0}\"",
            data.Replace("\"", "\"\"\"")
            .Replace("\n", "")
            .Replace("\r", "")
            );
    }
   public  static string ExportDataGrid(DataGrid grid,string SaveFileName,bool AutoOpen)
    {
        string colPath;
        System.Reflection.PropertyInfo propInfo;
        System.Windows.Data.Binding binding;
        System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
       var source = grid.ItemsSource;

        if (source == null)
            return "";

        List<string> headers = new List<string>();
        grid.Columns.ToList().ForEach(col =>
        {
            if (col is DataGridBoundColumn)
            {
                headers.Add(FormatCSVField(col.Header.ToString()));
            }
        });
        strBuilder
        .Append(String.Join(",", headers.ToArray()))
        .Append("\r\n");

        foreach (var data in source)
        {
                List<string> csvRow = new List<string>();
                foreach (DataGridColumn col in grid.Columns)
                {
                    if (col is DataGridBoundColumn)
                    {
                        binding = (col as DataGridBoundColumn).Binding;
                        colPath = binding.Path.Path;

                        propInfo = data.GetType().GetProperty(colPath);
                        if (propInfo != null)
                        {
                            string valueConverted = "";
                            if (binding.Converter.GetType().ToString() != "System.Windows.Controls.DataGridValueConverter")
                                valueConverted = binding.Converter.Convert(propInfo.GetValue(data, null), typeof(System.String), binding.ConverterParameter, System.Globalization.CultureInfo.CurrentCulture).ToString();
                            else
                                valueConverted = FormatCSVField(propInfo.GetValue(data, null) == null ? "" : propInfo.GetValue(data, null).ToString());

                            csvRow.Add(valueConverted.ToString());
                        }
                    }
                }
                strBuilder
                    .Append(String.Join(",", csvRow.ToArray()))
                    .Append("\r\n");
            }

        if (AutomationFactory.IsAvailable)
        {
            var sampleFile = "\\" + SaveFileName;
            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            path += "\\Pement";


            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            else
            {
                var files = System.IO.Directory.EnumerateFiles(path);
                foreach (var item in files)
                {
                    try
                    {
                        System.IO.File.Delete(item);
                    }
                    catch { }
                }
            }

            StreamWriter sw = File.CreateText(path + sampleFile);
            sw.WriteLine(strBuilder.ToString());
            sw.Close();

            if (AutoOpen)
                OpenExcelFile(path + sampleFile, true, true);
        }
        else
        {
            SaveFileDialog sfd = new SaveFileDialog()
            {
                DefaultExt = "csv",
                Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                FilterIndex = 1
            };
            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(strBuilder.ToString());
                        writer.Close();
                    }
                    stream.Close();
                }
            } 
        }
        return strBuilder.ToString();
    }
于 2010-11-20T21:30:25.713 回答
1

我想说的是,您可以使用 a 添加导出按钮ControlTemplate,然后遍历 中的每个项目DataSource,然后使用 in 中的每一列Columns使用该方法获取每个单元格的内容GetCellContent,或者使用 DataGridColumn 的绑定信息来获取适当的单元格值。然后,您可以获取此内容的显示值并将其写入您的报告。

就像是...

foreach (YourType item in grid.DataSource)
{
   foreach (DataGridColumn column in grid.Columns)
   {
      FrameworkElement cellContent = column.GetCellContent(item);

      // Now, determine the type of cell content and act accordingly.
      TextBlock block = cellContent as TextBlock;
      if (block != null)
      {
         // Report text value...
      }

      // ...etc...

   }
}

或者使用DaniCE描述的绑定信息。

于 2009-02-04T15:08:14.257 回答
1

这些解决方案对我不起作用,因此我将它们修改为有效的解决方案。(我的解决方案不需要在字段周围加上引号,所以我省略了 FormatCSVField 函数)

    public void SaveAs(string csvPath)
    {
        string data = ExportDataGrid(true, _flexGrid);
        StreamWriter sw = new StreamWriter(csvPath, false, Encoding.UTF8);
        sw.Write(data);
        sw.Close();
    }

    public string ExportDataGrid(bool withHeaders, Microsoft.Windows.Controls.DataGrid grid) 
    {
        System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
        System.Collections.IEnumerable source = (grid.ItemsSource as System.Collections.IEnumerable);

        if (source == null) return "";

        List<string> headers = new List<string>();

        grid.Columns.ToList().ForEach(col =>
        {
            if (col is Microsoft.Windows.Controls.DataGridBoundColumn)
            {
                headers.Add(col.Header.ToString());
            }
        });

        strBuilder.Append(String.Join(",", headers.ToArray())).Append("\r\n");
        foreach (Object data in source)
        {
            System.Data.DataRowView d = (System.Data.DataRowView)data;
            strBuilder.Append(String.Join(",", d.Row.ItemArray)).Append("\r\n");
        }

        return strBuilder.ToString();
    }
于 2011-03-07T16:43:10.410 回答
1

检查 Ryan 的解决方案。看起来不错,但不能保证,因为我刚刚找到它。Ryan 做了上面要求的 DLL。

http://www.rshelby.com/post/exporting-data-from-silverilght-datagrid-to-excel.aspx

上面 Dakota 的解决方案中的 David 看起来更容易实现,并且总是重定向到经典的 asp.net 页面,上面有一个数据网格,内容类型设置为 excel,但是这样你就有更多的代码需要支持可能不适用于离线的浏览器解决方案(实际上不起作用)。

无论如何,这是一项常见的任务。我希望这里或微软的一些人想出一个 Mort 即插即用的解决方案:)

我从 Silverlight 数据网格中找到了 CVS 导出的扩展方法-

http://www.codeproject.com/KB/silverlight/SilverlightDataGridExport.aspx

它具有即插即用的潜力,但我不得不稍微调整一下以使其与具有项目数据源的数据网格一起使用(请参阅帖子中的评论)。比我更聪明、更有经验的人应该能够调整到完美。检查一下,它应该让你接近你所需要的。

于 2009-12-02T22:16:19.420 回答
0

我需要做同样的事情。我使用了 t3rse 的实现,但必须进行一些更改。我没有足够的声誉来评论他的回答,所以我会在这里列出:

  • 对于表示 propInfo.GetValue(data, null).ToString() 的行,我在调用 ToString() 之前检查了 GetValue 返回的值是否为 Null。

  • 在方法 FormatCSVField() 中,它用三个双引号替换了一个双引号。它应该只用两个双引号替换它。

  • 该实现仅使用 DataGridBoundColumn 类型的列,而忽略其他列。我有一些不是我想要包含的 DataGridBoundColumn 的列,因此我使用 col.SortMemberPath 获得了这些列的数据源的属性名称。

于 2013-10-23T15:59:14.453 回答
0

这是一个对我有用的好方法 http://forums.silverlight.net/forums/p/179321/404357.aspx

于 2010-05-24T12:39:52.217 回答