在C#(MVC)中将内容从一个Word文档复制到另一个Word文档

时间:2015-12-29 09:07:22

标签: c# asp.net-mvc visual-studio-2013 foreach spire.doc

我是论坛的新手,我刚开始在Visual Studio 2013上使用新的Web应用程序。我需要创建一个将所有内容从一个Word文档复制到另一个Word文档的应用程序。我找到了应该完成工作的this plugin,但我不知道如何将它放在我的代码中并使其工作。我需要在MVC应用程序中执行它,所以也许我没有将代码放在正确的位置(现在它在模型中)。有人可以帮我告诉我如何使它工作?请查看我的代码:

using Spire.Doc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DocumentApplication.Models
{
public class HomeModel
{
    public string Message { get; set; }        
}

public class CopyDocument
{
    Document sourceDoc = new Document("source.docx");
    Document destinationDoc = new Document("target.docx");        
    foreach (Section sec in sourceDoc.Sections) 
    {
        foreach (DocumentObject obj in sec.Body.ChildObjects)
        {
            destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
        }
    }
    destinationDoc.SaveToFile("target.docx");
    System.Diagnostics.Process.Start("target.docx");    
}

public class OpenDocument
{        
    Document document = new Document(@"C:\Users\daniel\Documents\ElAl-DRP.doc");
}
}

我无法编译这个,因为我在“foreach”行上有一个错误,上面写着:“类'struct'或接口成员声明中的'foreach'无效”。

请帮帮我。

提前致谢

1 个答案:

答案 0 :(得分:0)

好的,这很粗糙,但至少可行。

我无法从Spire工作中得到这个例子,我不得不做出一些改变。

此示例将采用上传的文件,将其保存到" AppData / uploads"然后,它将在" AppData / copies"中创建该已保存文件的副本。文件夹中。

我的变化

  1. 我们在内存中创建目标文档的方式,然后保存。
  2. 在" foreach"中创建一个新的部分。循环
  3. 将克隆的部分添加到新部分 清单项目
  4. SaveDocument方法的文档格式(这是一个巨大的         假设)
  5. <强>控制器

        using System.IO;
        using System.Web;
        using System.Web.Mvc;
        using Spire.Doc;
        using Spire.Doc.Collections;
    
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(HttpPostedFileBase file)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
    
                    var outputPath = Path.Combine(Server.MapPath("~/App_Data/copies"), fileName);
    
                    var sourceDoc = new Document(path);
                    var destinationDoc = new Document();
                    foreach (Section sec in sourceDoc.Sections)
                    {
                        var newSection = destinationDoc.AddSection();
                        foreach (DocumentObject obj in sec.Body.ChildObjects)
                        {
                            newSection.Body.ChildObjects.Add(obj.Clone());
                        }
                    }
                    destinationDoc.SaveToFile(outputPath, FileFormat.Docx);
                }
    
                return View();
            }    
        }
    

    查看

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="file" id="file"/>
        <button type="submit">Save</button>
    }