export grid to excel fast and wyswyg

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 progame 的 blog

export grid to excel quickly and wyswyg 2003-12-28 11:50:28.89

'**
'export grid to excel
private sub exportexcel(grid as editgridctrllib.editgridctrl)
dim xlapp       as object       '*excel.application    '
dim xlbook      as object       '*excel.workbook       '
dim xlsheet     as object       '*excel.worksheet      '
dim cx          as long
dim data()      as string
dim cnt         as integer      ' visible column's count
dim curcol      as long
dim i           as integer
dim j           as integer
    ' if no column need output,exit
    with grid
        cnt = 0
        for i = 0 to .cols - 1
            if .colwidth(i) < 0 or .colwidth(i) > 50 then
                cnt = cnt + 1
            end if
        next i
    end with
   
    if cnt = 0 then
        exit sub
    end if
   
    cx = getdevicecaps(me.hdc, logpixelsy)
   
    g_utility.waiterbegin
   
    on error goto err_proc
   
    set xlapp = createobject("excel.application")
    set xlbook = xlapp.workbooks.add
    set xlsheet = xlbook.worksheets(1)
       
    xlapp.screenupdating = false
    ' begin to fill
    with me.grdlist
        redim data(.rows - 1, cnt - 1)
       
        curcol = 0
       
        for i = 0 to .cols - 1
       
            if .colwidth(i) < 0 or .colwidth(i) > 50 then
                for j = 0 to .rows - 1
                    data(j, curcol) = .textmatrix(j, i)
                next j
               
                xlsheet.columns(curcol + 1).select
               
                if fix(.colalignment(i) / 3) = 0 then
                    xlapp.selection.horizontalalignment = -4131 ' xlleft
                end if
               
                if fix(.colalignment(i) / 3) = 1 then
                    xlapp.selection.horizontalalignment = -4108 ' xlcenter
                end if
               
                if fix(.colalignment(i) / 3) = 2 then
                    xlapp.selection.horizontalalignment = -4152 ' xlright
                end if
               
                ' resize column width
                xlsheet.columns(curcol + 1).columnwidth = .colwidth(clng(i)) / cx
               
                curcol = curcol + 1
            end if
           
        next i
    end with
   
    with xlsheet
        .range(.cells(1, 1), .cells(me.grdlist.rows, cnt)).value = data
    end with
   
    ' colheader align center
    xlsheet.rows(1).select
    xlapp.selection.horizontalalignment = -4108 ' xlcenter
    xlapp.activesheet.pagesetup.printgridlines = true
   
    if me.grdlist.fixedrows > 0 then
        xlapp.activesheet.pagesetup.printtitlerows = xlsheet.rows(me.grdlist.fixedrows).address
    end if
   
    if me.grdlist.fixedcols > 0 then
        xlapp.activesheet.pagesetup.printtitlecolumns = xlsheet.columns(me.grdlist.fixedcols).address
    end if
   
    xlapp.screenupdating = true
    xlapp.visible = true
    xlapp.activeworkbook.printpreview
    xlapp.displayalerts = false
    xlapp.activeworkbook.close false
    xlapp.displayalerts = true
   
    xlapp.quit
   
    set xlapp = nothing
    set xlbook = nothing
    set xlsheet = nothing
   
    g_utility.waitend
   
    exit sub
   
err_proc:
    g_utility.waitend
    if not xlapp is nothing then
        xlapp.quit
        set xlapp = nothing
    end if
    g_errlog.showmessage err
   
end sub

说明:
1、使用range.value一次性填充数据,可以极大地加快速度
2、screenupdating 设为false可以加快速度
3、对不可见列不打印,并且根据grid来设置对齐方式
4、迟绑定可以减少去excel版本的依赖性

为什么使用这种方法而不是其它更快速的方法
1、copyformrecordset
   这样的话就需要一个ado的结果集才可以操作,而且对列的对齐、列头文本、不可见列的操作都无法进行
2、querytable
   由于在三层开发中客户端并没有办法直接访问数据库,同时它还存在着和上面一样的缺陷
3、bcp
   这个是最快的了 可是局限性同上

本文关键:excel grid export report wyswyg
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top