寫一篇關於將Datatable轉換成Vba可接受的RecordSet類別,如果把結果顛倒過來呢?這也是可以的。其主要目的是讓Excel內的資料上傳後,可以透RecordSet 轉換成 Datatable解析其中資料內容或是讓.Net可以快速針對內容作處理。
主要需要靠
ADODB.Stream
&ADODB.Recordset
物件幫助。12Type adoStreamType = Type.GetTypeFromProgID("ADODB.Stream");Type adoRecordsetType = Type.GetTypeFromProgID("ADODB.Recordset");透過
System.Activator
物件幫忙進行轉換1234567891011121314151617181920212223242526272829303132string strXML = //<-接Recordset format字串用DataSet ds = new DataSet();OleDbDataAdapter odb = new OleDbDataAdapter();Type adoStreamType = Type.GetTypeFromProgID("ADODB.Stream");Type adoRecordsetType = Type.GetTypeFromProgID("ADODB.Recordset");object adoStream = Activator.CreateInstance(adoStreamType);object adoRecordset = Activator.CreateInstance(adoRecordsetType);try{adoStreamType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, adoStream, new object[] { });adoStreamType.InvokeMember("WriteText", System.Reflection.BindingFlags.InvokeMethod, null, adoStream, new object[] { strXML });adoStreamType.InvokeMember("Position", System.Reflection.BindingFlags.SetProperty, null, adoStream, new object[] { 0 });adoRecordsetType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, adoRecordset, new object[] { adoStream });odb.Fill(ds, adoRecordset, "Tmp_Data");}catch (Exception ex){clsWriteToLog.WriteToLog_E("oRecordSetToDatatable", ex.Message.ToString());}finally{odb.Dispose();odb = null;adoStreamType = null;adoRecordsetType = null;adoStream = null;adoRecordset = null;strXML = null;}
這樣就可以將Excel傳上來的RecordSet轉換成.Net可用的物件。