巧用VB调用Excel实现复杂报表设计与打印

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

2011.151,“”,VisualBasic6.0+SP6、Mi-crosoftAccess2003MicrosoftExcel2003,VBExcel。2(1)VBVB,()VB。:Excel,VBExcelADOExcel。(2):,,,,?:,“”,。(3)Excel:(),(),VBExcel,?:,,SQL,ADO。33.1(Modbas)“MicrosoftActiveXDataObjects2.6Li-brary”“MicrosoftExcel11.0ObjectsLibrary”。PublicselrsidAsString'PublicselstrAsString'PubliccxrsAsNewADODB.Recordset'PublicrsbookmarkAsVariant'PublicconnAsNewADODB.Connection'ADOPublicSubmain()'conn.OpenProvider=Microsoft.Jet.OLEDB.4.0;DataSource='+App.Path+\rsdata.mdb+';PersistSecurityInfo=Falseconn.CursorLocation=adUseClient'EndSub3.2(Rsadmin)(1)(rstab)(msfgrid)1。(2):,“”“”。(3):PrivateSubForm_Load()'Callmain'Calldg1ref'Calltrview_Click'EndSubPrivateSubmsfgrid_Click()'selrsid=msfgrid.TextMatrix(msfgrid.Row,1)'cxrs.MoveFirst'cxrs.Findrsid=&selrsid&'rsbookmark=cxrs.Bookmark'EndSubPrivateSubtrview_Click()':“”,VBExcel。:ADO;VBExcel;1532011.15cxrs.Openselect*fromrsusertaborderbyrsid,conn,adOpenKeyset,adLockOptimisticIfcxrs.RecordCount0Thenmsfgrid.Enabled=TrueWithmsfgrid.Rows=1DoWhileNotcxrs.EOF.Rows=.Rows+1.TextMatrix(.Rows-1,0)=.Rows-1Fori=0Tocxrs.Fields.Count-1.Col=i+1IfIsNull(cxrs.Fields(i))Then'.TextMatrix(.Rows-1,i+1)=EmptyElse.TextMatrix(.Rows-1,i+1)=cxrs.Fields(i)'EndIfNexticxrs.MoveNextLoopEndWithElsemsfgrid.Enabled=FalseEndIfEndSub3.3、、(Mdfuser)(1)7(cmdsave,cmd1,cmd2,cmd3,cmd4,cmdprint,cmdquit)(frm1)2。(2):、、。(3):PrivateSubcmdprint_Click()'IfMsgBox(Excel2003,vbQuestion+vbYesNo,)=vbYesThenDimxlsAppAsExcel.Application'ExcelDimxlsBookAsExcel.Workbook'DimxlsSheetAsExcel.Worksheet'SetxlsApp=CreateObject(Excel.Application)'Excel'DimstrSource,strDestinationAsString'strSource=App.Path&\excel\.xls''.xlsstrDestination=App.Path&\Temp.xls'FileCopystrSource,strDestination','SetxlsBook=xlsApp.Workbooks.Open(strDestination)'SetxlsSheet=xlsBook.Worksheets(1)'xlsApp.Caption=VisualBasic6.0(Sp6)MicrosoftEx-cel2003'WithxlsSheet//:Cells:Cells(,).Cells(1,1)='.Cells(3,2)=cxrs.Fields(rsname)'.Cells(4,2)=cxrs.Fields(rssex).Cells(5,2)=cxrs.Fields(rsmix).Cells(6,2)=cxrs.Fields(rsmard).Cells(7,2)=cxrs.Fields(rstel).Cells(8,2)=cxrs.Fields(rsaddress).Cells(3,4)=cxrs.Fields(rsbrith).Cells(4,4)=cxrs.Fields(rsdisid).Cells(5,4)=cxrs.Fields(rseducation).Cells(6,4)=cxrs.Fields(rsgraduate).Cells(7,4)=cxrs.Fields(rsprof).Cells(10,2)=cxrs.Fields(rsdepartment).Cells(11,2)=cxrs.Fields(rsnewdate).Cells(10,6)=cxrs.Fields(rspost).Cells(11,4)=cxrs.Fields(sigcondate).Cells(10,4)=cxrs.Fields(rsprofess).Cells(11,6)=cxrs.Fields(termindate).Cells(12,2)=cxrs.Fields(rsresume)EndWithActiveWindow.View=xlPageBreakPreview'xlsApp.Visible=True'xlsSheet.PrintOutpreview:=True',xlsBook.Save'SetxlsBook=NothingSetxlsSheet=NothingSetxlsApp=Nothing'ElseExitSubEndIfEndSub2542011.153.4(Selectitem)(1)ListView,3。(2):“”,,。(3):Subview_Click()''ListViewDimsrsAsNewADODB.Recordsetsrs.Openselect*fromrsusertab,conn,adOpenKeyset,adLockReadOnlyFori=0Tosrs.Fields.Count-1ListView1.ListItems.Add,,change(srs.Fields(i).Name)'changeNextisrs.CloseSetsrs=NothingEndSubFunctionchange(ByValbtstrAsString)AsString''SelectCasebtstrCasersidchange=Casersnamechange=Caserssexchange=Casersmixchange=Caserszzmchange=Casersbrithchange=Casersoldchange=Casersmardchange=Casersdisidchange=Caserswdatechange=Caserswyearchange=Caserstelchange=Caserseducationchange=Casersgraduatechange=Casersprofchange=Casersaddresschange=Casersreguserchange=Casersdepartmentchange=Casersnewdatechange=Caserspostchange=Casersnewyearchange=Casersstatechange=Casersprofesschange=Casesigcondatechange=Casetermindatechange=Casersresumechange=EndSelectEndFunctionPublicSubcmddata_Click()'DimxlsAppAsExcel.ApplicationDimxlsBookAsExcel.WorkbookDimxlsSheetAsExcel.WorksheetDimi,j,nAsIntegerDims,strangeAsStringSetxlsApp=CreateObject(Excel.Application)'Excel'SetxlsBook=xlsApp.Workbooks.Add'SetxlsSheet=xlsBook.Worksheets(1)'//1ExcelxlsSheet.Cells(1,1)=selstrxlsSheet.Rows(1).RowHeight=25'xlsSheet.Rows(1).Font.Size=14'3552011.15//ExcelDimaaAsListItem's=:j=2'j=22ForEachaaInListView1.ListItems'ListViewIfaa.Checked=TrueThenaa.Selected=Truen=n+1'()s=s+unchange(aa.Text)&,''SxlsSheet.Cells(j,n)=aa.Text'EndIfNext//Excel3rs.Openselect&Left(Trim(s),Len(Trim(s))-1)&fromrsusertab,conn,adOpenKeyset,adLockOptimistic''Ifrs.RecordCount0ThenDoUntilrs.EOFFori=1Tors.Fields.CountIfrs.Fields(i-1).Type=adDateThen''xlsSheet.Cells(j+1,i)=Format(rs.Fields(i-1),yyyy-mm-dd)'ElsexlsSheet.Cells(j+1,i)=rs.Fields(i-1)EndIfxlsSheet.Cells(j+1,i).EntireColumn.AutoFit'xlsSheet.Cells(j+1,i).HorizontalAlignment=Center'Nextirs.MoveNextj=j+1Loopstrange=a1:&CellName(n)&1'//WithxlsSheet.Range(strange).SelectWith.Application.Selection.HorizontalAlignment=xlCenter.VerticalAlignment=xlCenterEndWith.Application.Selection.Merge'EndWithxlsSheet.PageSetup.PrintTitleRows=$1:$2''xlsSheet.PageSetup.RightFooter=:&Format(Now,yyyymmddhh:MM:ss)'xlsSheet.PageSetup.CenterHorizontally=True''ActiveWindow.View=xlPageBreakPreview''xlsApp.Visible=True'xlsSheet.PrintOutpreview:=True',xlsBook.SaveAsApp.Path&\.xlsSetxlsBook=NothingSetxlsSheet=NothingSetxlsApp=Nothing',EndIfrs.CloseSetrs=NothingEndSubPub

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功