Friday, December 10, 2010

Exports Silverlight data to MS excel

Download LiteExcel from codeplex ( my open source silverlight library for manipulating MS excel without  COM interaction).
Add reference of “Lite.Library.dll” and “Lite.ExcelLibrary” to your Silverlight project.

Refere “Lite.ExcelLibrary.SpreadSheet” on the top of your Silverlight page.
using Lite.ExcelLibrary.SpreadSheet;
 
Writing an excel file
// open file dialog to select an export file.   
SaveFileDialog sDialog = new SaveFileDialog();
sDialog.Filter = "Excel Files(*.xls)|*.xls";
if (sDialog.ShowDialog() == true)
{
    // create an instance of excel workbook
     Workbook workbook = new Workbook();
     // create a worksheet object
     Worksheet worksheet = new Worksheet("Friends");
     // write data in worksheet cells
     worksheet.Cells[0, 0] = new Cell("Column1");
     worksheet.Cells[0, 1] = new Cell("Column2");
     worksheet.Cells[0, 2] = new Cell("Column3");
     worksheet.Cells[1, 0] = new Cell("string value");                    
     worksheet.Cells[1, 1] = new Cell(478574.5, "#,###.00");
     worksheet.Cells[1, 2] = new Cell(4);
     workbook.Worksheets.Add(worksheet);
     Stream sFile = sDialog.OpenFile();
     // save method needs a stream object to write an excel file. 
     workbook.Save(sFile);
}

28 comments:

  1. Hi again

    Please I need more functionality for formatting the cells in Excel.
    How can I do with your library?
    For instance: font in bold.

    Thanks

    ReplyDelete
  2. Hi, Yes it would be great if we could manipulate the Cells style.
    Muhammad it is possible with yours DLLs?.

    Thanks.

    ReplyDelete
  3. Thanks man, you resolve a lot of my problems, thanks soo muth.

    ReplyDelete
  4. Hi, I also wonder if we are able to merge cells and rows.
    Thanks...

    ReplyDelete
  5. The source doesn't compile (can't even open the project), when I add the binary dll to a project, it says assembly system.windows not referenced, when I add system.windows.presentation, it says exception in ms.internal.jolt...

    ReplyDelete
  6. I correct myself:

    Copy all source files into a new project and add references to:
    PresentationCore
    PresentationFramwork
    WindowsBase
    System.Xaml

    Then it works.
    One still needs to comment out
    TranslateImageToBytes in ImageTranslator.cs

    Now it works in a console application :-))

    ReplyDelete
  7. Added an issue on CodePlex.

    See here: http://excellite.codeplex.com/workitem/175

    ReplyDelete
  8. @Anonymous hey don`t complain, it`s a silverlight project!

    Thanks a lot for this! I used it and it works wonderfully, just need to try catch more around the book.load and book.save in case of problems. :)

    ReplyDelete
  9. How can I change the cell size, border, format?

    Please, post examples.

    Thanks

    ReplyDelete
  10. I'm using silverlight3 and it's giving build time error when referencing excellite classes.... will this work only on silver light 4?

    ReplyDelete
  11. Hello,
    your exportet Excel Workbook has always 49 empty Rows at the End of the File, do you know why and how to fix this?

    ReplyDelete
  12. Piotr KobylińskiJune 8, 2011 at 1:19 AM

    The sheet is instantly saved by OpenFile, is there an option to save the Excel to a stream, that I could do with the data what I want?

    ReplyDelete
  13. Its not working..
    i am using Office 2007,there is no data's in excel file after clicking the export button

    ReplyDelete
  14. How can I change the cell size, border, format?

    Please, post examples.

    ReplyDelete
  15. How can I change the cell size, border, format?

    Please, post examples.

    ReplyDelete
  16. Cell size border, color is not working.

    ReplyDelete
  17. CellStyle class is defined. But after meshing with it for two hours I realized that its dummy. Total waste..

    ReplyDelete
  18. Is it possible to do indentation of cells and/or grouping? I'm porting some Excel export code from an ASP.Net app that uses the Infragistics Excel library to SL4 and want to get the same look in grouped grids if possible.

    Cheers,

    Paul.

    ReplyDelete
  19. Hi,

    It is only working for .xls files? How can I use it for .xlsx? When I use excel 2007 it says it couldn't open the document. Please help me.

    ReplyDelete
  20. functionality for formatting the cells in Excel.
    Plz Read this

    http://humrahimcs.wordpress.com/2012/01/26/telerik-radgridview-export-to-excel-with-format/

    ReplyDelete
  21. Hi,

    I was trying to export multiple pictures in single worksheet and excel cannot open the resulting file.

    Can you help,

    Thanks,

    ReplyDelete
  22. Does this now work with Excel 2007-2010? I read on codeplex that it was not as yet /

    ReplyDelete
  23. Hi,

    How can i export multiple files in multiple worksheet?

    Any help would be appreciated.
    Thanks.

    ReplyDelete
  24. Hi,
    How can I change the cell size, border, format and color?
    Thanks.

    ReplyDelete
  25. What is the way if someone want to create excel from an ObservableCollection of custom class type ??

    ReplyDelete
  26. Hi, How to set font style for header ?

    ReplyDelete