【VB6.0 数据库连接EXCEL查询合并单元格成功解决办法】
Private Sub Command1_Click()On Error Resume NextDim i As Integer, j As Integerfilestr = App.Path & "\" & " 123.xls" ‘设置文件位置字符串ExcelConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filestr & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1""" ’建立连接字符串ExcelRecordSet.Open "select * from " & "[" & "Sheet1" & "$" & "]""", ExcelConnection ‘打开数据库arr1 = ExcelRecordSet.GetRows(-1) ’用数组接收结果记录集MSHFlexGrid1.Cols = UBound(arr1, 1) ‘把数组第一维最大值赋值给MSHFLEXGRID列的最大数量MSHFlexGrid1.Rows = UBound(arr1, 2)‘把数组第二维最大值赋值给MSHFLEXGRID行的最大数量For i = 0 To UBound(arr1, 1) - 1For j = 0 To UBound(arr1, 2) - 1If IsNull(arr1(i, j)) Then arr1(i, j) = "" ’处理数组元素如果为空值则赋值成空字符串MSHFlexGrid1.TextMatrix(j, i) = arr1(i, j) ‘用循环赋值把处理过的数组赋值给表格控件MSHFLEXGRID1Next jNext iExcelRecordSet.Close '关闭数据查询结果记录集ExcelConnection.Close ’ 关闭数据库连接Set ExcelRecordSet = Nothing ‘清除占用内存Set ExcelConnection = Nothing ’清除占用内存For i = 1 To MSHFlexGrid1.Rows - 1If MSHFlexGrid1.TextMatrix(i, 0) = "合计" Then xx = i '找MSHFLEXGRID1中”合计“所在行msgbox "合并单元格读取要用数组接收结果记录集的行数据GETROWS(-1),okey"Next iend sub