使用silverlight 4和c#创建CSV下载 [英] Create a CSV download using silverlight 4 and c#
问题描述
我很难找到一个示例或代码,以便能够在silverlight中创建CSV或文本文件作为可下载的链接。
I'm struggling to find an example or code to be able to create CSV or text file in silverlight as a downloadable link.
我在ASP.net中做过这个,但是不能找出使用Silverlight的方法。我在旋转我的轮子?或者我应该创建一个ASP页面?有没有办法在c#中这样做?
I've done this in ASP.net but can't figure out a way using Silverlight. Am I spinning my wheels? Or should I just create an ASP page? Is there a way of doing this in c#?
我想这样做是正确的方式,而不是一些黑客工作,将欣赏任何反馈和建议。
I'd like to do this the right way and not some hack job and will appreciate any feedback and advice.
在ASP中,我将使用:
In ASP I would of used:
Response.ContentType = "text/csv"
Response.AddHeader "Content-disposition", "attachment;filename=""EPIC0B00.CSV"""
Response.write....
推荐答案
我能够解决与上面非常类似的代码,只包括所需的引用,所以没有假设,加上这是一个实际的工作示例。
I was able to solve with very similar code as above, just including required references so there is no assumptions made, plus this is an actual working example.
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 4和c#创建CSV下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!