100求购VB中调用EXCEL打印预览的最简单的例子!呱呱

cloud_soft 2002-01-16 07:31:50
100求购VB中调用EXCEL打印预览的最简单的例子!呱呱
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
96091103 2002-01-16
  • 打赏
  • 举报
回复
仅供参考,别忘了在工程里引用EXCEL Object Library。
Private Sub butPrint_Click()
Dim strFile, strSource As String
Dim lngCount As Long
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
strSource = App.Path & "\Reports\Ac.xls"
strFile = App.Path & "\Ac.xls"
On Error GoTo Handle
FileCopy strSource, strFile
Set xlbook = xlApp.Workbooks.Open(strFile)
Set xlsheet = xlApp.Worksheets("劳模甲种")
If rstRecordset.RecordCount >= 1 Then
prgBar.Max = rstRecordset.RecordCount + 1
With xlsheet
For lngCount = 1 To 10
.Columns(lngCount).HorizontalAlignment = xlHAlignCenter
.Columns(lngCount).VerticalAlignment = xlVAlignCenter
Next
.Columns(4).NumberFormat = "yy-m-d"
.Columns(7).NumberFormat = "yy-m-d"
prgBar.Visible = True
For lngCount = 2 To rstRecordset.RecordCount + 1
prgBar.Value = lngCount
.Cells(lngCount, 1) = rstRecordset!序号
.Cells(lngCount, 2) = rstRecordset!姓名
.Cells(lngCount, 3) = rstRecordset!性别
.Cells(lngCount, 4) = rstRecordset!出生日期
.Cells(lngCount, 5) = rstRecordset!工作单位
.Cells(lngCount, 6) = rstRecordset!投保标准
.Cells(lngCount, 7) = rstRecordset!投保时间
.Cells(lngCount, 8) = rstRecordset!所在县市
.Cells(lngCount, 9) = rstRecordset!有效性
.Cells(lngCount, 10) = rstRecordset!备注
rstRecordset.MoveNext
Next
.Range(.Cells(1, 1), .Cells(rstRecordset.RecordCount + 1, 10)).Borders.LineStyle = xlContinuous
End With
prgBar.Visible = False
rstRecordset.MoveFirst
End If
xlbook.Save
xlApp.Visible = True
xlsheet.PrintPreview
xlApp.Quit
On Error GoTo 0
Exit Sub
Handle:
xlApp.Quit
End Sub
cqq_chen 2002-01-16
  • 打赏
  • 举报
回复
来迟一点,太可惜了,100分啊!下次和EXCEL有关的问题找我。
ferrytang 2002-01-16
  • 打赏
  • 举报
回复
下面给出一个具体实例,它在window98、Visual Basic 6.0、
Microsoft Office97的环境下调试通过。

  在VB中启动一个新的Standard EXE工程,在“工程”菜单的“引用”
选项下引用Excel Object Library;然后在Form中添加一个命令按钮
cmdExcel;最后在窗体中输入如下代码:

  Dim zsbexcel As Excel.Application
      Private Sub cmdExcel_Click()
          Set zsbexcel = New Excel.Application
   zsbexcel.Visible = True
   zsbexcel.SheetsInNewWorkbook = 1
   Set zsbworkbook = zsbexcel.Workbooks.Add
   With zsbexcel.ActiveSheet.Range("A2:C9").Borders   '边框设置
      .LineStyle = xlBorderLineStyleContinuous
      .Weight = xlThin
      .ColorIndex = 1
      End With
   With zsbexcel.ActiveSheet.Range("A3:C9").Font  '字体设置
       .Size = 14
      .Bold = True
      .Italic = True
      .ColorIndex = 3
   End With
  zsbexcel.ActiveSheet.Rows.HorizontalAlignment =
xlVAlignCenter   '水平居中

  zsbexcel.ActiveSheet.Rows.VerticalAlignment =
xlVAlignCenter    '垂直居中

  With zsbexcel.ActiveSheet
    .Cells(1, 2).Value = "100"
    .Cells(2, 2).Value = "200"
    .Cells(3, 2).Value = "=SUM(B1:B2)"
    .Cells(1, 3).Value = "中国人民解放军"
    .Range("A3:A9") = "50"
  End With
 zsbexcel.ActiveSheet.PageSetup.Orientation = xlPortrait    '
xlLandscape
 zsbexcel.ActiveSheet.PageSetup.PaperSize = xlPaperA4
 zsbexcel.ActiveSheet.PrintOut
 zsbexcel.DisplayAlerts = False
 zsbexcel.Quit
 zsbexcel.DisplayAlerts = True
 Set zsbexcel = Nothing

 

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧