參考資料:
[Updated] How To: Merge Multiple Microsoft Word Documents | Keith Rull
主要是參考這篇資料,有範例檔
繼上一篇「ASP.NET 使用TemplateEngine.Docx套件,Word套表產生docx檔」後,假如使用者在使用的過程中,產生出很多docx檔時,總不能讓使用者一個一個套表下載吧! 因此須製作一個功能,可以讓使用者可以一次將多個docx檔合併到同一個docx檔。
Step 1.首先要將 Microsoft Word 12.0 Object Library 加入參考到自己的專案中,這個步驟應該要看自己的電腦安裝哪一種Office,有的可能找不到12.0 Object Library,那就用其他版本試試看或者是使用Microsoft.Office.Interop.Word.dll檔加入參考。
news_merge_word資料夾主要是存放已經合併完成的docx檔。
temp_word資料夾存放程式套好表的docx檔。
Template_Word資料夾只存放一個空到Word檔,你可以想像這個Word檔是將多的docx檔存放到同一個docx檔,這個空的word檔檔名為 normal.docx,你可以先設定好normal.docx上下左右的邊界,當多個docx檔合併到normal.docx時,會依照normal.docx的邊界下去合併。
Step 3.建立一個aspx網頁,畫面上只需要一個Button1按鈕,並在這個按鈕上建立Click事件。
以下為程式碼,程式碼的部分我是參考上方的參考資料
Imports System
Imports Word = Microsoft.Office.Interop.Word
Partial Class test_word_multi_file_merge
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str_path As String = String.Empty
'假設陣列中,是已經透過程式word套表,產生出的docx檔
Dim str_arrfile() As String = {Server.MapPath("~/temp_word/word1.docx"), Server.MapPath("~/temp_word/word2.docx"), Server.MapPath("~/temp_word/word3.docx")}
'第二個參數是將合併後的word檔,給予新的檔案名稱、第三個參數是否要有分頁符號
Merge(str_arrfile, Server.MapPath("~/news_merge_word/new_merge_word_" & Date.Now.ToString("yyyyMMddHHmmss") & ".docx"), True, Page)
End Sub
Sub Merge(ByVal filesToMerge As String(), ByVal outputFilename As String, ByVal insertPageBreaks As Boolean, ByVal this_page As Page)
'normal.docx檔(可以針對normal.docx先設定好邊界)是空白的word檔,可以把它想像成,把多個word檔合併到normal.docx;
Merge(filesToMerge, outputFilename, insertPageBreaks, Server.MapPath("~/Template_Word/normal.docx"))
End Sub
Sub Merge(ByVal filesToMerge As String(), ByVal outputFilename As String, ByVal insertPageBreaks As Boolean, ByVal documentTemplate As String)
Dim defaultTemplate As Object = documentTemplate
Dim pageBreak As Object = Word.WdBreakType.wdPageBreak
Dim outputFile As Object = outputFilename
' Create a new Word application
Dim wordApplication As Word._Application = New Word.Application()
Try
' Create a new file based on our template
Dim wordDocument As Word._Document = wordApplication.Documents.Add(defaultTemplate)
' Make a Word selection object.
Dim selection As Word.Selection = wordApplication.Selection
'Count the number of documents to insert;
Dim documentCount As Integer = filesToMerge.Length
'A counter that signals that we shoudn't insert a page break at the end of document.
Dim breakStop As Integer = 0
' Loop thru each of the Word documents
For Each file As String In filesToMerge
breakStop += 1
' Insert the files to our template
selection.InsertFile(file)
'Do we want page breaks added after each documents?
If insertPageBreaks AndAlso breakStop <> documentCount Then
selection.InsertBreak(pageBreak) '插入分頁
End If
Next
' Save the document to it's output file.
wordDocument.SaveAs(outputFile)
' Clean up!
wordDocument = Nothing
Catch ex As Exception
'I didn't include a default error handler so i'm just throwing the error
Throw ex
Finally
' Finally, Close our Word application
wordApplication.Quit()
End Try
End Sub
End Class
沒有留言:
張貼留言