如何在C#中将Excel值设置为DataGrid?

时间:2019-05-29 20:50:45

标签: c# excel mvvm datagrid

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.Application app=new Excel.Application();
Excel.Workbook workbook=app.Workbooks.Open(file,Type.Missing);
Excel.WorkSheet sheet=workbook.Sheets[i];
Excel.Range range=sheet.UsedRange;
Excel.Range excelrange=(Excel.Range)sheet.get_Range("A1:C100",Type.Missing);
Dictionary<string,string> dict=new Dictionary<string,string>();
foreach(Excel.Range cell in excelrange)
{
string cellindex=cell.ge_Address(//this area missing value);
string cellvalue=Convert.ToString(cell.Value);
Lecturers.DataSource=cellvalue
}

}

嗨,我在Excel中得到了值。它以相同的方式来。但是当我想设置datagrid时我做不到。如何设置datagrid? Excel使用相同的datagrid行计数。

 **Lecturers.DataSource=cellvalue** 

此行使用set datagrid。但是我没有设置datagrid。

1 个答案:

答案 0 :(得分:0)

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

    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>