62,046
社区成员
发帖
与我相关
我的任务
分享
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var tableName = dt.Rows[0][2].ToString().Trim();
var str1 = string.Format("Select * from [{0}]", tableName);
OleDbDataAdapter oda = new OleDbDataAdapter(str1, conn);
DataSet ds = new DataSet();
oda.Fill(ds); //这一行报错 定义了过多字段
conn.Close();
File.Delete( newName );
public static string[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
objConn = new OleDbConnection(strConn);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch (Exception ex)
{
return null;
}
finally
{
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}