如何使用带有 C# 的 OLEDB 将 Excel 列名称复制到字符串?
How do I copy an Excel column name to a string using OLEDB with C#?
这里是菜鸟...我需要从 excel 文件中获取列名称,以便将其用作字符串。我在 OLEDB 中使用 C#。
private void CheckFiles();
{
OleDbConnection MyConnection;
DataSet DtSet;
OleDbDataAdapter MyCommand;
string file = @"C:\Users\...path...15.xlsm";
MyConnection = new OleDbConnection(@"provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +
file + "';Extended Properties=Excel 8.0;");
MyCommand = new OleDbDataAdapter("Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = " +
tab, MyConnection);
MyConnection.Open();
MyCommand.TableMappings.Add("Table", "Net-informations.com");
MyConnection.Close();
}
提前致谢!
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;";
ConnectionString = string.Format(ConnectionString, @"FullPathToExcelFile"); //my path replace with your actual file path
OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();
OleDbCommand cmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter oleDBAdapter = new OleDbDataAdapter();
oleDBAdapter.SelectCommand = cmdSelect;
DataSet myDataset = new DataSet();
oleDBAdapter.Fill(myDataset);
conn.Close();
这里是菜鸟...我需要从 excel 文件中获取列名称,以便将其用作字符串。我在 OLEDB 中使用 C#。
private void CheckFiles();
{
OleDbConnection MyConnection;
DataSet DtSet;
OleDbDataAdapter MyCommand;
string file = @"C:\Users\...path...15.xlsm";
MyConnection = new OleDbConnection(@"provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +
file + "';Extended Properties=Excel 8.0;");
MyCommand = new OleDbDataAdapter("Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = " +
tab, MyConnection);
MyConnection.Open();
MyCommand.TableMappings.Add("Table", "Net-informations.com");
MyConnection.Close();
}
提前致谢!
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;";
ConnectionString = string.Format(ConnectionString, @"FullPathToExcelFile"); //my path replace with your actual file path
OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();
OleDbCommand cmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter oleDBAdapter = new OleDbDataAdapter();
oleDBAdapter.SelectCommand = cmdSelect;
DataSet myDataset = new DataSet();
oleDBAdapter.Fill(myDataset);
conn.Close();