【vb精品源码栏目提醒】:网学会员为广大网友收集整理了,VB操作Excel报表(实例、图例、源码、注释) - 其它资料,希望对大家有所帮助!
VB 操作 Excel 报表(实例、图例、
源码、注释) 用 Excel 报表非常方便,而用
VB 操作 Excel 更能延长
VB 的功能范围。
对于重复生成的表格更是非常方便。
字体,行高,列宽,合并单元格,排版等功能都有。
供大家
学习研究。
VB 中使用 Excel 控件要先引用哦。
要不你错都不知道哪错了。
郁闷吧! 一、报表预览如下 1、无内容报表如下:2、填表后预览如下: 测试内 容,随便 填的。
二、界面预览如下: 打印记录按钮为 Command3三、
源码如下:Private Sub Command3_Click 声明一个新的 Excel 对象 Set winkexcel New Excel.Application 该对象可见 winkexcel.Visible True 选择第一张表为操作表 winkexcel.SheetsInNewWorkbook 1 表添加内容 Set winkworkbook winkexcel.Workbooks.Add 设置指定单元格行高列宽 Set xlsheet winkworkbook.Worksheets1 xlsheet.Rows1.RowHeight 45 xlsheet.Rows2.RowHeight 33 xlsheet.Rows3.RowHeight 33 xlsheet.Rows4.RowHeight 33 xlsheet.Rows5.RowHeight 33 xlsheet.Rows6.RowHeight 300 xlsheet.Rows7.RowHeight 100 xlsheet.Rows8.RowHeight 45 xlsheet.Rows9.RowHeight 45 xlsheet.Rows10.RowHeight 26 xlsheet.Columns1.ColumnWidth 14 xlsheet.Columns2.ColumnWidth 24 xlsheet.Columns3.ColumnWidth 14 xlsheet.Columns4.ColumnWidth 24 设置表格内容的对齐方式winkexcel.ActiveSheet.Rows.HorizontalAlignment xlVAlignCenter水平居中winkexcel.ActiveSheet.Rows.VerticalAlignment xlVAlignCenter垂直居中 With winkexcel.ActiveSheet.RangeA1:D1合并单元格 .Merge End With With winkexcel.ActiveSheet.RangeB6:D6合并单元格 .Merge End With With winkexcel.ActiveSheet.RangeB7:D7合并单元格 .Merge End With 画边框线,细线,黑。
With winkexcel.ActiveSheet.RangeA3:D9.Borders 边框设置 .LineStyle xlBorderLineStyleContinuous .Weight xlThin .ColorIndex 1 End With 为单元格添加内容,请注意数字索引顺序。
行在前,列再后。
With winkexcel.ActiveSheet .Cells2 1.Value 检修部位: 第 1 行第 2 列 .Cells2 4.Value 编号: 第 2 行第 2 列 .Cells3 1.Value 设备型号 .Cells3 3.Value 入库检修时间 .Cells4 1.Value 设备编号 .Cells4 3.Value 检修完工时间 .Cells5 1.Value 修 程 .Cells5 3.Value 检 修 单 位 .Cells6 1.Value 检 修 主 要 项 目 .Cells7 1.Value 更换主要部件 注意添加空格,使自动换行后内容都居中。
.Cells8 1.Value 检修班长 (签字) .Cells8 3.Value 检修人 (签字) .Cells9 1.Value 主管部长 (签字) .Cells9 3.Value 验收员 (签字) .Cells10 1.Value 铁路运输部 .Cells3 4.Value DTPicker1.Value .Cells4 4.Value DTPicker2.Value .Cells10 4.Value DTPicker3.Value .Cells2 2.Value Combo7.Text .Cells2 4.Value 编号: Text2.Text .Cells3 2.Value Text3.Text .Cells4 2.Value Text1.Text .Cells5 2.Value Text4.Text .Cells5 4.Value Combo6.Text .RangeA1:D1 内燃机车检修验收单 这个区域的值 .RangeB6:D6 Text7.Text .RangeB7:D7 Text8.TextEnd WithWith winkexcel.ActiveSheet 设置自动换行 .Cells6 1.WrapText True 设置文字方向为竖版 .Cells6 1.Orientation xlVertical .Cells7 1.WrapText True . .Cells7 1Orientation x lVertical .Cells8 1.WrapText True .Cells9 1.WrapText True .Cells8 3.WrapText True .Cells9 3.WrapText TrueEnd With 设置范围单元格内的字体 With winkexcel.ActiveSheet.RangeA1:D1.Font 字体设置 .Bold True .Size 20 .ColorIndex 1 End With With winkexcel.ActiveSheet.RangeA2:D10.Font 字体设置 .Size 12 .ColorIndex 1 End With 设置上下左右边距。
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 Set winkexcel NothingEnd Sub 总结: 用 Excel 报表是非常方便的,告诉你各秘诀,其实你可以用 excel的宏录制下,然后研究 Excel 所有的属性。
那样你想做多复杂的报表就可以做多复杂的报表。