在ASP.Net Core 2.2中使用HTML和CSS和DocumentFormat.OpenXml生成Docx文件

时间:2019-04-14 07:20:05

标签: c# asp.net asp.net-core openxml docx

是否可以使用DocumentFormat.OpenXml从HTML和CSS生成Docx文件?

我想使用ASP.Net Core 2.2创建一个可生成Docx文件的Web API应用程序,以及另一个需要生成Docx的应用程序,请调用我的Docx生成器应用程序。

换句话说,我想创建一个Docx生成器,其他应用程序可以从中获取帮助(其他想要从Docx生成器中获取帮助的应用程序可以通过任何编程语言(如JavaScript)来实现。

我发现在应用程序之间进行通信的唯一方法是使用HTML和CSS字符串从应用程序生成Docx文件。

当然,如果有其他更好的方法可以完成这项工作,请为我解释一下。

当前,我正在使用DocumentFormat.OpenXml生成Docx文件。

        using (MemoryStream mem = new MemoryStream())
        {
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
            {
                wordDoc.AddMainDocumentPart();
                // siga a ordem
                Document doc = new Document();
                Body body = new Body();


                // 1 paragrafo
                Paragraph para = new Paragraph();

                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
                Justification justification1 = new Justification() { Val = JustificationValues.Center };
                ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();

                paragraphProperties1.Append(paragraphStyleId1);
                paragraphProperties1.Append(justification1);
                paragraphProperties1.Append(paragraphMarkRunProperties1);

                Run run = new Run();
                RunProperties runProperties1 = new RunProperties();

                Text text = new Text() { Text = "The OpenXML SDK rocks!" };

                // siga a ordem 
                run.Append(runProperties1);
                run.Append(text);
                para.Append(paragraphProperties1);
                para.Append(run);

                // 2 paragrafo
                Paragraph para2 = new Paragraph();

                ParagraphProperties paragraphProperties2 = new ParagraphProperties();
                ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Normal" };
                Justification justification2 = new Justification() { Val = JustificationValues.Start };
                ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();

                paragraphProperties2.Append(paragraphStyleId2);
                paragraphProperties2.Append(justification2);
                paragraphProperties2.Append(paragraphMarkRunProperties2);

                Run run2 = new Run();
                RunProperties runProperties3 = new RunProperties();
                Text text2 = new Text();
                text2.Text = "Teste aqui";

                run2.AppendChild(new Break());
                run2.AppendChild(new Text("Hello"));
                run2.AppendChild(new Break());
                run2.AppendChild(new Text("world"));

                para2.Append(paragraphProperties2);
                para2.Append(run2);

                // todos os 2 paragrafos no main body
                body.Append(para);
                body.Append(para2);

                doc.Append(body);

                wordDoc.MainDocumentPart.Document = doc;

                wordDoc.Close();
            }
            return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");
        }

0 个答案:

没有答案