当我单击打开时,将我使用 wpf 中的对话框打开的 excel 文件加载到数据网格中

Posted

技术标签:

【中文标题】当我单击打开时,将我使用 wpf 中的对话框打开的 excel 文件加载到数据网格中【英文标题】:Load the excel file which i open using dialog box in wpf into datagrid when i click open 【发布时间】:2021-10-29 01:04:06 【问题描述】:
private void btndilog_Click(object sender, RoutedEventArgs e)
        
            var dilog = new Microsoft.Win32.OpenFileDialog();
            dilog.FileName = "Excel files";
            dilog.DefaultExt = ".xlsx";
            dilog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";

            ///show open file dialog box 
            bool? result = dilog.ShowDialog();
            ///process open file dialog box results
            if(result==true)
            
                string filename = dilog.FileName;
            
        

**我编写了这段代码来使用对话框加载一个 excel 文件,现在我想要当我点击打开按钮时将该 excel 文件的所有数据加载到我的数据网格中,因为我是 c# 和 wpf am 的新手遇到麻烦谁能帮帮我

Datagrid where i want to display the details of excel file

Excel file that i want to load to my data grid my i click on open button

【问题讨论】:

要完成您的任务,您需要使用能够处理 excel 文件的库。例如,您可以查看 VSTO(Visual Studio Tools for Office)。 【参考方案1】:

添加引用 Microsoft.Office.Interop.Excel 并使用这段代码首先将您的文件读入数据表,然后将数据表与数据网格绑定

//using a click event 
       private void MnBrowse_Click(object sender, RoutedEventArgs e)
            
    
    //using dialog box to open the excel file and giving the extension as .xlsx to open the excel files
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.DefaultExt = ".xlsx";
                openfile.Filter = "(.xlsx)|*.xlsx";
    
                ///show open file dialog box 
                bool? result = openfile.ShowDialog();
                ///process open file dialog box results
                if (result == true)
                
//copying the path of the excel file to a textbox named txtFilePath
                    txtFilePath.Text = openfile.FileName;
    //Add reference then select Microsoft .Office.Interop.Excel
                    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                    //Static File From Base Path...........
                    //Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "TestExcel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    //Dynamic File Using Uploader...........
                    Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
                    Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;
    //reading the excel file in the datatable
                    string strCellData = "";
                    double douCellData;
                    int rowCnt = 0;
                    int colCnt = 0;
    
                    DataTable dt = new DataTable();
                    for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
                    
                        string strColumn = "";
                        strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                        dt.Columns.Add(strColumn, typeof(string));
                    
    
                    for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
                    
                        string strData = "";
                        for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
                        
                            try
                            
                                strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                                strData += strCellData + "|";
                            
                            catch (Exception Ex)
                            
                                douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                                strData += douCellData.ToString() + "|";
                            
                        
                        strData = strData.Remove(strData.Length - 1, 1);
                        dt.Rows.Add(strData.Split('|'));
                    
    
                    dtGrid.ItemsSource = dt.DefaultView;
    
                    excelBook.Close(true, null, null);
                    excelApp.Quit();
    
                
    
    
            

【讨论】:

请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。

以上是关于当我单击打开时,将我使用 wpf 中的对话框打开的 excel 文件加载到数据网格中的主要内容,如果未能解决你的问题,请参考以下文章

打开通知警报后加载网络视图?

打开文件对话框并使用 WPF 控件和 C# 选择文件

电脑word2007双击打不开了怎么办

Excel文件第一次双击打不开,退出后第二次双击就能打开。

在使用ItemContainerGenerator进行自定义时,WPF ComboBox在第二次打开之前不会更新项目

怎么将sublime设置为打开指定文件格式的默认方式,而不是如图所示的这种出来对话框让自己再选择的方式?