建议使用数据库
datagrid导出代码如下
Private Sub Command7_Click()
Dim excel1 As Excel.Application
Set excel1 = New Excel.Application
excel1.Visible = True
excel1.Workbooks.Open (App.Path & "\程序模版.xls")
excel1.Workbooks("程序模版.xls").Sheets("确认对账结果").Select
If excel1.Workbooks("程序模版.xls").ReadOnly = True Then
excel1.DisplayAlerts = False
excel1.Application.Quit
excel1.DisplayAlerts = True
MsgBox "你已经打开“程序模版.xls”" & Chr(13) & "请关闭原有的“程序模版.xls”,在运行导出到excel。", vbOKOnly, "错误提示"
Exit Sub
Else
excel1.Workbooks("程序模版.xls").Sheets("确认对账结果").Cells.Clear
excel1.Workbooks("程序模版.xls").Sheets("确认对账结果").Range("A2").CopyFromRecordset Adodc1.Recordset
With excel1.Workbooks("程序模版.xls").Sheets("确认对账结果")
.Cells(1, 1) = "地域"
.Cells(1, 2) = "所属公司"
.Cells(1, 3) = "酒店名称"
.Cells(1, 4) = "期间总金额"
.Cells(1, 5) = "入账日期"
.Cells(1, 6) = "确认结果"
.Cells.Font.Size = 9
.Cells.Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns.AutoFit
.Rows.AutoFit
.Columns("d:d").NumberFormatLocal = "¥#,##0.00;¥-#,##0.00"
.Columns("e:e").NumberFormatLocal = "yyyy-mm""月"""
End With
End If
End Sub