当前位置: 首页 > 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)
  • JDK 6和JDK 7中的substring()方法
  • js继承的实现方法
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • 思考 CSS 架构
  • 鱼骨图 - 如何绘制?
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • #define,static,const,三种常量的区别
  • (03)光刻——半导体电路的绘制
  • (1)Android开发优化---------UI优化
  • (a /b)*c的值
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (南京观海微电子)——COF介绍
  • (三)mysql_MYSQL(三)
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (一)WLAN定义和基本架构转
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET MVC 验证码
  • .net mvc部分视图
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .net 发送邮件
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .Net 垃圾回收机制原理(二)
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET轻量级ORM组件Dapper葵花宝典
  • .py文件应该怎样打开?
  • /proc/vmstat 详解
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • []sim300 GPRS数据收发程序
  • [2008][note]腔内级联拉曼发射的,二极管泵浦多频调Q laser——
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法
  • [autojs]autojs开关按钮的简单使用