Method for C to Obtain Excel File Table Structure Information Based on OLEDB
- 2021-12-13 16:42:55
- OfStack
This paper gives an example of how C # obtains the structure information of Excel file table based on OLEDB. Share it for your reference, as follows:
This question comes from the forum. Similarly, you can get the table structure information of access and other databases.
using System;
namespace ConsoleApplication11
{
class Program
{
public static void Main()
{
getExcelFileInfo( @" c:a.xls " );
}
private static void getExcelFileInfo(string Path)
{
string strConn = " Provider=Microsoft.Jet.OLEDB.4.0; " + " Data Source= " + Path + " ; " + " Extended Properties=Excel 8.0; " ;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
conn.Open();
System.Data.DataTable table = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null );
foreach (System.Data.DataRow drow in table.Rows)
{
string TableName = drow[ " Table_Name " ].ToString();
Console.WriteLine(TableName + " : " );
System.Data.DataTable tableColumns = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, new object [] { null , null , TableName , null } );
foreach (System.Data.DataRow drowColumns in tableColumns.Rows)
{
string ColumnName = drowColumns[ " Column_Name " ].ToString();
Console.WriteLine( " " + ColumnName);
}
}
Console.ReadKey( true );
}
}
}
For more readers interested in C # related content, please check the topics on this site: "Summary of C # Operating Excel", "Summary of C # Programming Thread Use Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"
I hope this article is helpful to everyone's C # programming.