我想将一个元素(可能是 gridview、List 等)导出为 .xls 文件,并通过 Silverlight 4.0 中的给定字符串设置 WorkSheet。我怎么解决这个问题?
以下是使用 RadGridView API 的导出功能...
public RadGridView Grid { get; private set; }
void ExportWithoutDetail()
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = ReadFilter();
saveDialog.FilterIndex = 4;
if (saveDialog.ShowDialog() == true)
{
SetAllColumnVisibile();
Stream stream = null;
try
{
using (stream = saveDialog.OpenFile())
{
GridViewExportOptions opt = CreateExportOptions();
switch (saveDialog.FilterIndex)
{
case 1: //*.txt
opt.Format = ExportFormat.Text;
break;
case 2: //*.html
opt.Format = ExportFormat.Html;
break;
case 3: //*.csv
opt.Format = ExportFormat.Csv;
break;
case 4: //*.xls
opt.Format = ExportFormat.ExcelML;
break;
case 5: //*.xml
opt.Format = ExportFormat.ExcelML;
break;
}
Grid.Export(stream, opt);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (stream != null)
stream.Close();
}
}
}