目前做金融业的项目,该公司每天会产生很多 Word、Excel 文档,需要大量地转换为 PDF,除了自己保存外,也要给金融主管机构作为备份。由于文档数量很多,希望有工具能在下班前、作完设置后,放着让机器自动、大量地批转;除此之外,亦希望有特殊的功能,例如给主管机构的 Excel 文档,能仅选择其中某几个 sheet (工作表) 转成 PDF。 我找了网络上许多 PDF 转换工具,没有完全符合上述需求的,只好自己用 .NET / VSTO 写一个。
开发环境:(1) VS 2008 / Windows Form / VB.NET / (2) Office 2007 或 Office 2010 或更高版本 (需要引用新版 Office 里的 COM 组件)(3) (可将 Office 2007 或更高版本的文件,导出成 PDF 或 XPS 文件)执行环境:(1) .NET Framework(2) Office 2007 或 Office 2010 或更高版本 (旧版的 Office XP / Office 2003 不行)(3) (可将 Office 2007 或更高版本的文件,导出成 PDF 或 XPS 文件)特性:(1) 支持中文文件名 (免费的 PDFCreator 从 1.2.x 版后即不支持)、中文目录、中文命名的 sheet。(2) 可选择 Excel 的某一个或某几个 sheet,整合转换成「单一个」PDF (Adobe Acrobat Standard 亦无此功能,要很贵的 Adobe Acrobat Professional 才有)。(3) 可选择 Excel 的某一个或某几个 sheet,「各自」转换成一个 PDF (我在网络上未找到具备此功能的软件)。(4) 批转的速度很快,大量批转时亦不曾发生程序崩溃,或占据 Word、Excel 文件资源不释放的情形。(5) 可处理 Excel 里,整个 sheet 内容全为空白的不正常情形。-------------------------------------------------本帖的示例下载点:附源码。若需要简体中文介面的网友,请自行用 VS 开启项目后,改写及编译。---------------------------------------------------执行画面 |
功能一 - 大批文件转换 |
关键代码 (大批文件转换)
''' <summary> ''' Word 轉 PDF ''' </summary> ''' <param name="PathAndFileName"> 路徑名\檔名.副檔名 </param> Public Sub ConvertWordToPDF( ByVal PathAndFileName As String) Dim mstrResultFormPath As String = PathAndFileName Dim strPDFFileName As String = Path.ChangeExtension(mstrResultFormPath, " .pdf ") Dim docApp As Object = CreateObject( " Word.Application ") Dim docDoc As Object = docApp.Documents.Open(mstrResultFormPath) Try ' ExportFormat: 指定要以 PDF 還是 XPS 格式儲存文件 docDoc.ExportAsFixedFormat(OutputFileName:=strPDFFileName, ExportFormat:= 17) Catch ex As Exception Throw New Exception( " 轉換 Word 檔 " & PathAndFileName & " 時發生錯誤,錯誤為: " & ex.Message) Finally docApp.Documents.Close() docApp.Quit() End Try End Sub ''' <summary> ''' Excel 轉 PDF (可有空白 sheet) ''' </summary> ''' <param name="PathAndFileName"> 路徑名\檔名.副檔名 </param> Public Sub ConvertExcelToPDF( ByVal PathAndFileName As String) ' Dim ws As Excel.Worksheet Dim mstrResultFormPath As String = PathAndFileName ' 例:d:\excelTestPDF.xls Dim strPDFFileName As String = Path.ChangeExtension(mstrResultFormPath, " .pdf ") Dim xlsApp As Object = CreateObject( " Excel.Application ") Dim xlsWBook As Object = xlsApp.Workbooks.Open(mstrResultFormPath) Try xlsWBook.ExportAsFixedFormat(Type:= 0, Filename:=strPDFFileName, IgnorePrintAreas:= False, OpenAfterPublish:= False, IncludeDocProperties:= True, Quality:=Excel.XlFixedFormatQuality.xlQualityStandard) Catch ex As Exception Throw New Exception( " 轉換 Excel 檔 " & PathAndFileName & " 時發生錯誤,錯誤為: " & ex.Message) Finally xlsApp.Workbooks.Close() xlsApp.Quit() End Try End Sub
功能二 - Excel 特定 sheet 转换 |
关键代码 (Excel 的某一个或某几个 sheet,整合转换成「单一个」PDF)
' 勾選的 sheet 轉成單一個 PDF 檔 (若選到空白的 sheet,不會出錯,但不會印出來) Public Sub ConvertCheckedSheetToOnePDF( ByVal PathAndFileName As String, ByVal listChecked As List( Of String), ByVal listUnChecked As List( Of String)) Dim oExcel As New Excel.Application Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook Dim oSheets As Excel.Sheets ' Dim oSheet As Excel.Worksheet Dim sTemplate As String, sFileName As String ' Excel 檔路徑 sTemplate = PathAndFileName ' 捉路徑+檔名(不含副檔名) sFileName = PathAndFileName.Substring( 0, PathAndFileName.LastIndexOf( " . ")) oExcel.Visible = False oExcel.DisplayAlerts = False oBooks = oExcel.Workbooks oBooks.Open(sTemplate) oBook = oBooks.Item( 1) oSheets = oBook.Worksheets ' 將沒勾選的 Worksheet,從 Workbook 裡移除 If Not listUnChecked Is Nothing Then If listUnChecked.Count > 0 Then Dim sheetDelete As Excel.Worksheet = Nothing ' 暫存用 For Each s_unC In listUnChecked ' oBook.Sheets(s_unC).Delete() '這種寫法,會引發: 無效的索引 (發生例外狀況於 HRESULT: 0x8002000B (DISP_E_BADINDEX」 sheetDelete = oBook.Sheets(s_unC) ' 依 sheet 的中文名稱,作為移除的依據 If Not sheetDelete Is Nothing Then sheetDelete.Delete() End If sheetDelete = Nothing Next End If End If oBook.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:=sFileName & " .pdf ", Quality:=Excel.XlFixedFormatQuality.xlQualityStandard, IncludeDocProperties:= True, IgnorePrintAreas:= False, OpenAfterPublish:= False)
图 6 功能二,可选择 Excel 的某一个或某几个 sheet,「各自」转换成一个 PDF
关键代码 (Excel 的某一个或某几个 sheet,「各自」转换成一个 PDF)
' 勾選的 sheet 各轉成一個 PDF 檔 (若選到空白的 sheet,該 sheet 的轉換會失敗) Public Sub ConvertCheckedSheetToMultiplePDF( ByVal PathAndFileName As String, ByVal listChecked As List( Of String)) Dim oExcel As New Excel.Application Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook Dim oSheets As Excel.Sheets Dim oSheet As Excel.Worksheet Dim sTemplate As String, sFileName As String ' Excel 檔路徑 sTemplate = PathAndFileName ' 捉路徑+檔名(不含副檔名) sFileName = PathAndFileName.Substring( 0, PathAndFileName.LastIndexOf( " . ")) oExcel.Visible = False oExcel.DisplayAlerts = False oBooks = oExcel.Workbooks oBooks.Open(sTemplate) oBook = oBooks.Item( 1) oSheets = oBook.Worksheets For i As Integer = 0 To listChecked.Count - 1 ProgressBar1.PerformStep() ' 進度列 ' ss &= listChecked(i) & vbCrLf oSheet = CType(oSheets.Item(listChecked(i)), Excel.Worksheet) ' ss &= oSheet.Name & vbCrLf oSheet.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, IncludeDocProperties:= True, OpenAfterPublish:= False, Quality:=Excel.XlFixedFormatQuality.xlQualityStandard, _ Filename:=sFileName & " _ " & oSheet.Name & " .pdf ") Next
组件引用 |
结论 |
参考文章:
Word 及 Excel 的 Workbook.ExportAsFixedFormat Method如何给excel新增工作表 C# (Worksheet 操作大全)ASP.NET 產生 PDF 或 XPS 檔 (繁体中文)ASP.NET 產生 PDF 或 XPS 檔 (繁体中文)Merge Workbook With Another Workbook using VSTOPrinting multiple worksheets as seperate PDF filesASP.NET调用COM Word转Pdf之我见Print To PDF Using Microsoft's PDF/XPS Add-inHow do I export multiple sheets/charts to a single PDF, but not the entire workbook ? (此做法有缺陷)VSTO 入门 (Visual Studio Tools for Office)
参考书籍:
Visual Studio Tools for Office 2007
相关文章:
利用Office Save as PDF or XPS 实现Office批传PDFConvert Word-Documents to PDF on an ASP.NET ServerExcel VBA - WorkSheet新增、複製、刪除(不出現提示視窗)