回复 4楼iceman的帖子
不知道你是如何实现的 我在headersMerging里修改 加入导出功能,一样无法导出表头
导出结果如下图
我修改了
Demo C1DataGrid_Demo2010.5/Merging/HeadersMerging_SL5.xaml 和 CS 麻烦你看看
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------华丽的分割线--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CS文件
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using C1.Silverlight.DataGrid;
- using C1.Silverlight.Data;
- using System.Windows.Data;
- using C1.Silverlight.DataGrid.Excel;
- namespace C1DataGrid_Demo2010
- {
- public partial class HeadersMerging : UserControl
- {
- DataGridColumn[] _headerRowColumns;
- DataGridRow[] _headerColumnRows;
- public HeadersMerging()
- {
- InitializeComponent();
- // save headers rows and columns for later
- _headerRowColumns = grid.Columns.Take(2).ToArray();
- _headerColumnRows = grid.TopRows.Take(2).ToArray();
- // forbid unfreezing headers
- grid.FrozenColumnCountChanged += delegate
- {
- if (grid.FrozenColumnCount < _headerRowColumns.Length)
- grid.FrozenColumnCount = _headerRowColumns.Length;
- };
- // handle merging when view port changes
- grid.MergingCells += new EventHandler<DataGridMergingCellsEventArgs>(grid_MergingCells);
- #region ** Initialize data
- List<WordCupEntry> entries = new List<WordCupEntry>();
- entries.Add(new WordCupEntry() { Continent = "South America", Country = "Brazil", First = "5 (1958, 1962, 1970, 1994, 2002)", Second = "2 (1950*, 1998)", Third = "2 (1938, 1978)", Fourth = "1 (1974)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Italy", First = "4 (1934*, 1938, 1982, 2006)", Second = "2 (1970, 1994)", Third = "1 (1990*)", Fourth = "1 (1978)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Germany", First = "3 (1954, 1974*, 1990)", Second = "4 (1966, 1982, 1986, 2002)", Third = "4 (1934, 1970, 2006*, 2010)", Fourth = "1 (1958)" });
- entries.Add(new WordCupEntry() { Continent = "South America", Country = "Argentina", First = "2 (1978*, 1986)", Second = "2 (1930, 1990)", Third = "-", Fourth = "-" });
- entries.Add(new WordCupEntry() { Continent = "South America", Country = "Uruguay", First = "2 (1930*, 1950)", Second = "-", Third = "-", Fourth = "3 (1954, 1970, 2010)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "France", First = "1 (1998*)", Second = "1 (2006)", Third = "2 (1958, 1986)", Fourth = "1 (1982)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "England", First = "1 (1966*)", Second = "-", Third = "-", Fourth = "1 (1990)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Spain", First = "1 (2010)", Second = "-", Third = "-", Fourth = "1 (1950)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Netherlands", First = "-", Second = "3 (1974, 1978, 2010)", Third = "-", Fourth = "1 (1998)" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Czechoslovakia", First = "-", Second = "2 (1934, 1962)", Third = "-", Fourth = "-" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Hungary", First = "-", Second = "2 (1938, 1954)", Third = "-", Fourth = "-" });
- entries.Add(new WordCupEntry() { Continent = "Europe", Country = "Sweden", First = "-", Second = "1 (1958*)", Third = "2 (1950, 1994)", Fourth = "1 (1938)" });
- #endregion
- grid.ItemsSource = entries;
- }
- private void btnExcel_Click(object sender, RoutedEventArgs e)
- {
- var options = new ExcelSaveOptions()
- {
- FileFormat = ExcelFileFormat.Xlsx, // change this to offer a different Excel format
- KeepColumnWidths = true,
- KeepRowHeights = true
- };
- var excelExt = options.FileFormat.ToString();
- var dialog = new System.Windows.Controls.SaveFileDialog()
- {
- DefaultExt = "*." + excelExt,
- Filter = "Excel " + excelExt + " (*." + excelExt + ")|*." + excelExt + "|All files (*.*)|*.*",
- };
- if (dialog.ShowDialog() == false) return;
- using (var stream = dialog.OpenFile())
- {
- grid.Save(stream, options);
- }
- }
- void grid_MergingCells(object sender, DataGridMergingCellsEventArgs e)
- {
- // view port columns & rows without headers
- var nonHeadersViewportCols = grid.Viewport.Columns.Where(c => !_headerRowColumns.Contains(c)).ToArray();
- var nonHeadersViewportRows = grid.Viewport.Rows.Where(r => !_headerColumnRows.Contains(r)).ToArray();
- // merge column & rows headers
- foreach (var range in MergingHelper.Merge(Orientation.Vertical, _headerColumnRows, nonHeadersViewportCols, true))
- {
- e.Merge(range);
- }
- foreach (var range in MergingHelper.Merge(Orientation.Horizontal, nonHeadersViewportRows, _headerRowColumns, true))
- {
- e.Merge(range);
- }
- // merge header intersection as we want, in this case, horizontally
- foreach (var range in MergingHelper.Merge(Orientation.Horizontal, _headerColumnRows, _headerRowColumns, true))
- {
- e.Merge(range);
- }
- // merge content
- foreach (var range in MergingHelper.Merge(Orientation.Vertical, nonHeadersViewportRows, nonHeadersViewportCols, false))
- {
- e.Merge(range);
- }
- }
- }
- public class WordCupEntry
- {
- public string Continent { get; set; }
- public string Country { get; set; }
- public string First { get; set; }
- public string Second { get; set; }
- public string Third { get; set; }
- public string Fourth { get; set; }
- }
- }
复制代码 |