如何把excel所有数据导入到一个数组中?

解决方案 »

  1.   

    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" +"Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
    OleDbConnection conn = new OleDbConnection(strConn);
    conn.Open();
    OleDbDataAdapter myCommand = null;
    string strExcel = "select * from [" + "Sheet1"+ "$]";  //這裡的Sheet1是excel下面的每個工作表的序號
    myCommand = new OleDbDataAdapter(strExcel, strConn);
    DataSet ds = new DataSet();
    try
    {
    myCommand.Fill(ds,"temp");
    }
    catch
    {
    ds.Clear();
    conn.Close();
    return;
    }
    conn.Close();至此,你已經把excel的內容放到ds的一個table中了。你在再把它循環一下轉你成數組就行了。