VB操作Excel报表实例图例源码注释.docx
- 文档编号:25089886
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:7
- 大小:75.18KB
VB操作Excel报表实例图例源码注释.docx
《VB操作Excel报表实例图例源码注释.docx》由会员分享,可在线阅读,更多相关《VB操作Excel报表实例图例源码注释.docx(7页珍藏版)》请在冰豆网上搜索。
VB操作Excel报表实例图例源码注释
VB操作Excel报表(实例、图例、源码、注释)
用Excel报表非常方便,而用VB操作Excel更能延长VB的功能范围。
对于重复生成的表格更是非常方便。
字体,行高,列宽,合并单元格,排版等功能都有。
供大家学习研究。
VB中使用Excel控件要先引用哦。
要不你错都不知道哪错了。
郁闷吧!
一、报表预览如下
1、无内容报表如下:
2、填表后预览如下:
二、界面预览如下:
打印记录按钮为Command3
三、源码如下:
PrivateSubCommand3_Click()
声明一个新的Excel对象
Setwinkexcel=NewExcel.Application
该对象可见
winkexcel.Visible=True
选择第一张表为操作表
winkexcel.SheetsInNewWorkbook=1
表添加内容
Setwinkworkbook=winkexcel.Workbooks.Add
设置指定单元格行高列宽
Setxlsheet=winkworkbook.Worksheets
(1)
xlsheet.Rows
(1).RowHeight=45
xlsheet.Rows
(2).RowHeight=33
xlsheet.Rows(3).RowHeight=33
xlsheet.Rows(4).RowHeight=33
xlsheet.Rows(5).RowHeight=33
xlsheet.Rows(6).RowHeight=300
xlsheet.Rows(7).RowHeight=100
xlsheet.Rows(8).RowHeight=45
xlsheet.Rows(9).RowHeight=45
xlsheet.Rows(10).RowHeight=26
xlsheet.Columns
(1).ColumnWidth=14
xlsheet.Columns
(2).ColumnWidth=24
xlsheet.Columns(3).ColumnWidth=14
xlsheet.Columns(4).ColumnWidth=24
设置表格内容的对齐方式
winkexcel.ActiveSheet.Rows.HorizontalAlignment=xlVAlignCenter
'水平居中
winkexcel.ActiveSheet.Rows.VerticalAlignment=xlVAlignCenter
'垂直居中
Withwinkexcel.ActiveSheet.Range("A1:
D1")
'合并单元格
.Merge
EndWith
Withwinkexcel.ActiveSheet.Range("B6:
D6")
'合并单元格
.Merge
EndWith
Withwinkexcel.ActiveSheet.Range("B7:
D7")
'合并单元格
.Merge
EndWith
画边框线,细线,黑。
Withwinkexcel.ActiveSheet.Range("A3:
D9").Borders '边框设置
.LineStyle=xlBorderLineStyleContinuous
.Weight=xlThin
.ColorIndex=1
EndWith
为单元格添加内容,请注意数字索引顺序。
行在前,列再后。
Withwinkexcel.ActiveSheet
.Cells(2,1).Value="检修部位:
" '第1行第2列
.Cells(2,4).Value="编号:
" '第2行第2列
.Cells(3,1).Value="设备型号"
.Cells(3,3).Value="入库检修时间"
.Cells(4,1).Value="设备编号"
.Cells(4,3).Value="检修完工时间"
.Cells(5,1).Value="修 程"
.Cells(5,3).Value="检修单位"
.Cells(6,1).Value="检修主要项目"
.Cells(7,1).Value="更换主要部件"
注意添加空格,使自动换行后内容都居中。
.Cells(8,1).Value="检修班长 (签字)"
.Cells(8,3).Value="检修人 (签字)"
.Cells(9,1).Value="主管部长 (签字)"
.Cells(9,3).Value="验收员 (签字)"
.Cells(10,1).Value="铁路运输部"
.Cells(3,4).Value=DTPicker1.Value
.Cells(4,4).Value=DTPicker2.Value
.Cells(10,4).Value=DTPicker3.Value
.Cells(2,2).Value=""+Combo7.Text
.Cells(2,4).Value="编号:
"+Text2.Text
.Cells(3,2).Value=""+Text3.Text
.Cells(4,2).Value=""+Text1.Text
.Cells(5,2).Value=""+Text4.Text
.Cells(5,4).Value=""+Combo6.Text
.Range("A1:
D1")="内燃机车检修验收单" '这个区域的值
.Range("B6:
D6")=""+Text7.Text
.Range("B7:
D7")=""+Text8.Text
EndWith
Withwinkexcel.ActiveSheet
设置自动换行
.Cells(6,1).WrapText=True
设置文字方向为竖版
.Cells(6,1).Orientation=xlVertical
.Cells(7,1).WrapText=True
.Cells(7,1).Orientation=xlVertical
.Cells(8,1).WrapText=True
.Cells(9,1).WrapText=True
.Cells(8,3).WrapText=True
.Cells(9,3).WrapText=True
EndWith
设置范围单元格内的字体
Withwinkexcel.ActiveSheet.Range("A1:
D1").Font '字体设置
.Bold=True
.Size=20
.ColorIndex=1
EndWith
Withwinkexcel.ActiveSheet.Range("A2:
D10").Font '字体设置
.Size=12
.ColorIndex=1
EndWith
设置上下左右边距。
winkexcel.ActiveSheet.PageSetup.TopMargin=2/0.035
winkexcel.ActiveSheet.PageSetup.BottomMargin=2/0.035
winkexcel.ActiveSheet.PageSetup.LeftMargin=1.4/0.035
winkexcel.ActiveSheet.PageSetup.RightMargin=1.4/0.035
设置页眉页脚距离
winkexcel.ActiveSheet.PageSetup.CenterHorizontally=2/0.035
' winkexcel.ActiveSheet.PageSetup.CenterVertically=2/0.035
Setwinkexcel=Nothing
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 操作 Excel 报表 实例 图例 源码 注释