Reading an Excel File From C#

后端 未结 5 833
我寻月下人不归
我寻月下人不归 2020-12-09 23:38

I have a connection string to read an excel file from my C# project that looks like this..

String ConnectionString  = \"Provider=Microsoft.ACE.OLEDB.12.0;\"          


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 23:43

    Following Code will Read the Excel file & Fill DataTable with its data

    try
                {
                    string connectionString = string.Empty;
    
                    if (Path.GetExtension(ExcelFileName) == ".xlsx")
                    {
                        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFileName +
                            ";Extended Properties=Excel 12.0;";
                    }
                    else
                    {
                        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFileName + ";Extended Properties=Excel 8.0;";
                    }
    
                    OleDbCommand selectCommand = new OleDbCommand();
                    OleDbConnection connection = new OleDbConnection();
                    OleDbDataAdapter adapter = new OleDbDataAdapter();
                    connection.ConnectionString = connectionString;
    
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
    
                    DataTable dtSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    
                    List SheetsName = GetSheetsName(dtSchema);
                    for (int i = 0; i < SheetsName.Count; i++)
                    {
                        selectCommand.CommandText = "SELECT * FROM [" + SheetsName[i] + "]";
                        selectCommand.Connection = connection;
                        adapter.SelectCommand = selectCommand;
                        DataTable Sheet = new DataTable();
                        Sheet.TableName = SheetsName[i].Replace("$", "").Replace("'", "");
                        adapter.Fill(Sheet);
    
                        if (Sheet.Rows.Count > 0)
                        {
                            Records.Tables.Add(Sheet);                        
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteLog(ex);
                }
    

提交回复
热议问题