Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
Exporting to Excel (group by)
posted

Hello,

When I use the Silverlight 'Export to Excel' funcitonality, I am able to export the basic data

But, when I format that data (such as using Group By functionality) and then export, my excel worksheet contains no data, only the Column headers.

Is this a known issue or am I doing something incorrectly?

Thank you, --Chris

 

Parents
  • 45
    Suggested Answer
    posted

    Stefana,

    I have take the code directly from the C# tab on the Infragistics Silverlight Sample Browser at: http://labs.infragistics.com/silverlight/lobsamples/2009.2/

    The only differnce is that my code references my Infragistics datagrid with it's Group By functionality turned on

    <

     

    igGrid:XamWebGrid.GroupBySettings>

     

     

     

     

    <igGrid:GroupBySettings AllowGroupByArea="Top" />

     

     

     

    </igGrid:XamWebGrid.GroupBySettings>

    private void TestExcel_Click(object sender, RoutedEventArgs e)
            {

                Workbook dataWorkbook = new Workbook();
                Worksheet sheetOne = dataWorkbook.Worksheets.Add("Data Sheet");

                //Freeze header row
                sheetOne.DisplayOptions.PanesAreFrozen = true;
                sheetOne.DisplayOptions.FrozenPaneSettings.FrozenRows = 1;

                // Build Column List
                sheetOne.DefaultColumnWidth = 5000;
                sheetOne.Columns[1].Width = 8500;
                sheetOne.Columns[3].Width = 10000;
                int currentColumn = 0;
                foreach (Column column in this.dataGrid.Columns)
                {
                    if (column.Visibility == Visibility.Visible)
                    {
                        this.SetCellValue(sheetOne.Rows[0].Cells[currentColumn], column.HeaderText);
                        currentColumn++;
                    }
                }

                // Export Data From Grid
                int currentRow = 1;
                foreach (Row row in this.dataGrid.Rows)
                {
                    int currentCell = 0;
                    foreach (Cell cell in row.Cells)
                    {
                        if (cell.Column.Visibility == Visibility.Visible)
                        {
                            this.SetCellValue(sheetOne.Rows[currentRow].Cells[currentCell], cell.Value);
                            currentCell++;
                        }
                    }
                    currentRow++;
                }

                this.SaveExport(dataWorkbook);
            }

            private void SetCellValue(WorksheetCell cell, object value)
            {
                cell.Value = value;
                cell.CellFormat.ShrinkToFit = ExcelDefaultableBoolean.True;
                cell.CellFormat.VerticalAlignment = VerticalCellAlignment.Center;
                cell.CellFormat.Alignment = HorizontalCellAlignment.Center;
            }


            private void SaveExport(Workbook dataWorkbook)
            {
                SaveFileDialog dialog = new SaveFileDialog { Filter = "Excel files|*.xls", DefaultExt = "xls" };

                bool? showDialog = dialog.ShowDialog();
                if (showDialog == true)
                {
                    using (Stream exportStream = dialog.OpenFile())
                    {
                        dataWorkbook.Save(exportStream);
                        exportStream.Close();
                    }

                }
            }

Reply Children