当前位置: 首页 > news >正文

.NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器

Aspose.Words for .NET提供了一套完整的功能,用于在多个.NET应用程序中操作和转换MS Word文档。您可以在桌面或Web应用程序中创建新的或编辑现有的Word文档。

在本文中,将展示如何利用Aspose.Words for .NET的字处理功能,以及如何在ASP.NET MVC中创建基于Web的MS Word编辑器
aspose.words最新下载(技术交流群 761297826)icon-default.png?t=M7J4https://www.evget.com/product/564/download在ASP.NET MVC中创建MS Word编辑器的步骤

为了演示,将在此应用程序中使用了基于JavaScript的Suneditor WYSIWYG编辑器。您可以使用相同的内容,也可以选择其他任何适合您要求的HTML编辑器。以下是创建ASP.NET Word编辑器的步骤。

  • 在Visual Studio中创建一个新的ASP.NET Core Web应用程序。

  • 选择 Web应用程序(模型-视图-控制器) 模板。

  • 下载所见即所得编辑器的文件,并将其保存在 wwwroot 目录中。

  • 打开NuGet软件包管理器,然后安装Aspose.Words for .NET软件包。

  • 在index.cshtml 视图中添加以下脚本。
    @{
    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>
  • 在HomeController.cs 控制器中添加以下方法 。
    [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");
    }
  • 在您喜欢的浏览器中生成并运行该应用程序。

相关文章:

  • 【毕业设计】基于单片机的手势检测识别系统 - arduino 物联网嵌入式
  • 【Node.js】深度解析常用核心模块-path模块
  • C语言指针操作(六)*返回指针值的函数
  • 10. Vue 常用的修饰符的作用详解?
  • 第五篇 python 基本语法(一)
  • 猿创征文| JAVA Web的环境部署
  • Python基础(一) | Python的基本语法
  • 110道Java初级面试题及答案(最新Java初级面试题大汇总)
  • 简易版实现vue1.0
  • 6大面试技能树:JAVA基础+JVM+算法+数据库+计算机网络+操作系统,时刻为面试做准备!!
  • sobel算子边缘提取及程序优化(C语言)
  • python数据结构与算法
  • cookie,storage,sesstion区别
  • 学生家乡网页设计作品静态HTML网页—— HTML+CSS+JavaScript制作辽宁沈阳家乡主题网页源码(11页)
  • MKD调试下载的时候提示:Contents mismatch at: xxxxxxxxH (Flash=xxH Required=xxH)
  • 【5+】跨webview多页面 触发事件(二)
  • CSS3 变换
  • ESLint简单操作
  • overflow: hidden IE7无效
  • PaddlePaddle-GitHub的正确打开姿势
  • React+TypeScript入门
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • SQL 难点解决:记录的引用
  • storm drpc实例
  • Swift 中的尾递归和蹦床
  • Transformer-XL: Unleashing the Potential of Attention Models
  • vue-cli在webpack的配置文件探究
  • vue脚手架vue-cli
  • Vue--数据传输
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 关于字符编码你应该知道的事情
  • 基于HAProxy的高性能缓存服务器nuster
  • 记一次删除Git记录中的大文件的过程
  • 前端js -- this指向总结。
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 十年未变!安全,谁之责?(下)
  • 手写一个CommonJS打包工具(一)
  • 微信公众号开发小记——5.python微信红包
  • 一份游戏开发学习路线
  • 自制字幕遮挡器
  • #pragma multi_compile #pragma shader_feature
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (二)windows配置JDK环境
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • (转)人的集合论——移山之道
  • .libPaths()设置包加载目录
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .net反编译工具