| 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 说明: 为什么使用这种方法而不是其它更快速的方法 |