1

我是论坛的新手,我刚开始在 Visual Studio 2013 上使用新的 Web 应用程序。我需要创建一个应用程序,将所有内容从一个 Word 文档复制到另一个文档。我发现这个插件应该可以完成这项工作,但我不知道如何将它放入我的代码中并使其工作。我需要在 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”行上有一个错误,上面写着:“类'结构'或接口成员声明中的令牌'foreach'无效”。

请帮我。

提前致谢

4

1 回答 1

0

好的,这很粗糙,但至少有效。

我无法从 Spire 中制作示例,我必须进行一些更改。

此示例将获取上传的文件,将其保存到“AppData/uploads”文件夹中,然后它将在“AppData/copies”文件夹中创建该保存文件的副本。

我的改变

  1. 我们在内存中创建目标文档然后保存的方式。
  2. 在“foreach”循环中创建一个新部分
  3. 将克隆的部分添加到新的部分列表项
  4. SaveDocument 方法上的文档格式(这是一个巨大的假设)

控制器

    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>
}
于 2015-12-29T16:15:50.900 回答