Importing xls file to DataGrid Silverlight 4
Hi there,
Silverlight does not allow us to import excel file directly until we make our application OOB. If some one did not want to do that then below is the way of how to did it.
1. Adding Dll Reference
You need to download a library that is located at:
http://excellite.codeplex.com/
Install it and add these 2 references into your silverlight project:
/// <summary>
/// read the xls file and show that data in grid
/// </summary>
private void Event_ReadXLSFile ()
{
OpenFileDialog XLSFile = new OpenFileDialog();
XLSFile.Filter = "Excel Files(*.xls)|*.xls";
if (XLSFile.ShowDialog() == true)
{
LocationMaster.Clear();
// importing using excel lite library
FileStream fs = XLSFile.File.OpenRead();
Workbook wBook = Workbook.Open(fs);
Worksheet wSheet = wBook.Worksheets[0];
// loop over rows
for (int i = 1; i < wSheet.Cells.LastRowIndex + 1; i++)
{
clsLocationMaster clm = new clsLocationMaster();
// loop over columns
for (int j = wSheet.Cells.FirstColIndex; j < wSheet.Cells.LastColIndex + 1; j++)
{
// using switch to assign class its values
switch (j)
{
case 0:
clm.StockPoint = wSheet.Cells[i, j].Value.ToString();
break;
case 1:
clm.BranchCode = wSheet.Cells[i, j].Value.ToString();
break;
case 2:
clm.Location = wSheet.Cells[i, j].Value.ToString();
break;
case 3:
clm.Description = wSheet.Cells[i, j].Value.ToString();
break;
default:
break;
}
}
// adding a row to collection
LocationMaster.Add(clm);
}
// assign collection to paged collectionview
pcvGridData = new PagedCollectionView(LocationMaster);
}
}
LocationMaster => Observable collection of class type clsLocationMaster
Silverlight does not allow us to import excel file directly until we make our application OOB. If some one did not want to do that then below is the way of how to did it.
1. Adding Dll Reference
You need to download a library that is located at:
http://excellite.codeplex.com/
Install it and add these 2 references into your silverlight project:
- Lite.Library.dll
- Lite.ExcelLibrary.dll
- using Lite.ExcelLibrary.SpreadSheet;
This method should executes when user fires the Import action.
/// read the xls file and show that data in grid
/// </summary>
private void Event_ReadXLSFile ()
{
OpenFileDialog XLSFile = new OpenFileDialog();
XLSFile.Filter = "Excel Files(*.xls)|*.xls";
if (XLSFile.ShowDialog() == true)
{
LocationMaster.Clear();
// importing using excel lite library
FileStream fs = XLSFile.File.OpenRead();
Workbook wBook = Workbook.Open(fs);
Worksheet wSheet = wBook.Worksheets[0];
// loop over rows
for (int i = 1; i < wSheet.Cells.LastRowIndex + 1; i++)
{
clsLocationMaster clm = new clsLocationMaster();
// loop over columns
for (int j = wSheet.Cells.FirstColIndex; j < wSheet.Cells.LastColIndex + 1; j++)
{
// using switch to assign class its values
switch (j)
{
case 0:
clm.StockPoint = wSheet.Cells[i, j].Value.ToString();
break;
case 1:
clm.BranchCode = wSheet.Cells[i, j].Value.ToString();
break;
case 2:
clm.Location = wSheet.Cells[i, j].Value.ToString();
break;
case 3:
clm.Description = wSheet.Cells[i, j].Value.ToString();
break;
default:
break;
}
}
// adding a row to collection
LocationMaster.Add(clm);
}
// assign collection to paged collectionview
pcvGridData = new PagedCollectionView(LocationMaster);
}
}
LocationMaster => Observable collection of class type clsLocationMaster
Comments
Post a Comment