• C# 中使用Aspose.Words下载文档


    MVC中使用方式:

    1.前端js调用受到限制,简单的方式有:window.location.href = url; 或者 window.open(url);访问控制器中的方法

    2,后台使用代码:

    //方式一
            public ActionResult NewGenerate(bool wordDoc)
            {
                string rootFolderFile = System.Web.HttpContext.Current.Server.MapPath("~/Template/ReportTemplate.doc");
                if (System.IO.File.Exists(rootFolderFile))
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(rootFolderFile);
                    var builder = new Aspose.Words.DocumentBuilder(doc);
                    if (builder.MoveToBookmark("Report_No"))
                    {
                        builder.Write("testing");//write 内容不能为null
                    }
                    //方式一
                    if (wordDoc)
                    {
                        //doc文档
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
                        var docStream = new MemoryStream();
                        doc.Save(docStream, Aspose.Words.SaveFormat.Doc);
                        return File(docStream.ToArray(), "application/msword", fileName);
                    }
                    else
                    {
                        //pdf文档
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                        var docStream = new MemoryStream();
                        doc.Save(docStream, Aspose.Words.SaveFormat.Pdf);
                        return File(docStream.ToArray(), "application/vnd.ms-pdf", fileName);
                    }
                }
            }
    //方式二
            public ActionResult Generate(bool wordDoc)
            {
                string rootFolderFile = System.Web.HttpContext.Current.Server.MapPath("~/Template/ReportTemplate.doc");
                if (System.IO.File.Exists(rootFolderFile))
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(rootFolderFile);
                    var builder = new Aspose.Words.DocumentBuilder(doc);
                    if (builder.MoveToBookmark("Report_No"))
                    {
                        builder.Write("testing");//write 内容不能为null
                    }
                                    
                    //doc文档
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
                    if (wordDoc)
                        Response.ContentType = "application/msword";
                    else
                    {
                        fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                        Response.ContentType = "application/vnd.ms-pdf";
                    }
    
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.ContentEncoding = System.Text.Encoding.Unicode;
                    using (var memoryStream = new MemoryStream())
                    {
                        doc.Save(memoryStream, Aspose.Words.SaveFormat.Doc);
                        memoryStream.WriteTo(Response.OutputStream);
                        memoryStream.Close();
                    }
                    Response.End();
                }
            }

    ASP.NET中使用方式:

    public void Generate(bool wordDoc)
            {
                string rootFolderFile = System.Web.HttpContext.Current.Server.MapPath("~/Template/ReportTemplate.doc");
                if (System.IO.File.Exists(rootFolderFile))
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(rootFolderFile);
                    var builder = new Aspose.Words.DocumentBuilder(doc);
                    if (builder.MoveToBookmark("Report_No"))
                    {
                        builder.Write("testing");//write 内容不能为null
                    }
                                    
                    //doc文档
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
                    if (wordDoc)
                        doc.Save(Response, fileName, Aspose.Words.ContentDisposition.Attachment, new Aspose.Words.Saving.OoxmlSaveOptions(Aspose.Words.SaveFormat.Docx));
                    else
                    {
                        fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                        doc.Save(Response, fileName, Aspose.Words.ContentDisposition.Attachment, new Aspose.Words.Saving.OoxmlSaveOptions(Aspose.Words.SaveFormat.Pdf));
                    }
                    Response.End();
                }
            }
    寻寻觅觅转流年,磕磕碰碰道缘浅。 揽几缕、轻挽起,暮暮朝朝与君语。
  • 相关阅读:
    笔记33 Spring MVC的高级技术——Spring MVC配置的替代方案
    笔记32 SpringMVC中使用静态资源、处理中文乱码
    笔记31——注解
    笔记30 视图解析 ——TilesViewResolver
    笔记29 视图解析 ——InternalResourceViewResolver
    笔记28 接受请求的输入 ——处理表单
    笔记27 接受请求的输入 ——通过路径参数接受输入
    笔记26 接受请求的输入 ——处理查询参数
    笔记25 传递模型数据到视图中
    笔记24 定义类级别的请求处理
  • 原文地址:https://www.cnblogs.com/bingshao/p/15529257.html
Copyright © 2020-2023  润新知