C# Silverlight DataGrid:导出到excel或csv

C# Silverlight DataGrid:导出到excel或csv,c#,asp.net,silverlight,datagrid,C#,Asp.net,Silverlight,Datagrid,是否有办法将Silverlight DataGrid数据导出到excel或csv 我在网上搜索过,但找不到任何例子 非常感谢我不认为Silverlight提供了下载文件的方法。你可以在你的应用程序中添加一个调用URL的按钮,即。将用于生成Silverlight应用程序中显示的数据的参数包含为Querystring值,运行查询并使用您喜爱的Excel文件生成组件动态生成文件。重定向到它,它将下载到用户系统。我使用剪贴板找到了它 要使代码通用,可以修改第一个示例以读取列绑定,并使用反射将其应用于数据

是否有办法将Silverlight DataGrid数据导出到excel或csv

我在网上搜索过,但找不到任何例子


非常感谢

我不认为Silverlight提供了下载文件的方法。你可以在你的应用程序中添加一个调用URL的按钮,即。将用于生成Silverlight应用程序中显示的数据的参数包含为Querystring值,运行查询并使用您喜爱的Excel文件生成组件动态生成文件。重定向到它,它将下载到用户系统。

我使用剪贴板找到了它

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

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();
}

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

我想您可以使用
控制模板添加导出按钮,然后在
数据源中迭代每个项,然后使用
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...

   }
}

或者使用.Silverlight 3所述的绑定信息更改此问题的答案,因为它允许用户在其指定的位置在用户桌面上创建文件。我修改了DaniCE提交的代码,为了可读性,我将代码分成了几个方法,并使用了一种松散定义的CSV格式,Excel应该能够识别这种格式

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();
}
private void exporthistory按钮\u单击(对象发送方,路由目标)
{
字符串数据=ExportDataGrid(true,historyDataGrid);
SaveFileDialog sfd=新建SaveFileDialog()
{
DefaultExt=“csv”,
Filter=“CSV文件(*.CSV)|*.CSV |所有文件(*.*)|*.*”,
FilterIndex=1
};
if(sfd.ShowDialog()==true)
{
使用(Stream=sfd.OpenFile())
{
使用(StreamWriter=新StreamWriter(流)){
writer.Write(数据);
writer.Close();
}
stream.Close();
}
}
}
专用字符串格式CsvField(字符串数据){
返回String.Format(“\”{0}\”,
数据。替换(“\”,“\”\”)
.Replace(“\n”和“”)
.Replace(“\r”,”)
);
}
公共字符串ExportDataGrid(bool with headers,DataGrid)
{
字符串路径;
System.Reflection.PropertyInfo propInfo;
System.Windows.Data.Binding绑定;
System.Text.StringBuilder strBuilder=new System.Text.StringBuilder();
System.Collections.IList source=(grid.ItemsSource作为System.Collections.IList);
if(source==null)
返回“”;
列表标题=新列表();
grid.Columns.ToList().ForEach(col=>{
if(列为DataGridBoundColumn){
Add(FormatCSVField(col.Header.ToString());
}
});
strBuilder
.Append(String.Join(“,”,headers.ToArray()))
.Append(“\r\n”);
foreach(源中的对象数据)
{
List csvRow=新列表();
foreach(grid.Columns中的DataGridColumn列)
{
if(列为DataGridBoundColumn)
{
绑定=(列作为DataGridBoundColumn)。绑定;
colPath=binding.Path.Path;
propInfo=data.GetType().GetProperty(colPath);
if(propInfo!=null)
{
Add(FormatCSVField(propInfo.GetValue(data,null.ToString());
}
}
}
strBuilder
.Append(String.Join(“,”,csvRow.ToArray()))
.Append(“\r\n”);
}
返回strBuilder.ToString();
}

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

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

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

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


它具有即插即用的潜力,但我不得不稍微尝试一下,使它能够与带有项数据源的数据网格一起工作(请参阅文章中的评论)。一个比我更聪明、更有经验的人应该能够调整到完美。看看吧,它会让你接近你需要的东西。

这里有一个很好的方法,对我很有效

我知道这是一篇老文章,但它确实帮助了我。我对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();
    }
private静态void OpenExcelFile(字符串路径)
{
动态应用程序;
excelApp=AutomationFactory.CreateObject(“Excel.Application”);
动态工作簿=excelApp.workbooks;
对象删除=缺少.Value;
工作簿=excelApp.Workbooks.Open(路径,
省略,省略,省略,省略,省略,省略,
省略,省略,省略,省略,省略,省略,
省略,省略,省略,省略);
动态工作表=excelApp.ActiveSheet;
//打开现有图纸
sheet.Cells.entireclumn.AutoFit();
excelApp.Visible=true;
}
专用静态字符串格式CsvField(字符串数据)
{
返回String.Format(“\”{0}\”,
数据。替换(“\”,“\”\”\
    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();
    }