Silverlight DataGrid:导出到excel或csv [英] Silverlight DataGrid: Export to excel or csv

查看:140
本文介绍了Silverlight DataGrid:导出到excel或csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



感谢很多

解决方案

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文件(* .csv)| * .csv |所有文件(*。 *)| *。*,
FilterIndex = 1
};
if(sfd.ShowDialog()== true)
{
using(Stream stream = sfd.OpenFile())
{
using(StreamWriter writer = new StreamWriter(stream)){
writer.Write(data);
writer.Close();
}
stream.Close();
}
}
}

私有字符串FormatCSVField(字符串数据){
return String.Format(\{0} \
data.Replace(\,\\\)
.Replace(\\\
,)
.Replace (\r,)
);
}

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

列表< 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\\\
);

foreach(源中的对象数据)
{
列表< 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( \\\\);
}


返回strBuilder.ToString();
}


Is there a way I can export my Silverlight DataGrid data to excel or csv?

I searched the web but can't find any examples!

Thanks a lot

解决方案

Silverlight 3 changes the answer to this question because it gives the ability of the user to create a file on the user's desktop in a location that they specify. I adapted the code submitted by DaniCE, split things into a few methods for readability and am using a loosely defined CSV format that Excel should recognize.

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

这篇关于Silverlight DataGrid:导出到excel或csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆