我正在努力寻找能够在 Silverlight 中创建 CSV 或文本文件作为可下载链接的示例或代码。
我已经在 ASP.net 中完成了此操作,但无法找到使用 Silverlight 的方法。我在转动我的轮子吗?或者我应该只创建一个 ASP 页面?有没有办法在 c# 中做到这一点?
我想以正确的方式做到这一点,而不是一些黑客工作,并会感谢任何反馈和建议。
在 ASP 中我会使用:
Response.ContentType = "text/csv"
Response.AddHeader "Content-disposition", "attachment;filename=""EPIC0B00.CSV"""
Response.write....
最佳答案
我能够使用与上面非常相似的代码来解决,只包含所需的引用,因此没有做出任何假设,而且这是一个实际的工作示例。
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
....
private void btnSave_Click(object sender, RoutedEventArgs e)
{
string data = ExportData(); // This is where the data is built
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); //Write the data :)
writer.Close();
}
stream.Close();
}
}
}
private string ExportData()
{
return "!this is the exported text";
}
关于silverlight - 使用 silverlight 4 和 c# 创建一个 CSV 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5037030/