【问题标题】:Editing gridview excel export编辑gridview excel导出
【发布时间】:2016-04-29 20:09:47
【问题描述】:

我目前实现了一种方法,它可以将作为参数传递的任何 RadGridView 导出到 excel 中。它完全可以导出,我想通过在 excel 文件的第一行添加标题然后在该行下方附加 RadGridView 来增强它。请问是否有人知道我应该怎么做?

public static void Export(RadGridView grid)
{
    const string extension = "xls";

    var dialog = new SaveFileDialog
    {
        DefaultExt = extension,
        Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
        FilterIndex = 1
    };

    if (dialog.ShowDialog() != true)
    {
        return;
    }

    using (var stream = dialog.OpenFile())
    {
        var exportOptions = new GridViewExportOptions
        {
            Format = ExportFormat.ExcelML,
            ShowColumnHeaders = true,
            ShowColumnFooters = true,
            ShowGroupFooters = false,
        };
        exportOptions.Items = (IEnumerable)grid.ItemsSource;
        grid.Export(stream, exportOptions);
    }
}

【问题讨论】:

    标签: c# wpf mvvm telerik


    【解决方案1】:

    如果有人感兴趣。这是我使用的解决方案,

    public static void ExportWithHeader(RadGridView grid, string header)
    {
        try
        {
            string extension = "xls";
            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = extension,
                Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
                FilterIndex = 1,
                FileName = header
            };
            if (dialog.ShowDialog() == true)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    MemoryStream ms = new MemoryStream();
                    grid.Export(
                        ms,
                        new GridViewExportOptions()
                        {
                            Format = ExportFormat.ExcelML,
                            ShowColumnHeaders = true,
                            ShowColumnFooters = true,
                            ShowGroupFooters = false,
                        });
                    ms.Seek(0, SeekOrigin.Begin);
                    header = String.Format(
                        "<Row><Cell  ss:Index='1'><Data ss:Type='String'>{0}</Data></Cell></Row>", header);
                    StreamReader sr = new StreamReader(ms);
                    string msStr = sr.ReadToEnd();
                    msStr = msStr.Insert(msStr.IndexOf("<Row>"), header);                    
                    stream.Write(Encoding.UTF8.GetBytes(msStr), 0, msStr.Length);
                }
    
                Process.Start(dialog.FileName);
            }
        }
        catch
        {
            Notification.Error("Process Busy", "Please exit excel instance.");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多