翻譯|使用教程|編輯:李顯亮|2020-04-20 11:12:34.443|閱讀 721 次
概述:在本文中,將展示如何利用Aspose.Words for .NET的字處理功能,以及如何在ASP.NET MVC中創建基于Web的MS Word編輯器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words for .NET提供了一套完整的功能,用于在多個.NET應用程序中操作和轉換MS Word文檔。您可以在桌面或Web應用程序中創建新的或編輯現有的Word文檔。
在本文中,將展示如何利用Aspose.Words for .NET的字處理功能,以及如何在ASP.NET MVC中創建基于Web的MS Word編輯器。
Aspose.Words for .NET已升級至V20.4,如果你還沒有用過Aspose.Words可以點擊這里下載最新版測試。
為了演示,將在此應用程序中使用了基于JavaScript的Suneditor WYSIWYG編輯器。您可以使用相同的內容,也可以選擇其他任何適合您要求的HTML編輯器。以下是創建ASP.NET Word編輯器的步驟。
@{ | |
|
ViewData["Title"] = "Word Editor in ASP.NET"; |
|
} |
|
<div class="row"> |
|
<div class="col-md-12"> |
|
<form asp-controller="Home" asp-action="UploadFile" method="post" class="form-inline" |
|
enctype="multipart/form-data"> |
|
<br /> |
|
<div class="form-group"> |
|
<input type="file" name="file" accept=".doc, .docx" class="form-control custom-file-input" /> |
|
div> |
|
<div class="form-group"> |
|
<button type="submit" class="form-control btn btn-primary">Openbutton> |
|
div> |
|
<div class="form-group" style="position:relative; float :right"> |
|
<button type="button" id="download" class="form-control btn btn-success" value="Save and Download">Save and Downloadbutton> |
|
div> |
|
form> |
|
<br /> |
|
<form method="post" asp-action="Index" id="formDownload"> |
|
<textarea name="editor" id="editor" rows="80" cols="100"> |
|
@if (ViewBag.HtmlContent == null) |
|
{ |
|
<p>Write something or open an existing Word document. p> |
|
} |
|
else |
|
{ |
|
@ViewBag.HtmlContent; |
|
} |
|
textarea> |
|
form> |
|
div> |
|
div> |
|
|
|
<link href="~/suneditor/dist/css/suneditor.min.css" rel="stylesheet"> |
|
|
|
<script src="~/suneditor/dist/suneditor.min.js">script> |
|
<script> |
|
var suneditor = SUNEDITOR.create('editor', { |
|
display: 'block', |
|
width: '100%', |
|
height: '30%', |
|
popupDisplay: 'full', |
|
buttonList: [ |
|
['font', 'fontSize', 'formatBlock'], |
|
['paragraphStyle', 'blockquote'], |
|
['bold', 'underline', 'align', 'strike', 'subscript', 'superscript', 'horizontalRule', 'list'], |
|
['table', 'link', 'image'], |
|
['align', 'horizontalRule', 'list', 'lineHeight'], |
|
['codeView'] |
|
], |
|
placeholder: 'Start typing something...' |
|
}); |
|
script> |
|
<script> |
|
$(document).ready(function () { |
|
$("#download").click(function () { |
|
suneditor.save(); |
|
$("#formDownload").submit(); |
|
}); |
|
}); |
|
script> |
[HttpPost] | |
|
public FileResult Index(string editor) |
|
{ |
|
try |
|
{ |
|
// Create a unique file name |
|
string fileName = Guid.NewGuid() + ".docx"; |
|
// Convert HTML text to byte array |
|
byte[] byteArray = Encoding.UTF8.GetBytes(editor.Contains("") ? editor : "" + editor + ""); |
|
// Generate Word document from the HTML |
|
MemoryStream stream = new MemoryStream(byteArray); |
|
Document Document = new Document(stream); |
|
// Create memory stream for the Word file |
|
var outputStream = new MemoryStream(); |
|
Document.Save(outputStream, SaveFormat.Docx); |
|
outputStream.Position = 0; |
|
// Return generated Word file |
|
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Rtf, fileName); |
|
} |
|
catch (Exception exp) |
|
{ |
|
return null; |
|
} |
|
} |
|
[HttpPost] |
|
public ViewResult UploadFile(IFormFile file) |
|
{ |
|
// Set file path |
|
var path = Path.Combine("wwwroot/uploads", file.FileName); |
|
using (var stream = new FileStream(path, FileMode.Create)) |
|
{ |
|
file.CopyTo(stream); |
|
} |
|
// Load Word document |
|
Document doc = new Document(path); |
|
var outStream = new MemoryStream(); |
|
// Set HTML options |
|
HtmlSaveOptions opt = new HtmlSaveOptions(); |
|
opt.ExportImagesAsBase64 = true; |
|
opt.ExportFontsAsBase64 = true; |
|
// Convert Word document to HTML |
|
doc.Save(outStream, opt); |
|
// Read text from stream |
|
outStream.Position = 0; |
|
using(StreamReader reader = new StreamReader(outStream)) |
|
{ |
|
ViewBag.HtmlContent = reader.ReadToEnd(); |
|
} |
|
return View("Index"); |
|
} |
以下是如何在ASP.NET Word編輯器中創建或編輯Word文檔的演示。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn