我用过的一个例子: Sub ExportExcelRecordset(Adorecordset As Recordset) On Error GoTo err_handle Dim excelsheet As Object MDImain.StatusBar1.Panels(1).Text = "正在启动EXCEL,请稍侯...." Set excelsheet = CreateObject("excel.application") excelsheet.Workbooks.Add excelsheet.Visible = True Adorecordset.MoveFirst Dim rows As Long, cols As Long Dim Row As Long, Col As Long rows = Adorecordset.RecordCount cols = Adorecordset.Fields.Count For Col = 0 To cols - 1 excelsheet.Cells(1, Col + 1).Value = Adorecordset.Fields(Col).name Next For Row = 2 To rows + 1 For Col = 0 To cols - 1 excelsheet.Cells(Row, Col + 1).Value = IIf(IsNull(Adorecordset.Fields(Col).Value), "", Adorecordset.Fields(Col).Value) Next Adorecordset.MoveNext NextMDImain.StatusBar1.Panels(1).Text = "就绪" Exit Sub Screen.MousePointer = 0 Exit Suberr_handle: If err.Number = 1004 Then MsgBox "因Excel被用户关闭而中断输出!", vbInformation Else Resume Next End If Screen.MousePointer = 0 End Sub
Sub ExportExcelRecordset(Adorecordset As Recordset)
On Error GoTo err_handle
Dim excelsheet As Object
MDImain.StatusBar1.Panels(1).Text = "正在启动EXCEL,请稍侯...."
Set excelsheet = CreateObject("excel.application")
excelsheet.Workbooks.Add
excelsheet.Visible = True
Adorecordset.MoveFirst
Dim rows As Long, cols As Long
Dim Row As Long, Col As Long
rows = Adorecordset.RecordCount
cols = Adorecordset.Fields.Count
For Col = 0 To cols - 1
excelsheet.Cells(1, Col + 1).Value = Adorecordset.Fields(Col).name
Next
For Row = 2 To rows + 1
For Col = 0 To cols - 1
excelsheet.Cells(Row, Col + 1).Value = IIf(IsNull(Adorecordset.Fields(Col).Value), "", Adorecordset.Fields(Col).Value)
Next
Adorecordset.MoveNext
NextMDImain.StatusBar1.Panels(1).Text = "就绪"
Exit Sub
Screen.MousePointer = 0
Exit Suberr_handle:
If err.Number = 1004 Then
MsgBox "因Excel被用户关闭而中断输出!", vbInformation
Else
Resume Next
End If
Screen.MousePointer = 0
End Sub
然后将复制后的DataTable中的相关列(需要隐藏的)删除,然后再导出。
public object oDataSourse;
public object[] oDataRow = new object [100] ;
public int i;
string sConn = "server=HEHE; uid=sa; pwd=; database=bookstore";
string sComm = "SELECT * FROM book_detail";
SqlDataAdapter DataAdapter =new SqlDataAdapter(sComm,sConn);
DataSet ds = new DataSet ();
DataAdapter.Fill(ds,"book_detail");
oDataSourse = ds.Tables [0].DefaultView ;DataTable dt = ds.Tables [0];
for(i = 0 ;i <=9 ;i++)
{
oDataRow[i] = dt.Rows[0][i];//第0行任意列!
}
Maybe help you!