VB.Net把excel导出PDF文件的实例
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
'导出PDF文件
Private Sub MyExportPDFMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Dim MyFileName As String = "G:\MyExcelPDF.pdf"
Dim MyDlg As New SaveFileDialog()
MyDlg.AddExtension = True
MyDlg.DefaultExt = "pdf"
If (MyDlg.ShowDialog() = DialogResult.OK) Then
MyFileName = MyDlg.FileName
End If
Dim missing = System.Reflection.Missing.Value
Me.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, _
MyFileName, Excel.XlFixedFormatQuality.xlQualityStandard, _
True, False, missing, missing, True, missing)
End Sub
End Class
As a beginner, you should learn to program before playing with stuff like this.
To parse a PDF you need a third party library, most of which cost money. To open stuff in Excel, the easiest way is to create a CSV, a text file where each line is a row, with commas between values.