日期:2014-05-18 浏览次数:21329 次
public DataSet ExcelToDs(string path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + path + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataSet ds = new DataSet();
//一个EXCEL文件可能有多个工作表,遍历之
foreach (DataRow dr in dtSchema.Rows)
{
string table = dr["TABLE_NAME"].ToString();
if (table == "'map copy$'")
{
string tname = "map copy$";
string strExcel = "SELECT * FROM [" + tname + "A1:IV100]";
ds.Tables.Add(table);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, conn);
[color=#FF0000] myCommand.Fill(ds, table);//定义了过多字段[/color] }
}
conn.Close();
return ds;
}