资源描述:
《VB操作Excel实现读取写入打印》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、VB操作EXCEL,实现数据写入以及数据组合1】定义EXCEL的一系列操作DimxlAppAsExcel.ApplicationDimExcelShowStrAsStringSetxlApp=CreateObject("Excel.Application")xlApp.Visible=trueAppLogFile.WriteLine(CStr(Date)&"_"&CStr(Time)&":SetxlApp=newExcel.Application")DimxlBookAsWorkbookDimxlSheetAsWorksheetDimxlrow,sheetIndex,sheetColumnA
2、sInteger'fileurl="D:VB英迈TemplateWriteData.xls"Editable=空值SetxlBook=xlApp.Workbooks.Open(fileUrl,Editable)'打开EXCEL模板SetxlSheet=xlApp.ActiveSheet--------------------------------------------------------------------------------------------【2】写入数据的操作xlSheet.Range(xlSheet.Cells(beginRow,1),xlSheet.Cel
3、ls(endRow-1,1)).Merge'合并单元格xlSheet.Cells(beginRow,1).FormulaR1C1=CustName'单行插入xlSheet.Cells(beginRow,1).VerticalAlignment=xlTop'垂直,写入数据的位置,这里为高xlSheet.Cells(beginRow,2).HorizontalAlignment=xlHAlignCenter'水平写入数据的位置,这里为居中xlSheet.Range(xlSheet.Cells(beginRow,1),xlSheet.Cells(endRow-1,19)).Font.ColorIn
4、dex=ConstModule.COLOR_BLUE'单元格的字体颜色xlSheet.Range(xlSheet.Cells(beginRow,1),xlSheet.Cells(endRow-1,19)).Font.Bold=True'单元格的数据为粗体显示xlSheet.Range(xlSheet.Cells(beginRow,1),xlSheet.Cells(endRow-1,19)).Interior.ColorIndex=ConstModule.COLOR_SILVER'单元格的背景颜色定义数组,一次性写入,列子如下:IfDetailRes.RecordCount>0ThenDeta
5、ilRes.MoveFirstForrow2=0ToDetailRes.RecordCount-1DimarrayProduct(50,17)AsStringarrayProduct(row2,0)=DetailRes.Fields("Product")arrayProduct(row2,1)=DetailRes.Fields("rev")arrayProduct(row2,2)=DetailRes.Fields("sagm")arrayProduct(row2,3)=DetailRes.Fields("sagm_per")&"%"arrayProduct(row2,4)=DetailRes
6、.Fields("gp")arrayProduct(row2,5)=DetailRes.Fields("gp_per")&"%"arrayProduct(row2,6)=DetailRes.Fields("opex")arrayProduct(row2,7)=DetailRes.Fields("opex_per")&"%"arrayProduct(row2,8)=DetailRes.Fields("oper_profit")arrayProduct(row2,9)=DetailRes.Fields("oper_profit_per")&"%"arrayProduct(row2,10)=Det
7、ailRes.Fields("dio")arrayProduct(row2,11)=DetailRes.Fields("dpo")arrayProduct(row2,12)=DetailRes.Fields("dso")arrayProduct(row2,13)=DetailRes.Fields("working_capital")arrayProduct(row2,14)=DetailRes.Fields(