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;
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();
Hello,We currently don’t support XLSX format due to limitations in the Silverlight platform. This feature should be included in the next release planned for the next month.We are also working on to include it in the next service release for Silverlight 10.2.About the formatting in the forums you can paste your code to editor(like notepad) that is removing the leading character any formatting generated by other editors. Or using smaller code snipped and use preview of the forum to see how is looking before post it.Let me know if you have any further questions.Sincerely,DimiDeveloper Support EngineerInfragistics, Inc.
Oops. Forgot to include a method.
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;
I could also use a suggestion or two about formatting my posts (with & without source code). I'm not sure why my original post had a ton of leading whitespace.