【问题标题】:How to set excel value to datagrid in c#?如何在c#中将excel值设置为datagrid?
【发布时间】:2019-05-29 20:50:45
【问题描述】:

xmal.c

  <StackPanel HorizontalAlignment="Left" Height="337" VerticalAlignment="Top" Width="514" Margin="103,39,0,0">
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source=Lecturers}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                    <DataGridTextColumn Header="Surname" Binding="{Binding Surname}"/>
                    <DataGridTextColumn Header="Phone" Binding="{Binding Phone_Number}" />
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>

Excel 行列:

Name   Surname   Phone_Number

A        C           123

C        C           124

你好。我在 Excel 中得到了值。它以同样的方式出现。但是当我想设置datagrid时我做不到。如何设置数据网格?使用 Excel 计算相同的数据网格行数。

 **Lecturers.DataSource=cellvalue** 

这一行使用set datagrid。但我没有设置数据网格。

【问题讨论】:

    标签: c# excel mvvm datagrid


    【解决方案1】:

    首先创建一个代表你的数据的类

        public class Lecturer
        {
           public string Name { get; set;}
    
           public string Surname { get; set;}
    
           public int Phone_Number { get; set;}
        }
    

    然后在您的 ViewModel 中,声明一个 ObservableCollection,您将能够在其中存储 Read 对象。 ObservableCollection 实现了 INotifyPropertyChanged 接口,该接口允许您的视图收到 ViewModel 中数据更改的通知。

    [...]
    
    public ObservableCollection<Lecturer> Lecturers { get; set; }
    
    public MyViewModel()
    {
      Lecturers = new ObservableCollection<Lecturer>();
    }
    
    public void ExtractLecturersFromXlFile(string filePath)
    {
     Excel.Application xlApp = new Excel.Application();
     Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath);
     Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
    
     Excel.Range range = sheet.UsedRange;
     Excel.Range excelrange = (Excel.Range)sheet.get_Range("A1:C100",Type.Missing);
    
     // Iterate through datas, fill Lecturer's object and add it to the Observable                 
     // Collection
    
    
     // Cleanup  
     GC.Collect();
     GC.WaitForPendingFinalizers();
    
     Marshal.ReleaseComObject(xlWorksheet);
    
     // Close and release  
     xlWorkbook.Close();
     Marshal.ReleaseComObject(xlWorkbook);
    
     // Quit and release  
     xlApp.Quit();
     Marshal.ReleaseComObject(xlApp);
    
    }
    

    在 XAML 中

         <StackPanel HorizontalAlignment="Left" Height="337" VerticalAlignment="Top"                   Width="514" Margin="103,39,0,0">
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Lecturers}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                    <DataGridTextColumn Header="Surname" Binding="{Binding Surname}"/>
                    <DataGridTextColumn Header="Phone" Binding="{Binding Phone_Number}" />
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多