翻译|使用教程|编辑:李显亮|2020-04-20 11:12:34.443|阅读 729 次
概述:在本文中,将展示如何利用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@fz165y.cn