| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 其他 >> 实例分析正文
最新推荐 更多内容
VB.Net把excel导出XPS文件
VB.Net把excel导出XPS文件
作者:Ilu    来源:乐博网整理     更新时间:2009-9-23

乐博网lob.cn提示:调试环境为 vs2008 +  windows2003 / windows2008 / xp / vista

VB.Net把excel导出XPS文件
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core

Public Class Sheet1
    Private MyShowDataMenu As Office.CommandBarButton
    Private MyExportPDFMenu As Office.CommandBarButton
    Private MyExportXPSMenu As Office.CommandBarButton
    Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        AddMenuBar()
    End Sub
    Private Sub Sheet1_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
    End Sub
    Private Sub AddMenuBar()
        Try
            Dim missing = System.Reflection.Missing.Value
            Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing
            Dim MyCommandBarMenu As Office.CommandBar = CType( _
                Application.CommandBars.ActiveMenuBar, Office.CommandBar)
            Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count
            MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add( _
                Office.MsoControlType.msoControlPopup, missing, missing, _
                MyControlsCount, True), Office.CommandBarPopup)
            If (MyCommandBarPopup IsNot Nothing) Then
                MyCommandBarPopup.Caption = "数据导入导出"
                MyShowDataMenu = CType(MyCommandBarPopup.Controls.Add( _
                    Office.MsoControlType.msoControlButton, missing, _
                    missing, missing, True), Office.CommandBarButton)
                MyShowDataMenu.Caption = "显示客户信息"
                MyShowDataMenu.TooltipText = "显示Northwind数据库""客户""数据表的信息"
                MyShowDataMenu.FaceId = 65
                AddHandler MyShowDataMenu.Click, AddressOf MyShowDataMenuCommand_Click
                MyExportPDFMenu = CType(MyCommandBarPopup.Controls.Add( _
                    Office.MsoControlType.msoControlButton, missing, missing, _
                    missing, True), Office.CommandBarButton)
                MyExportPDFMenu.Caption = "导出PDF文件"
                MyExportPDFMenu.TooltipText = "将当前工作表中显示的内容导出为PDF文件"
                MyExportPDFMenu.FaceId = 66
                AddHandler MyExportPDFMenu.Click, AddressOf MyExportPDFMenuCommand_Click
                MyExportXPSMenu = CType(MyCommandBarPopup.Controls.Add( _
                    Office.MsoControlType.msoControlButton, missing, missing, _
                    missing, True), Office.CommandBarButton)
                MyExportXPSMenu.Caption = "导出XPS文件"
                MyExportXPSMenu.TooltipText = "将当前工作表中显示的内容导出为XPS文件"
                MyExportXPSMenu.FaceId = 67
                AddHandler MyExportXPSMenu.Click, AddressOf MyExportXPSMenuCommand_Click
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "乐博网信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    '显示客户信息
    Private Sub MyShowDataMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
        Dim MySQL As String = "Select 客户ID,公司名称,联系人姓名,地址 From 客户"
        Dim MyPath As String = "G:\Northwind.mdb"
        Dim missing = System.Reflection.Missing.Value
        Dim MyRange As Excel.Range = Globals.Sheet1.Range("A3", missing)
        Dim MyQueryTables As Excel.QueryTables = Globals.Sheet1.QueryTables
        Dim MyQueryTable As Excel.QueryTable = MyQueryTables.Add( _
            "OLEDB;Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + _
            MyPath + ";", MyRange, MySQL)
        MyQueryTable.RefreshStyle = Excel.XlCellInsertionMode.xlInsertEntireRows
        MyQueryTable.Refresh(False)
    End Sub
    
    '导出XPS文件
    Private Sub MyExportXPSMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
        Dim MyFileName As String = "G:\MyExcelXPS.xps"
        Dim MyDlg As New SaveFileDialog()
        MyDlg.AddExtension = True
        MyDlg.DefaultExt = "xps"
        If (MyDlg.ShowDialog() = DialogResult.OK) Then
            MyFileName = MyDlg.FileName
        End If   '更多.net源码和实例,来自乐博网 www.lob.cn
        Dim missing = System.Reflection.Missing.Value
        Me.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypeXPS, MyFileName, _
                               Excel.XlFixedFormatQuality.xlQualityStandard, _
                               True, False, missing, missing, True, missing)
    End Sub
End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net数据自增的3个实例
    VB.Net在Excel工作簿中使用Chart宿主控件显示图表
    VB.Net为excel取消用户密码的实例
    VB.Net为excel设置用户密码的实例
    VB.Net把excel导出PDF文件的实例
    VB.Net把excel导入Access数据库
    VB.Net把excel导入文本文件的实例
    VB.Net把excel导入XML文件的实例
    VB.Net显示和隐藏Excel操作窗格的实例
    VB.Net Excel合并单元格的实例
    VB.Net输出Word和Excel报表的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]