'把MSHFlexGrid的数据导到EXCELPublic Function GridToExcel(HFGrid As MSHFlexGrid) Dim Irowcount As Integer Dim Icolcount As Integer Dim i As Integer Dim j As Integer
Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet
If HFGrid.Rows = 1 Then MsgBox "没有要导出的数据" Exit Function Else Irowcount = HFGrid.Rows Icolcount = HFGrid.Cols End If
Set xlApp = CreateObject("Excel.Application") Set xlBook = Nothing Set xlSheet = Nothing Set xlBook = xlApp.Workbooks().Add Set xlSheet = xlBook.Worksheets("sheet1") For i = 1 To Irowcount For j = 1 To Icolcount xlSheet.Cells(i, j) = HFGrid.TextMatrix(i - 1, j - 1) Next j Next i
xlApp.Visible = True With xlSheet .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体" '设标题为黑体字 .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True '标题字体加粗 .Range(.Cells(1, 1), .Cells(Irowcount, Icolcount)).Borders.LineStyle = xlContinuous '设表格边框样式 End With
xlApp.Application.Visible = True Set xlApp = Nothing '"交还控制给Excel Set xlBook = Nothing Set xlSheet = Nothing
向单元格插入数据"43" 引用Execel对象 Dim xlApp As Excel.Application xlApp = New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet xlApp = CreateObject("Excel.Application") xlApp.Visible = False xlBook = xlApp.Workbooks.Open(文件路径) xlSheet = xlBook.Worksheets(1) xlSheet.Cells(4, 3) = "43" xlBook.Save() xlApp.Quit()
sorry,我这个是从VB.NET代码里摘出来的,可能有不一样的地方 在VB6里是要set xlApp = New Excel.Application吧?很久不用,不太记得了,不好意思
viena(晓琴) 不好意思,真的不行
怎么不行啊?具体点吗,添加引用了吗?菜单:工程-引用-Microsoft Excel X.0 Object Library(X和你的机器的装的office版本有关,假设OFFICE2000是9)向单元格插入数据"43" 引用Execel对象 Dim xlApp As Excel.Application set xlApp = New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet set xlApp = CreateObject("Excel.Application") xlApp.Visible = False set xlBook = xlApp.Workbooks.Open(文件路径) set xlSheet = xlBook.Worksheets(1) xlSheet.Cells(4, 3) = "43" xlBook.Save() xlApp.Quit() Set xlApp = Nothing Set xlBook = Nothing Set xlSheet = Nothing这样呢?
Dim Irowcount As Integer
Dim Icolcount As Integer
Dim i As Integer
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
If HFGrid.Rows = 1 Then
MsgBox "没有要导出的数据"
Exit Function
Else
Irowcount = HFGrid.Rows
Icolcount = HFGrid.Cols
End If
Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
For i = 1 To Irowcount
For j = 1 To Icolcount
xlSheet.Cells(i, j) = HFGrid.TextMatrix(i - 1, j - 1)
Next j
Next i
xlApp.Visible = True With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount, Icolcount)).Borders.LineStyle = xlContinuous
'设表格边框样式
End With
xlApp.Application.Visible = True
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Function
引用Execel对象
Dim xlApp As Excel.Application
xlApp = New Excel.Application Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(文件路径)
xlSheet = xlBook.Worksheets(1) xlSheet.Cells(4, 3) = "43" xlBook.Save()
xlApp.Quit()
在VB6里是要set xlApp = New Excel.Application吧?很久不用,不太记得了,不好意思
引用Execel对象
Dim xlApp As Excel.Application
set xlApp = New Excel.Application Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
set xlBook = xlApp.Workbooks.Open(文件路径)
set xlSheet = xlBook.Worksheets(1) xlSheet.Cells(4, 3) = "43" xlBook.Save()
xlApp.Quit() Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing这样呢?
xlApp.Quit()在VB中应该是 xlBook.Save
xlApp.Quit