帮我看一下datagrid控件导出到execl的详细代码???多谢!
Private Sub Command1_Click()
Dim objFileSystem As Object
Dim objExcelText As Object
Dim strTableString As String, i As Integer, strFileName As String
Dim pubConn As New adodb.Connection
Dim rsTable As New adodb.Recordset
Dim strConn As String
Dim strSQL As String
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=develop; password=12345;Data Source=ServerNmae "
pubConn.Open strConn
rsTable.CursorLocation = adUseClient
strSQL = "select top 10 * from gate_register "
rsTable.Open strSQL, pubConn, adOpenDynamic, adLockOptimistic
For i = 0 To rsTable.Fields.Count - 1
strTableString = strTableString & rsTable.Fields(i).Name & Chr(9) '獲取字段名
Next
strTableString = strTableString & rsTable.GetString '字段名+數據庫的記錄
cmDialog.CancelError = False
cmDialog.FileName = "FileName " '默認生成的文件名
cmDialog.DialogTitle = "Save Export File "
cmDialog.Filter = "Excel (*.xls)|*.xls|文本文件(*.DBF)|*.DBF|檔案文件(*.doc)|*.doc|所有文件(*.*)|*.* "
cmDialog.DefaultExt = "*.xls "
cmDialog.ShowSave
strFileName = cmDialog.FileName
Set objFileSystem = CreateObject( "Scripting.FileSystemObject ")
Set objExcelText = objFileSystem.createtextfile(strFileName, True)
objExcelText.writeline (strTableString)
objExcelText.Close
Set objFileSystem = Nothing
[解决办法]
谢谢,接分