简体   繁体   English

动态创建openxml文档

[英]Create openxml document dynamically

I am trying to build a openxml document dynamically, basically I have some classes responsible for creating the sections of my document (tables,paragraph...) then I need to have another class that builds my document, in my case I called it doc, and my other classes are docTable,docRun ... 我正在尝试动态地构建一个openxml文档,基本上我有一些类负责创建文档的各个部分(表格,段落...),然后我需要另一个类来构建文档,在我的情况下,我将其称为doc ,而我的其他类是docTable,docRun ...

So at the moment I have this: 所以目前我有这个:

    DocRun projectNameTitle = new DocRun();
    Run projectNameTxt = disclaimerDescription.createParagraph(document.projectName, SUBTITLECOLOR, FONTSIZESUBTITLE,FONTTYPE);

    DocRun dateParagraph = new DocRun();
    Run dateTxt = disclaimerDescription.createParagraph(date, PARAGRAPHTITLECOLOR, DATEFONTSIZE, DEFAULTFONT);

    Doc simpleDoc = new Doc();
    simpleDoc.CreateDoc(dateParagraph, projectNameTitle);

the paragraph builds correctly I just need to set it to the body of the doc at the moment, there is where the doc class enters, the parameters passed should be responsible to build the document in the order they are passed. 该段构建正确,我现在只需要将其设置为doc的正文,在doc类进入的地方,传递的参数应负责按照传递的顺序构建文档。

Here is my class responsible for building the document: 这是我的班级负责构建文档的:

using System.Web.Hosting;

namespace openXml
{
    public class Doc
    {
        public const String DOCUMENTSLOCATION = "~/Files"; // default documents location
        public void CreateDoc(params object[] document)
        {
            var stream = new MemoryStream();
            using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();

                new Document(new Body()).Save(mainPart);

                Body body = mainPart.Document.Body;

                foreach (var docSections in document)
                {
                    body.Append(new Paragraph(new ParagraphProperties(),
                   new Run((Run)docSections)));
                }
            }
            stream.Seek(0, SeekOrigin.Begin);
            Directory.CreateDirectory(HostingEnvironment.MapPath(DOCUMENTSLOCATION));
            System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/Files/test5.docx"), stream.ToArray());
        }
    }
}

I am having some troubles here, because I don't know if I am passing a run or other thing, how can I iterate over a list of items passed and append to the document in this case, I don't know what I am doing wrong here, the document doesn't get created :S 我在这里遇到了一些麻烦,因为我不知道我是否要传递运行或其他东西,在这种情况下,如何在传递的项目列表上进行迭代并追加到文档中,我不知道自己是什么在这里做错了,文档没有被创建:S

Try add this lines at the end of using 请尝试在结尾处添加此行using

doc.MainDocumentPart.Document.Save();
doc.Close();
fileBytes = stream.ToArray();

And save file like that: 并像这样保存文件:

File.WriteAllBytes(string path, fileBytes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM