翻譯|使用教程|編輯:胡濤|2023-02-22 09:36:07.387|閱讀 163 次
概述:本文為您提供了有關如何使用 C# 在 ASP.NET 應用程序中合并 MS Word 文檔的完整指南,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
如果您正在處理一堆 Word 文檔,您可能會遇到需要以編程方式將多個文檔合并為一個文檔的場景。為此,本文為您提供了有關如何使用 C# 在 ASP.NET 應用程序中合并 MS Word 文檔的完整指南。
合并多個 MS Word 文檔可能有助于將相同類型的文檔保存到一個文件中,在共享之前合并多個文檔,等等。為了讓您實現這一點,我們將向您展示如何創建一個 ASP.NET 應用程序來合并 MS Word (DOC/DOCX) 文檔。此 Word 文檔合并應用程序將具有以下功能:
Aspose.Words for .NET是一個功能豐富的文字處理庫,可讓您輕松處理 MS Word 文檔。它還允許您在 ASP.NET 或任何 .NET/.NET Core 應用程序中將多個 Word 文檔合并為一個文檔。Aspose.Words for .NET 可以使用NuGet安裝,也可以作為DLL文件下載。
PM> install-package Aspose.Words
以下是創建 ASP.NET 應用程序的步驟,該應用程序可讓您在不使用 MS Office/Word 的情況下合并兩個或多個 Word (DOC/DOCX) 文檔。
@{ ViewData["Title"] = "Merge MS Word Documents in ASP.NET"; } <div class="row"> <div class="col-md-12" align="center"> <h2 class="text-info">Merge Two or More Word DOC/DOCX Documents</h2> <p class="text-info">Merge MS Word documents and get the results in DOCX or PDF format.</p> </div> </div> <br /> <form asp-controller="Home" asp-action="UploadFiles" method="post" class="form-inline dropzone" enctype="multipart/form-data"> <div class="row"> <div class="col-md-12" align="center"> <div> <input type="file" id="input-id" name="files" multiple accept=".doc, .docx" class="form-control file" data-preview-file-type="text" /> </div> </div> </div> <hr /> <div class="row"> <div class="col-md-12" align="center"> <div class="input-group-lg"> <strong>Save As</strong> <select name="outputFormat" class="form-control"> <option value="DOCX">DOCX</option> <option value="PDF">PDF</option> </select> <button type="submit" class="form-control btn btn-success">Merge and Download</button> </div> </div> </div> </form> <script> // Drag and drop plugin options $("#input-id").fileinput({ 'mainClass': "input-group-lg", 'showBrowse': true, 'showUpload': false, 'previewFileType': 'any', 'showClose': false, 'maxFileCount': 5, }); </script
public FileResult UploadFiles(List<IFormFile> files, string outputFormat) { if (files.Count() <= 1) { // display some message return null; } string fileName = "merged-document.docx"; string path = "wwwroot/uploads"; List<Document> documents = new List<Document>(); // upload files foreach (IFormFile file in files) { string filePath = Path.Combine(path, file.FileName); // Save files using (var stream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(stream); } // Add all documents to the list documents.Add(new Document(filePath)); } // Load first Word document Document doc1 = documents[0]; for (int i = 1; i < documents.Count(); i++) { // Merge Word documents doc1.AppendDocument(documents[i], ImportFormatMode.KeepSourceFormatting); } var outputStream = new MemoryStream(); if (outputFormat == "DOCX") { doc1.Save(outputStream, SaveFormat.Docx); outputStream.Position = 0; // Return generated Word file return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Rtf, fileName); } else { fileName = "merged-document.pdf"; doc1.Save(outputStream, SaveFormat.Pdf); outputStream.Position = 0; // Return generated PDF file return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName); } } view rawHomeController.cs hosted with ? by GitHub 在_layout.cshtml文件的 head 標簽中包含以下拖放插件的 JS 和 CSS 文件。 <!--drag and drop file plugin--> <link media="all" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script> <script src="http://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/js/fileinput.min.js"></script> <!--end of drag and drop-->
以上便是使用 C# ASP.NET 合并 MS Word 文檔 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn