在網路上查了很多資料 但偏偏都少了這一句

請先 引用 Micorsoft Excel 11.0 Object Libary
讓我 try 了好久 >_< 後面再加上 把 Northwind.sdf檔讀入 MSHFledGrid 中 在Form 中要建立 button MSHFlefGrid1 ADODC1 Textbox1 and Progress1 Private Sub btntra_Click() '******** '*匯出到 Excel '****************** Call PrintTableToExcel(Me.MSH, "", "", App.Path & "\" & txtOut.Text & ".xls", Me.ProgressBar1) End Sub Public Sub PrintTableToExcel(ByRef myGrid As MSHFlexGrid, ByVal strHeader As String, _ ByVal strInfo As String, ByVal strFileName As String, _ ByRef proBar As ProgressBar) '************* '*執行 匯出的動作 '********************* If myGrid.Rows <= 1 Then Exit Sub Dim ExcelApp As Excel.Application Dim r As Long, c As Long On Error Resume Next Set ExcelApp = GetObject(, "Excel.Application") If Err.Number <> 0 Then
Err.Clear
Set ExcelApp = CreateObject("Excel.application")
End If
Dim wsBook As Workbook
Dim wsSheet As Worksheet
With ExcelApp.AutoCorrect.Application
.WindowState = xlMinimized
.SheetsInNewWorkbook = 1
.Visible = False
.Workbooks.Add
Set wsBook = .ActiveWorkbook
Set wsSheet = .ActiveSheet
End With

proBar.Min = 0
proBar.Max = myGrid.Rows
proBar.Value = 0
proBar.Visible = True

With wsSheet
.Cells.Font.Name = "System"
.Cells.Font.Size = 12
.Name = "Page1"

Range(Cells(1, 1), Cells(1, myGrid.Cols)).Select
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.Merge '合併表格
'.Cells(1, 1) = strHeader

Range(Cells(2, 1), Cells(2, myGrid.Cols)).Select
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.Merge '合併表格
'.Cells(2, 1) = strInfo

'開始複製到 Excel
For r = 0 To myGrid.Rows - 1
For c = 0 To myGrid.Cols - 1
.Cells(r + 3, c + 1) = myGrid.TextMatrix(r, c)
Next
On Error Resume Next
proBar.Value = r + 1
If Err.Number <> 0 Then Err.Clear
Next


Range(.Cells(3, 1), .Cells(myGrid.Rows + 2, myGrid.Cols)).Select

With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlWide
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlWide
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlWide
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlWide
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With

proBar.Visible = False

ExcelApp.AutoCorrect.Application.DisplayAlerts = True
Call ExcelApp.AutoCorrect.Application.ActiveWorkbook.SaveAs(strFileName)
ExcelApp.Quit
End Sub







Private Sub Form_Load()
'*********************
'*將 Northwind.sdf 轉入 MSHfixgrid
'*************************
Dim sqlfile As String
Dim con As New ADODB.Connection

sqlfile = App.Path & "\northwind.sdf"

With con
.Provider = "Microsoft.SQLSERVER.CE.OLEDB.3.5"
.Open sqlfile
End With

Adodc1.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=" & App.Path & "\northwind.sdf"
Adodc1.RecordSource = "select * from employees"
Adodc1.Refresh
Set MSH.DataSource = Adodc1



End Sub