Writing XLS File in ViewModel from Grid data Silverlight 4
Hi there,
We can write CSV file in our view model but silverlight did not allow us to write XLS file in ViewModel due to some security resons, if we need it we have to do somethong else.
I use a library to perform this functionality, you can get it from:
http://excellite.codeplex.com/
This link also provide some useful demos about using this DLL, and these are better than mine :)
Let's start the task.
1. Add Reference
Install it and add these 2 references into your silverlight project:
Write the below method in your view model that will be fired when its corresponding command is required by user:
/// <summary>
/// generate excel file from grid's data
/// </summary>
private void GenerateXLSFile ()
{
try
{
SaveFileDialog SaveXLSFile = new SaveFileDialog();
SaveXLSFile.Filter = "Excel Files(*.xls)|*.xls";
SaveXLSFile.DefaultExt = ".xls";
if (SaveXLSFile.ShowDialog() == true)
{
// initialization
Workbook book = new Workbook();
Worksheet sheet = new Worksheet("Export");
// headers of each column
sheet.Cells[0, 0] = new Cell(rsZ01S250.Manufacturer);
sheet.Cells[0, 1] = new Cell(rsZ01S250.Branch);
sheet.Cells[0, 2] = new Cell(rsZ01S250.PartNo);
sheet.Cells[0, 3] = new Cell(rsZ01S250.PartDescription);
sheet.Cells[0, 4] = new Cell(rsZ01S250.Price);
sheet.Cells[0, 5] = new Cell(rsZ01S250.LastUpdatedBy);
sheet.Cells[0, 6] = new Cell(rsZ01S250.LastUpdatedDate);
// loop over the collection
for (int i=0; i < PurchasePriceEntry.Count; i++)
{
clsPurchasePriceEntry clsObj = new clsPurchasePriceEntry();
clsObj = PurchasePriceEntry[i];
sheet.Cells[i + 1, 0] = new Cell(clsObj.Manufacturer);
sheet.Cells[i + 1, 1] = new Cell(clsObj.Branch);
sheet.Cells[i + 1, 2] = new Cell(clsObj.PartNo);
sheet.Cells[i + 1, 3] = new Cell(clsObj.PartDescription);
sheet.Cells[i + 1, 4] = new Cell(clsObj.Price);
sheet.Cells[i + 1, 5] = new Cell(clsObj.LastUpdatedBy);
sheet.Cells[i + 1, 6] = new Cell(clsObj.LastUpdatedDate);
}
// adding sheet to book
book.Worksheets.Add(sheet);
Stream XLSFile = SaveXLSFile.OpenFile();
// saving the file, require stream object to save
book.Save(XLSFile);
CommonFunctions.ShowMessage(rsCommon.INF0004, MessageBoxType.Information);
}
}
catch (Exception)
{
CommonFunctions.ShowMessage(Resources.rsCommon.ERR0007, MessageBoxType.Error);
}
}
rsZ01S250 => my resource file from where i show all the text of screen
clsPurchasePriceEntry => Class whose type of collection is bound to data grid
PurchasePriceEntry => Collection that is the source of grid data
We can write CSV file in our view model but silverlight did not allow us to write XLS file in ViewModel due to some security resons, if we need it we have to do somethong else.
I use a library to perform this functionality, you can get it from:
http://excellite.codeplex.com/
This link also provide some useful demos about using this DLL, and these are better than mine :)
Let's start the task.
1. Add Reference
Install it and add these 2 references into your silverlight project:
- Lite.Library.dll
- Lite.ExcelLibrary.dll
- using Lite.ExcelLibrary.SpreadSheet;
Write the below method in your view model that will be fired when its corresponding command is required by user:
/// <summary>
/// generate excel file from grid's data
/// </summary>
private void GenerateXLSFile ()
{
try
{
SaveFileDialog SaveXLSFile = new SaveFileDialog();
SaveXLSFile.Filter = "Excel Files(*.xls)|*.xls";
SaveXLSFile.DefaultExt = ".xls";
if (SaveXLSFile.ShowDialog() == true)
{
// initialization
Workbook book = new Workbook();
Worksheet sheet = new Worksheet("Export");
// headers of each column
sheet.Cells[0, 0] = new Cell(rsZ01S250.Manufacturer);
sheet.Cells[0, 1] = new Cell(rsZ01S250.Branch);
sheet.Cells[0, 2] = new Cell(rsZ01S250.PartNo);
sheet.Cells[0, 3] = new Cell(rsZ01S250.PartDescription);
sheet.Cells[0, 4] = new Cell(rsZ01S250.Price);
sheet.Cells[0, 5] = new Cell(rsZ01S250.LastUpdatedBy);
sheet.Cells[0, 6] = new Cell(rsZ01S250.LastUpdatedDate);
// loop over the collection
for (int i=0; i < PurchasePriceEntry.Count; i++)
{
clsPurchasePriceEntry clsObj = new clsPurchasePriceEntry();
clsObj = PurchasePriceEntry[i];
sheet.Cells[i + 1, 0] = new Cell(clsObj.Manufacturer);
sheet.Cells[i + 1, 1] = new Cell(clsObj.Branch);
sheet.Cells[i + 1, 2] = new Cell(clsObj.PartNo);
sheet.Cells[i + 1, 3] = new Cell(clsObj.PartDescription);
sheet.Cells[i + 1, 4] = new Cell(clsObj.Price);
sheet.Cells[i + 1, 5] = new Cell(clsObj.LastUpdatedBy);
sheet.Cells[i + 1, 6] = new Cell(clsObj.LastUpdatedDate);
}
// adding sheet to book
book.Worksheets.Add(sheet);
Stream XLSFile = SaveXLSFile.OpenFile();
// saving the file, require stream object to save
book.Save(XLSFile);
CommonFunctions.ShowMessage(rsCommon.INF0004, MessageBoxType.Information);
}
}
catch (Exception)
{
CommonFunctions.ShowMessage(Resources.rsCommon.ERR0007, MessageBoxType.Error);
}
}
rsZ01S250 => my resource file from where i show all the text of screen
clsPurchasePriceEntry => Class whose type of collection is bound to data grid
PurchasePriceEntry => Collection that is the source of grid data
Comments
Post a Comment