如何用EXCEL模版做报表

MarGo 2002-03-25 07:44:47
能给个例子吗?
谢谢
...全文
836 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zerg2001 2002-04-01
  • 打赏
  • 举报
回复
用EXCEL作打印表,最大缺点就是速度慢,如果输出的页面多的话,
还是不要用为好。
gase 2002-04-01
  • 打赏
  • 举报
回复
差不多
lihonggen0 2002-04-01
  • 打赏
  • 举报
回复
用VB控制EXCEL生成报表
做为一种简捷、系统的 Windows应用程序开发工具,Visual Basic 5 具有强大的数据处理功能,提供了多种数据访问方法,可以方便地存取Microsoft SQL Server、Oracle、XBase等多种数据库,被广泛应用于建立各种信息管理系统。但是,VB缺乏足够的、符合中文习惯的数据表格输出功能,虽然使用Crystal Report控件及 Crystal Reports程序可以输出报表,但操作起来很麻烦,中文处理能力也不理想。Excel作为Micorsoft公司的表格处理软件在表格方面有着强大的功能,我们可用VB5编写直接控制Excel操作的程序,方法是用VB的OLE自动化技术获取Excel 97 的控制句柄,从而直接控制Excel 97的一系列操作。

下面给出一个实例:

首先建立一个窗体(FORM1)在窗体中加入一个DATA控件和一按钮,

引用Microsoft Excel类型库:

从"工程"菜单中选择"引用"栏;

选择Microsoft Excel 8.0 Object Library;

选择"确定"。

在FORM的LOAD事件中加入:
  Data1.DatabaseName = 数据库名称
  Data1.RecordSource = 表名
  Data1.Refresh

在按钮的CLICK事件中加入
  Dim Irow, Icol As Integer
  Dim Irowcount, Icolcount As Integer
  Dim Fieldlen() "存字段长度值
  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet

  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Add
  Set xlSheet = xlBook.Worksheets(1)

  With Data1.Recordset
  .MoveLast

  If .RecordCount < 1 Then
    MsgBox ("Error 没有记录!")
    Exit Sub
  End If

  Irowcount = .RecordCount "记录总数
  Icolcount = .Fields.Count "字段总数

  ReDim Fieldlen(Icolcount)
  .MoveFirst

8

  For Irow = 1 To Irowcount + 1
   For Icol = 1 To Icolcount
  Select Case Irow
  Case 1 "在Excel中的第一行加标题
  xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
  Case 2 "将数组FIELDLEN()存为第一条记录的字段长

  If IsNull(.Fields(Icol - 1)) = True Then
    Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
     "如果字段值为NULL,则将数组Filelen(Icol)的值设为标题名的宽度
  Else
    Fieldlen(Icol) = LenB(.Fields(Icol - 1))
  End If

  xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
   "Excel列宽等于字段长
  xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
   "向Excel的CellS中写入字段值
  Case Else
  Fieldlen1 = LenB(.Fields(Icol - 1))

  If Fieldlen(Icol) < Fieldlen1 Then
  xlSheet.Columns(Icol).ColumnWidth = Fieldlen1
   "表格列宽等于较长字段长
  Fieldlen(Icol) = Fieldlen1
   "数组Fieldlen(Icol)中存放最大字段长度值
  Else
   xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
  End If

  xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
  End Select
  Next
  If Irow <> 1 Then
  If Not .EOF Then .MoveNext
  End If
  Next
  With xlSheet
  .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体"
   "设标题为黑体字
  .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
   "标题字体加粗
  .Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
   "设表格边框样式
  End With
  xlApp.Visible = True "显示表格
  xlBook.Save "保存
  Set xlApp = Nothing "交还控制给Excel
  End With

本程序在中文Windows98、中文VB5下通过。


turbochen 2002-04-01
  • 打赏
  • 举报
回复
签名!
MarGo 2002-03-26
  • 打赏
  • 举报
回复
不知道如何定位表格的位置
goldmoon 2002-03-25
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim rs As ADODB.Recordset
Dim strSource, strDestination As String
Dim obj As New bus_CompanyC.Company

Set rs = obj.TrainRepListAll

strSource = App.Path & "\TrainRep.xls"

strDestination = App.Path & "\print\TrainRep.xls" '创建临时文件

FileCopy strSource, strDestination

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = False '隐藏EXCEL应用程序窗口

Set xlBook = xlApp.Workbooks.Open(strDestination)

Set xlsheet = xlBook.Worksheets(1)

For i = 1 To rs.RecordCount
For j = 1 To rs.Fields.Count - 1
If Not (rs.BOF Or rs.EOF) Then
xlsheet.cells(i + 2, j) = rs.Fields.Item(j - 1)
End If
Next j
rs.MoveNext
Next i '填写excel表格

xlBook.Save '保存文件

xlsheet.printPreview

xlsheet.PrintOut '执行打印

xlApp.Quit '退出EXCEL


End Sub
water_j 2002-03-25
  • 打赏
  • 举报
回复
up!
gz!
happybeyond 2002-03-25
  • 打赏
  • 举报
回复
在“部件”对话框窗口中,选择“可插入对象”标签,选中“Excel图表”选项,次即Microsoft Excel程序!
MarGo 2002-03-25
  • 打赏
  • 举报
回复
???????????

809

社区成员

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

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