using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
namespace Skyiv.Ben.Util
{
sealed class Pub
{
public static string GetExcelFirstTableName(string excelFileName)
{
string tableName = null;
if (File.Exists(excelFileName))
{
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet."+
"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tableName = dt.Rows[0][2].ToString().Trim();
}
}
return tableName;
}
}
}
以下是测试程序:
namespace Skyiv.Ben.Test
{
using Skyiv.Ben.Util;
class MainTest
{
static void Main(string [] args)
{
foreach (string s in args)
Console.WriteLine("[{0}] => [{1}]", s, Pub.GetExcelFirstTableName(s));
}
}
}
请发表评论