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
355
Export to Excel in Word 2007 format
posted

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I have a application module that displays a Silverlight XamGrid and I want to allow users to export the contents of the grid to Excel in either 'Excel 2007' or 'Excel 97-2003' format.  I use common code (derived from an IG sample) to save in either format, merely setting the Workbook.Format based upon the user's selection.  With this code, saving to 'Excel 97-2003' format works fine, but saving to 'Excel 2007' format results in the following problems: 1) The file is saved with no size (0 bytes); and 2) Upon attempting to open the file in Excel 2007, an error is displayed ("Excel cannot open the file xxx because the file format or file extension is not valid. ...and other stuff...").

Below is the method that I have created to export the grid to Excel.  Is there an extra step that must be performed in order to successfully save to ‘Excel 2007’ format?

        private void ExportGridToExcel(XamGrid grid, string worksheetName)

        {

            SaveFileDialog dialog = new SaveFileDialog { Filter =

"Excel 2007-2010 files (.xlsx)|*.xlsx|Excel 97-2003 files (.xls)|*.xls", DefaultExt = "xlsx" };

            bool? showDialog = dialog.ShowDialog();

 

            if (showDialog == true)

            {

                // create an output file stream for exporting the grid data

                using (Stream exportStream = dialog.OpenFile())

                {

                    try

                    {

                        // determine which Excel format to use for saving (Excel 2007

   // or Excel 97-2003)

                        WorkbookFormat workbookFormat = WorkbookFormat.Excel2007;

                        switch (dialog.FilterIndex)

                        {

                            case 1:

                                workbookFormat = WorkbookFormat.Excel2007;

                                break;

 

                            case 2:

                                workbookFormat = WorkbookFormat.Excel97To2003;

                                break;

 

                            default:

                                throw new Exception("Unrecognized workbook format selection");

                        }

 

                        // create an Excel workbook in the specified format

                        Workbook dataWorkbook = new Workbook(workbookFormat);

 

                        // create the Excel worksheet to receive the export data

                        Worksheet worksheet = dataWorkbook.Worksheets.Add(worksheetName);

 

                        // freeze header row

                        worksheet.DisplayOptions.PanesAreFrozen = true;

                        worksheet.DisplayOptions.FrozenPaneSettings.FrozenRows = 1;

 

                        // build prepare the header row (using the grid's column header

                        // as the text for each column)

                        worksheet.DefaultColumnWidth = 5000;

                        int currentColumn = 0;

                        foreach (Column column in grid.Columns)

                        {

                            if (column.Visibility == Visibility.Visible)

                            {

                                this.SetCellValue(worksheet.Rows[0].Cells[currentColumn],

                                                 column.HeaderText);

                                currentColumn++;

                            }

                        }

 

                        // export data from the grid to the Excel worksheet

                        int currentRow = 1;

                        foreach (Row row in grid.Rows)

                        {

                            int currentCell = 0;

                            foreach (Cell cell in row.Cells)

                            {

                                if (cell.Column.Visibility == Visibility.Visible)

                                {

                                    this.SetCellValue(worksheet.Rows[currentRow].Cells

[currentCell], cell.Value);

                                    currentCell++;

                                }

                            }

                            currentRow++;

                        }

 

                        // save the workbook

                        dataWorkbook.Save(exportStream);

                    }

                    finally

                    {

                        // close the output stream

                        exportStream.Close();

                    }

                }

            }

        }