csstome 发表于 2013-1-26 14:19:05

c#通过oledb获取excel文件表结构信息

这个问题来自论坛提问,同理可以获得access等数据库的表结构信息。
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespaceConsoleApplication11
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifclassProgram
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicstaticvoidMain()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifgetExcelFileInfo(@"c:a.xls");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatestaticvoidgetExcelFileInfo(stringPath)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringstrConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"DataSource="+Path+";"+"ExtendedProperties=Excel8.0;";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.OleDb.OleDbConnectionconn=newSystem.Data.OleDb.OleDbConnection(strConn);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifconn.Open();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataTabletable=conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifforeach(System.Data.DataRowdrowintable.Rows)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringTableName=drow["Table_Name"].ToString();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.WriteLine(TableName+":");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifSystem.Data.DataTabletableColumns=conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,newobject[]...{null,null,TableName,null});
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifforeach(System.Data.DataRowdrowColumnsintableColumns.Rows)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringColumnName=drowColumns["Column_Name"].ToString();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.WriteLine(" "+ColumnName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.ReadKey(true);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: c#通过oledb获取excel文件表结构信息