| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 605 人关注过本帖
标题:关于ADO的使用~
只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
结帖率:97.66%
收藏
已结贴  问题点数:100 回复次数:7 
关于ADO的使用~
目前我程序中某一小块的SUB程式码如下~
请教高手要如何改写成ADO的方式做到同样的功能呢?
程序代码:
Private Sub WriteDataToExcel(Target As String)
Dim xlApp As EXCEL.Application, xlBook As EXCEL.Workbook, xlsheet As EXCEL.Worksheet
Dim i As Integer, j As Integer, k As Integer, StartNum As Integer, StartNum1 As Integer
Dim TempString As String, Temp() As String, CodeString As String, Text As String
Dim AllFailCount1 As Integer, AllFailCount2 As Integer, FailCount1() As Integer, FailCount2() As Integer, iCount As Integer

On Error GoTo ErrorHandling

    Set xlApp = CreateObject("Excel.Application")
'    xlApp.Visible = True
    xlApp.Visible = False
    xlApp.DisplayAlerts = False                     '把Excel的警告訊息關掉
    Set xlBook = xlApp.Workbooks.Add
    Set xlsheet = xlBook.Sheets(1)
    xlsheet.Activate
    xlsheet.Cells.HorizontalAlignment = xlCenter

    StartNum = 1                                    '
    StartNum1 = 1                                   '
    iCount = 2
    
    ReDim FailCount1(AllIC - 1): ReDim FailCount2(AllIC - 1)
    AllFailCount1 = 0: AllFailCount2 = 0

    With xlsheet

        .Select
        .Cells.Font.Name = "Tahoma"                  '設定字型
        .Cells.Font.Size = 12                       '設定字體大小
        .Cells.Borders.LineStyle = xlContinuous
        .Cells.Borders.Weight = xlThin              '設定儲存格間框線粗細
        .Cells.Borders.ColorIndex = 15              '設定儲存格框線顏色

        xlApp.ActiveWindow.Zoom = 75                '設定縮放大小

        For i = 0 To UBound(DataBase.E_SubList) + 2
            If i < 2 Then Call PictureBorder(StartNum + i, StartNum1 + 0, StartNum + i, AllIC * 2 + 3, xlsheet)
            For j = 0 To 2 + AllIC * 2
                If i = 0 Then
                    With .Range(.Cells(StartNum + i, StartNum1 + 0), .Cells(StartNum + i, AllIC * 2 + 3))
                        If j = 0 Then
                            xlsheet.Cells(StartNum + i, StartNum1 + j) = "Sub"
                        ElseIf j = 1 Then
                            xlsheet.Cells(StartNum + i, StartNum1 + j) = "Hardware Bin"
                        ElseIf j = 2 Then
                            xlsheet.Cells(StartNum + i, StartNum1 + j) = "Soft Bin"
                        ElseIf j > 2 Then
                            If j Mod 2 = 1 Then
                                xlsheet.Cells(StartNum + i, StartNum1 + j + 0) = "#" & (j - iCount)
                                iCount = iCount + 1
                            ElseIf j Mod 2 = 0 Then
                                xlsheet.Range(xlsheet.Cells(StartNum + i, StartNum1 + j - 1), xlsheet.Cells(StartNum + i, StartNum1 + j)).Merge
                            End If
                        End If
                    End With
                ElseIf i = 1 Then
                    With .Range(.Cells(StartNum + i, StartNum1 + 0), .Cells(StartNum + i, AllIC * 2 + 3))
                        .Select
                        .Interior.Color = RGB(153, 204, 255)
                        .Font.Bold = True
                        If j = 2 Then
                            xlsheet.Range(xlsheet.Cells(StartNum + i, StartNum1 + j - 2), xlsheet.Cells(StartNum + i, StartNum1 + j)).Merge
                        ElseIf j > 2 Then
                            If j Mod 2 = 1 Then
                                .Cells(StartNum, StartNum1 + j) = "AAA"
                            ElseIf j Mod 2 = 0 Then
                                .Cells(StartNum, StartNum1 + j) = "BBB"
                            End If
                        End If
                    End With
                Else
                    If j = 0 Then
                        If i - 2 <= UBound(DataBase.E_SubList) Then
                            .Cells(StartNum + i, StartNum1 + j) = Trim(Mid(DataBase.E_SubList(i - 2).SubName, 1, InStr(DataBase.E_SubList(i - 2).SubName, ",") - 1))
                            Call PictureBorder(StartNum + i, StartNum1 + j, StartNum + i, StartNum1 + j, xlsheet)
                        End If
                    ElseIf j = 1 Then
                        .Cells(StartNum + i, StartNum1 + j) = Trim(Mid(DataBase.E_SubList(i - 2).SubName, InStr(DataBase.E_SubList(i - 2).SubName, ",") + 1, InStrRev(DataBase.E_SubList(i - 2).SubName, ",") - InStr(DataBase.E_SubList(i - 2).SubName, ",") - 1))
                        Call PictureBorder(StartNum + i, StartNum1 + j, StartNum + i, StartNum1 + j, xlsheet)
                    ElseIf j = 2 Then
                        .Cells(StartNum + i, StartNum1 + j) = Trim(Mid(DataBase.E_SubList(i - 2).SubName, InStrRev(DataBase.E_SubList(i - 2).SubName, ",") + 1))
                        Call PictureBorder(StartNum + i, StartNum1 + j, StartNum + i, StartNum1 + j, xlsheet)
                    ElseIf j > 2 Then
                        If j Mod 2 = 1 Then
                            .Cells(StartNum + i, StartNum1 + j) = DataBase.E_SubList(i - 2).E_ICList((j \ 2) - 1)
                            If DataBase.E_SubList(i - 2).E_ICList((j \ 2) - 1) = "1" Then FailCount1((j \ 2) - 1) = FailCount1((j \ 2) - 1) + 1
                            Text = "123" & Chr(10) & "456"
                            Call CellsWriteComment(StartNum + i, StartNum1 + j, StartNum + i, StartNum1 + j, Text, xlsheet)
                            .Cells(StartNum + i, StartNum1 + j + 1) = DataBase.E_SubList(i - 2).F_ICList((j \ 2) - 1)
                            If DataBase.E_SubList(i - 2).F_ICList((j \ 2) - 1) = "1" Then FailCount2((j \ 2) - 1) = FailCount2((j \ 2) - 1) + 1
                            Text = "789" & Chr(10) & "456"
                            Call CellsWriteComment(StartNum + i, StartNum1 + j + 1, StartNum + i, StartNum1 + j + 1, Text, xlsheet)
                        End If
                    End If
                End If
                MyDoEvents
            Next j
            MyDoEvents
        Next i
        
        j = i
        .Cells(StartNum + j + 1, StartNum1 + 0) = "Total Fail"
        
        For i = 0 To UBound(FailCount1)
            .Cells(StartNum + j + 1, StartNum1 + 3 + i * 2) = FailCount1(i)
            .Cells(StartNum + j + 1, StartNum1 + 3 + i * 2 + 1) = FailCount2(i)
            AllFailCount1 = AllFailCount1 + FailCount1(i)
            AllFailCount2 = AllFailCount2 + FailCount2(i)
        Next i
        
        .Cells(StartNum + j + 3, StartNum1 + 0) = "Sum"
        .Cells(StartNum + j + 3, StartNum1 + 1) = AllFailCount1
        .Cells(StartNum + j + 3, StartNum1 + 2) = AllFailCount2
        
        .Cells(StartNum + j + 5, StartNum1 + 0) = "Note : "
        .Cells(StartNum + j + 5, StartNum1 + 1) = "0->Pass"
        .Cells(StartNum + j + 5, StartNum1 + 2) = "1->Fail"
        .Cells(StartNum + j + 5, StartNum1 + 3) = "0->None"
        
        .Range("D3").Select
        ActiveWindow.FreezePanes = True
        
        .Columns("A:A").HorizontalAlignment = xlGeneral
        .Columns("A:A").VerticalAlignment = xlCenter
        .Columns("A:A").ColumnWidth = 43.13
        .Columns("B:B").ColumnWidth = 13.88
        .Columns("C:C").ColumnWidth = 13.88
        
    End With
    
    If IsFolderExist(txtTargetPath.Text) = False Then MkDir txtTargetPath.Text
    If IsFileExist(Target) = True Then Target = Mid(Target, 1, InStrRev(Target, ".") - 1) & "_" & Format(Now, "yyyymmddhhmmss") & ".xls"
    
    Set xlsheet = Nothing
    xlBook.SaveAs (Target)
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    
Exit Sub

ErrorHandling:
    Call ErrorWriteBuff(Text1.Text, CLng(i), "WriteDataToExcel", Err.Number, Err.Description, "")
    Resume Next
End Sub

Private Sub CellsWriteComment(Start1 As Integer, End1 As Integer, Start2 As Integer, End2 As Integer, Comment As String, SheetObject As EXCEL.Worksheet)
    
On Error GoTo ErrorHandling
    
    With SheetObject
        With .Range(.Cells(Start1, End1), .Cells(Start2, End2))
            .Select
            .AddComment
            .Comment.Visible = False
            .Comment.Text Text:=Comment
        End With
    End With
    
Exit Sub

ErrorHandling:
    Call ErrorWriteBuff(Text1.Text, 0, "CellsWriteComment", Err.Number, Err.Description, "")
    Resume Next
End Sub

Private Sub PictureBorder(Start1 As Integer, End1 As Integer, Start2 As Integer, End2 As Integer, SheetObject As EXCEL.Worksheet)

On Error GoTo ErrorHandling

    With SheetObject
    
        With .Range(.Cells(Start1, End1), .Cells(Start2, End2))
            
            .Select
            .Interior.Color = RGB(153, 204, 255)
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            
            .Borders(xlDiagonalDown).LineStyle = xlNone
            .Borders(xlDiagonalUp).LineStyle = xlNone
        
            With .Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With .Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With .Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With .Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With .Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With .Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
        
        End With
        
    End With
    
Exit Sub

ErrorHandling:
    Call ErrorWriteBuff(Text1.Text, 0, "PictureBorder", Err.Number, Err.Description, "")
    Resume Next
End Sub


[ 本帖最后由 wube 于 2011-9-14 11:12 编辑 ]
搜索更多相关主题的帖子: color 
2011-09-14 11:10
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
收藏
得分:50 
图片附件: 游客没有浏览图片的权限,请 登录注册

无知
2011-09-15 00:15
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:50 
.Cells.Font.Name = "Tahoma"                  '設定字型
        .Cells.Font.Size = 12                       '設定字體大小
        .Cells.Borders.LineStyle = xlContinuous
        .Cells.Borders.Weight = xlThin              '設定儲存格間框線粗細
        .Cells.Borders.ColorIndex = 15              '設定儲存格框線顏色

        xlApp.ActiveWindow.Zoom = 75                '設定縮放大小

这段类似的代码,我想不出来。
ADO 方式,好像只能读 写 数据,不能设置单元格 属性。

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-15 08:03
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
意思就是需得分两种模式达到相同的功能?主要是因为这样写执行起来速度真的很慢。

因为是用2007,所以不考虑横向256的极限问题,预想最少要预想每个表可能会有
10000* 200的格子要填值,加上每个格子还都要填上资料来源的注解,所以等于有
10000* 200*2笔资料要输入,而另外还需设定的EXCEL的VBA的处理,框线粗细,字
型种类,冻结窗框,格子宽度,数个格子合并,列靠左或至中....等设定,甚至还
要在加上搜寻的功能和图表,全都要用VB6中跑出来,而这些要同时做在同一个EXCEL
的档案里的数个表中,不知道有何方法能提升档案产生效率?

填值的部分的ADO我想我可以查得到很多范例程式码,只是没用过所以不清楚的ADO的
极限在哪。

若如上楼所言,ADO能做到的就像CSV能做到的一样吗,因为CSV对于EXCEL来说也是只
能填值,无法对储存格做额外处理,文字处理之外的都无法用?那格子的注解部分也
是无法使用的ADO来解决吗?

或是有其他更好的方法可以一次解决我这种需求呢?

[ 本帖最后由 wube 于 2011-9-15 12:27 编辑 ]

不要選我當版主
2011-09-15 12:25
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
老实说产出的EXCEL档案这段只是很小的一部分,来源资料是不小的资料档预先
汇整处理,透过不少自订结构,这段只是很小的一段,我想表达的是,前面那
段数千行包含几个类,几个模块,几个表,执行后的速度~感觉比最后这几行
还快得多,所以直觉认为这段写法上应该是有问题的吧?

不要選我當版主
2011-09-15 12:34
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
填写 EXCEL 时,我感觉 速度慢的地方就是设置 格式。

设置格式,需要不断的设置属性,
在VB6中,访问控件的属性是 慢速操作 ,在 EXCEL 中,我认为也是差不多的。

至于填值为什么更快,我觉得 填值 后 EXCEL 的处理没有 设置格式 所需要处理的东西多而显得更快一点。
EXCEL 里还有一个 消耗处理时间的地方就是 公式 ,越多的公式,越多的 互相引用,会造成 EXCEL 的严重性能下降。

ADO 填值,我认为是根本不去设置 属性,而全是 EXCEL 的默认属性。

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-15 15:59
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
所以我才坚持不直接使用VBA,而都使用VB6去完成所有工作,否则先前我也试过把VBA的代码先在VB6的工程内
编完,之后产生EXCEL档时,再把预先编好的VBA代码全写到该EXCEL里面,连EXCEL上的按钮....等控制项都是
使用VB6的去让EXCEL自动生成,包含Form或模块或类都有方法可以指定产生,还看过有高手连EXCEL菜单之类的
UI介面都能由VB6编代码去控制自动生成。

不然同事们都是直接使用XLT去当鏁设计范本,再去让它产生新的XLS,只是这样虽然简单些,但是代码要更改
时一次要改两边,而且弹性太差,一些VBA中用到的代码变量值无法随时做动态变更。

P.S 不过也得要有开信任设置,微软防止VBA的太强大的防护锁。

不要選我當版主
2011-09-15 19:05
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
所以总结来说,先使用ADO的填值增加效率,最后才用的COM去操作EXCEL内的VBA做美化动作。

不要選我當版主
2011-09-15 19:08
快速回复:关于ADO的使用~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.062090 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved