[英]Copy Content from one Word Document to another in C# (MVC)
我是该论坛的新手,我只是开始在Visual Studio 2013上的新Web应用程序中工作。我需要创建一个将所有内容从一个Word文档复制到另一个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”行上显示错误:“类'struct'或接口成员声明中的无效令牌'foreach'”。
请帮我。
提前致谢
好的,这很粗糙,但至少可以正常工作。
我无法通过Spire的工作来举例说明,我必须进行一些更改。
本示例将获取一个上载的文件,将其保存到“ AppData / uploads”文件夹中,然后将在“ AppData / copies”文件夹中创建该已保存文件的副本。
我的变化
调节器
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>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.