locked
Silverlight with Excel RRS feed

  • Question

  • How can I bind the excel data to DataGrid?

    I have a excel with one sheet. There are three columns, Date, Product and Price. I want to show in DataGrid with the same formate(has three columns).

    Monday, July 27, 2015 1:48 PM

Answers

  • Excel runs in a browser sandbox.

    It can't just read a file on your hard drive unless there is "User interaction" such as choosing the file to work with every time.

    Does that fit your scenario?

    You'd have to put this file on the server and have a service there that reads it, provides the data to the Silverlight client.



    Monday, July 27, 2015 2:10 PM
    Moderator
  • Hi Gkhnd,

    Please try with ExcelLite which can help us read Excel. I create a test project that read excel with ExcelLite. After I get the data from the excel file, I create a ViewModel which used to bind to DataGrid. Please refer to following code.

    private void btnSelectFile_Click(object sender, RoutedEventArgs e)
    {
       OpenFileDialog oFile = new OpenFileDialog();
       oFile.Filter = "Excel Files(*.xls)|*.xls";
    
       if (oFile.ShowDialog() == true)
       {
          FileStream fs = oFile.File.OpenRead();
          Workbook book = Workbook.Open(fs);
    
          Worksheet sheet = book.Worksheets[0];
    
          List<ExcelViewModel> lstDataSource = new List<ExcelViewModel>();
          
          for (int i = sheet.Cells.FirstRowIndex + 1; i <= sheet.Cells.LastRowIndex; i++)
           {
              ExcelViewModel evm = new ExcelViewModel();
              evm.CustomerName = sheet.Cells[i, 0].StringValue;
              evm.Tel = sheet.Cells[i, 1].StringValue;
              evm.Address = sheet.Cells[i, 2].StringValue;
              lstDataSource.Add(evm);
           }
    
            dgData.ItemsSource = lstDataSource;
        }
    
    }
    
    public class ExcelViewModel
    {
       public string CustomerName { set; get; }
       public string Tel { set; get; }
       public string Address { set; get; }
    
    }
    


    Best Regards,
    Weiwei

    Tuesday, July 28, 2015 5:34 AM
    Moderator

All replies

  • Excel runs in a browser sandbox.

    It can't just read a file on your hard drive unless there is "User interaction" such as choosing the file to work with every time.

    Does that fit your scenario?

    You'd have to put this file on the server and have a service there that reads it, provides the data to the Silverlight client.



    Monday, July 27, 2015 2:10 PM
    Moderator
  • Hi Gkhnd,

    Please try with ExcelLite which can help us read Excel. I create a test project that read excel with ExcelLite. After I get the data from the excel file, I create a ViewModel which used to bind to DataGrid. Please refer to following code.

    private void btnSelectFile_Click(object sender, RoutedEventArgs e)
    {
       OpenFileDialog oFile = new OpenFileDialog();
       oFile.Filter = "Excel Files(*.xls)|*.xls";
    
       if (oFile.ShowDialog() == true)
       {
          FileStream fs = oFile.File.OpenRead();
          Workbook book = Workbook.Open(fs);
    
          Worksheet sheet = book.Worksheets[0];
    
          List<ExcelViewModel> lstDataSource = new List<ExcelViewModel>();
          
          for (int i = sheet.Cells.FirstRowIndex + 1; i <= sheet.Cells.LastRowIndex; i++)
           {
              ExcelViewModel evm = new ExcelViewModel();
              evm.CustomerName = sheet.Cells[i, 0].StringValue;
              evm.Tel = sheet.Cells[i, 1].StringValue;
              evm.Address = sheet.Cells[i, 2].StringValue;
              lstDataSource.Add(evm);
           }
    
            dgData.ItemsSource = lstDataSource;
        }
    
    }
    
    public class ExcelViewModel
    {
       public string CustomerName { set; get; }
       public string Tel { set; get; }
       public string Address { set; get; }
    
    }
    


    Best Regards,
    Weiwei

    Tuesday, July 28, 2015 5:34 AM
    Moderator