翻譯|使用教程|編輯:李顯亮|2020-07-09 10:17:28.440|閱讀 426 次
概述:合并多個MS Word文檔在各種情況下可能很有用。如果要在自己的應用程序中添加此功能怎么辦?在本文中將展示如何在ASP.NET Web應用程序中使用C#合并MS Word(DOC / DOCX)文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
合并多個MS Word文檔在各種情況下可能很有用。例如,它可以用于將相似類型的文檔保存到單個文件中,在共享之前合并多個文檔,等等。
但是,如果要在自己的應用程序中添加此功能怎么辦?在本文中,將展示如何在ASP.NET Web應用程序中使用C#合并MS Word(DOC / DOCX)文檔。此Word Document Merger應用程序將具有以下功能:
Aspose.Words for .NET是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經(jīng)更新至v20.7,添加了新節(jié)點以處理多節(jié)結構化文檔標簽,改進了SmartArt冷渲染的性能,RevisionOptions類擴展了新的屬性,點擊下載體驗
以下是創(chuàng)建ASP.NET應用程序的步驟,該應用程序使您無需使用MS Office / Word即可合并兩個或更多Word(DOC / DOCX)文檔。
在Visual Studio中創(chuàng)建一個 ASP.NET Core Web應用程序。
選擇 Web應用程序(模型-視圖-控制器) 模板。
使用NuGet軟件包管理器安裝 Aspose.Words for .NET軟件包。
將以下腳本插入 index.cshtml 文件中。
@{ | |
|
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 Documentsh2> |
|
<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 Asstrong> |
|
<select name="outputFormat" class="form-control"> |
|
<option value="DOCX">DOCXoption> |
|
<option value="PDF">PDFoption> |
|
select> |
|
<button type="submit" class="form-control btn btn-success">Merge and Downloadbutton> |
|
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> |
在HomeController.cs類中插入以下代碼。
public FileResult UploadFiles(Listfiles, string outputFormat) { if (files.Count() <= 1) { // display some message return null; } string fileName = "merged-document.docx"; string path = "wwwroot/uploads"; List documents = new List (); // 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); } }
在_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--> |
生成應用程序并在瀏覽器中運行它。
以下是使用ASP.NET Word Document Merger應用程序合并MS Word文檔的演示。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn