创建docx word文档Web API .NET Core 2.0

MOH*_*OOD 4 c# report dynamic-reports .net-core asp.net-core-2.0

我正在Asp.net core 2.0中开发一个Web API项目。我需要一种库或创建Word文档的方法。我搜索了尝试过的NPOI和DocX。两者都不如预期。谁能推荐我一个工具?

Ber*_*Bar 6

乍一看,以下链接可以提供某种帮助:

https://asp.net-hacker.rocks/2017/02/23/word-document-formatter-in-aspnetcore.html

无论如何,关于DOCX-Microsoft使用Open XML SDK的Word文档-您可以使用2.8.1版将Open XML SDK安装到您的解决方案中。全部免费。

可在以下位置获得文档:

在GIT => https://github.com/OfficeDev/Open-XML-SDK上 ;

在MSDN上=> https://docs.microsoft.com/zh-cn/office/open-xml/open-xml-sdk

ps:MSDN文档都是关于版本SDK 2.5而不是2.8.1的,但是如GIT链接中所述,没有实质性的更改。

无论如何在Web APP CORE 2.0中使用它,都可以执行以下操作:

  1. 安装Nuget Packgaes版本SDK 2.8.1
  2. 在控制器中引用以下名称空间:

    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果只想将其保存在本地文件夹中,请执行以下操作:

    public static void CreateWordprocessingDocument(string filepath)
    {
        // Create a document by supplying the filepath. 
        using (WordprocessingDocument wordDocument =
            WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
        {
            // Add a main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
    
            // Create the document structure and add some text.
            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

并这样称呼:

 public IActionResult GenerateDocx()
    {
        string filePath = @"c:\word\Invoice.docx";
        CreateWordprocessingDocument(filePath);
    }
Run Code Online (Sandbox Code Playgroud)
  1. 如果要生成要从其浏览器保存到用户计算机的docx,请在单击链接后执行以下操作:

    // GET verb
    public IActionResult GenerateDocxBrowser()
    {
    
        // open xml sdk - 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");
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

上面的代码甚至显示了如何使用最重要的对象:正文,段落,运行和文本。请记住始终遵循此顺序!

  • 是否可以从 html 和 css 生成 .docx 文件?我想创建一个生成 docx 文件的 web api 应用程序,以及另一个需要生成 docx 的应用程序,调用我的 docx 生成器应用程序。换句话说,我想创建一个 docx 生成器,其他应用程序可以从中获得帮助。我发现在我的应用程序之间进行通信的唯一方法是使用 html 和 css 字符串来生成文件。当然,如果可能有另一种更好的方法来完成这项工作,请为我解释一下。 (3认同)