frmExcelPrint.vb
资源名称:GOLDPRINT.rar [点击查看]
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:9k
源码类别:
打印编程
开发平台:
Others
- ' Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表
- ' 将所有的格式、报表统计、图形分析等等借用Excel强大功能
- Public Class frmExcelPrint
- Inherits System.Windows.Forms.Form
- #Region " Windows 窗体设计器生成的代码 "
- Public Sub New()
- MyBase.New()
- '该调用是 Windows 窗体设计器所必需的。
- InitializeComponent()
- '在 InitializeComponent() 调用之后添加任何初始化
- End Sub
- '窗体重写 dispose 以清理组件列表。
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- 'Windows 窗体设计器所必需的
- Private components As System.ComponentModel.IContainer
- '注意: 以下过程是 Windows 窗体设计器所必需的
- '可以使用 Windows 窗体设计器修改此过程。
- '不要使用代码编辑器修改它。
- Friend WithEvents dataGrid1 As System.Windows.Forms.DataGrid
- Friend WithEvents btnExcelTemplate As System.Windows.Forms.Button
- Friend WithEvents btnExcel As System.Windows.Forms.Button
- Friend WithEvents btnExcelPreview As System.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.dataGrid1 = New System.Windows.Forms.DataGrid
- Me.btnExcelTemplate = New System.Windows.Forms.Button
- Me.btnExcel = New System.Windows.Forms.Button
- Me.btnExcelPreview = New System.Windows.Forms.Button
- CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
- Me.SuspendLayout()
- '
- 'dataGrid1
- '
- Me.dataGrid1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
- Me.dataGrid1.CaptionText = "MIS金质打印通 通打天下报表 作者:长江支流 下载:www.webmis.com.cn"
- Me.dataGrid1.DataMember = ""
- Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
- Me.dataGrid1.Location = New System.Drawing.Point(0, 40)
- Me.dataGrid1.Name = "dataGrid1"
- Me.dataGrid1.ReadOnly = True
- Me.dataGrid1.Size = New System.Drawing.Size(712, 408)
- Me.dataGrid1.TabIndex = 13
- '
- 'btnExcelTemplate
- '
- Me.btnExcelTemplate.Location = New System.Drawing.Point(392, 8)
- Me.btnExcelTemplate.Name = "btnExcelTemplate"
- Me.btnExcelTemplate.Size = New System.Drawing.Size(312, 23)
- Me.btnExcelTemplate.TabIndex = 11
- Me.btnExcelTemplate.Text = "Excel模板套打、格式、统计分析报表、图形分析"
- '
- 'btnExcel
- '
- Me.btnExcel.Location = New System.Drawing.Point(8, 8)
- Me.btnExcel.Name = "btnExcel"
- Me.btnExcel.Size = New System.Drawing.Size(180, 23)
- Me.btnExcel.TabIndex = 10
- Me.btnExcel.Text = "Excel输出"
- '
- 'btnExcelPreview
- '
- Me.btnExcelPreview.Location = New System.Drawing.Point(200, 8)
- Me.btnExcelPreview.Name = "btnExcelPreview"
- Me.btnExcelPreview.Size = New System.Drawing.Size(180, 23)
- Me.btnExcelPreview.TabIndex = 12
- Me.btnExcelPreview.Text = "Excel打印预览"
- '
- 'frmExcelPrint
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
- Me.ClientSize = New System.Drawing.Size(712, 446)
- Me.Controls.Add(Me.dataGrid1)
- Me.Controls.Add(Me.btnExcelTemplate)
- Me.Controls.Add(Me.btnExcel)
- Me.Controls.Add(Me.btnExcelPreview)
- Me.Name = "frmExcelPrint"
- Me.Text = "Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表"
- CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
- Me.ResumeLayout(False)
- End Sub
- #End Region
- '用Excel打印
- Private Sub btnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcel.Click
- Me.Cursor = Cursors.WaitCursor
- ImportPrint(False)
- Me.Cursor = Cursors.Default
- End Sub
- '直接调用Excel打印预览,用户可以调整列宽、设置页面、页边距、页眉页脚、调整居中方式
- Private Sub btnExcelPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcelPreview.Click
- Me.Cursor = Cursors.WaitCursor
- ImportPrint(True)
- Me.Cursor = Cursors.Default
- End Sub
- '用Excel定制模板打印,所有的格式、报表统计、图形分析等等借用Excel强大功能
- Private Sub btnExcelTemplate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcelTemplate.Click
- Me.Cursor = Cursors.WaitCursor
- ExcelTemplatePrint()
- Me.Cursor = Cursors.Default
- End Sub
- '加载打印示例数据
- Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Me.dataGrid1.DataSource = Me.GetDataSource()
- End Sub
- Private Function GetDataSource() As System.Data.DataTable
- Return PrintDataSource.GetDataSource(10)
- End Function
- Private Sub ImportPrint(ByVal IsPrintPreview As Boolean)
- Dim dt As New System.Data.DataTable
- dt = CType(dataGrid1.DataSource, DataTable)
- Dim font As New Font("黑体", 21, FontStyle.Bold)
- '用Excel打印,步骤为:打开、写数据、打印预览、关闭
- Dim excel As New GoldPrinter.ExcelAccess
- excel.Open()
- excel.FormCaption = Me.dataGrid1.CaptionText '"MIS金质打印通 通打天下报表"
- excel.MergeCells(1, 1, 1, dt.Columns.Count) '合并单元格写标题,并设置字体
- excel.SetFont(1, 1, 1, dt.Columns.Count, font)
- excel.SetCellText(1, 1, 1, dt.Columns.Count, "为灾区募捐,为中国程序员呐喊")
- '打印网格及网格线
- excel.SetCellText(dt, 3, 1, True)
- '可以在打印预览时单击“页边距”,调整每列的宽。单击“设置页边距”,选择水平居中方式
- If (IsPrintPreview) Then
- excel.PrintPreview()
- excel.Close()
- Else
- excel.ShowExcel()
- End If
- font.Dispose()
- End Sub
- '用Excel模板打印,比较适合套打、格式、统计分析报表、图形分析、自定义打印
- Private Sub ExcelTemplatePrint()
- '用Excel打印,步骤为:打开、写数据、打印预览、关闭
- Dim dt As New System.Data.DataTable
- dt = CType(dataGrid1.DataSource, DataTable) '少打几行看看,哈,打多少也行,只要你不闲慢
- Dim excel As New GoldPrinter.ExcelAccess
- Dim strExcelTemplateFile As String = System.IO.Path.GetFullPath("..ExcelTemplateexample.xlt")
- excel.Open(strExcelTemplateFile) '用模板文件
- excel.IsVisibledExcel = True
- excel.FormCaption = Me.dataGrid1.CaptionText '"MIS金质打印通 通打天下报表"
- Dim intDataTemplateRowIndex As Int32 = 16 '//15、16、17为模板数据行,在中间插入,则SUM(E15:E17)会自动更新如SUM(E15:E18)
- Dim intDataStartRowIndex As Int32 = 18 '//写数据起始行,这里应用了Excel本身的技巧,把格式和合计留给Excel本身去做。
- '//大家可以打开Excel文件,其中模板定义了三个数据行即15、16、17行(定义格式如小数位数、对齐),还有一个合计行18行,合计行定义了形如 =SUM(E15:E17) 。
- '//为什么定义三行数据行呢?因为定义多于两行的话,插入复制单元格后Excel会自动把SUM(E15:E17)中间的合计行也会增加一行
- '//如拷贝第16行,并在16行插入复制单元格后,数据行会成为4行,而合计=SUM(E15:E17)自动变成=SUM(E15:E18)、
- '//同理,图形统计也利用了这一原理
- '//***哈哈,大家注意了,以上是Excel本身的技术,与程序无关啊,我只是在制作Excel模板的时候用了Excel本身的技术,大家如果看不明白我上面的解释,不用管。
- '打印网格上面说明
- excel.SetCellText(12, "B", "010-81120172")
- 'excel.SetCellText(12, "E", "go@webmis.com.cn")
- '打印网格及网格线,可以直接调用
- 'excel.SetCellText(dt, intDataStartRowIndex, 1, True)
- '打印网格下面说明
- excel.SetCellText(19, "B", "010-81120172")
- excel.SetCellText(20, "B", "flygoldfish@sina.com")
- excel.SetCellText(19, "E", "150439795")
- excel.SetCellText(20, "E", System.DateTime.Now.ToLongDateString())
- excel.SetCellText(21, "A", "定制模板打印真方便啊,可以把好多事情如格式、计算、图表等让Excel自己做。")
- excel.SetCellText(22, "A", "不信?打开" + strExcelTemplateFile)
- '打印数据网格
- '这里用另一种方法,直接输出到模板文件指定的数据行上,不影响其它数据
- Dim i, j As Int32
- For i = 0 To dt.Rows.Count - 1
- excel.InsertRow(intDataTemplateRowIndex, intDataTemplateRowIndex) '拷贝插入模板行并设置行高
- excel.SetRowHeight(intDataTemplateRowIndex, 14.25F)
- Next
- For i = 0 To dt.Rows.Count - 1
- For j = 0 To dt.Columns.Count - 1
- excel.SetCellText(intDataStartRowIndex + i, j + 1, dt.Rows(i)(j).ToString())
- Next
- Next
- '画边框及网格线
- excel.SetBordersEdge(intDataStartRowIndex, 1, intDataStartRowIndex + dt.Rows.Count - 1, dt.Columns.Count, False)
- '删除模板数据行
- excel.DeleteRow(17)
- excel.DeleteRow(16)
- excel.DeleteRow(15)
- excel.PrintPreview() '预览
- excel.Close() '关闭并释放
- End Sub
- End Class