C#:编辑/保存/发送docx文档

时间:2018-10-19 14:08:15

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

曾经遇到很多问题。我想在ASP.NET Core服务器上使用OpenXML,我想基于一个模板创建一个新的docx文档。完全保存完此文档后,我希望将其发送给我的客户,以便他可以直接下载。这是我的代码:

public IActionResult Post([FromBody] Consultant consultant)
    {
            using (Stream templateStream = new MemoryStream(Properties.Resources.templateDossierTech))
            using (WordprocessingDocument template =
                WordprocessingDocument.Open(templateStream, false))
            {

                string fileName = environment.WebRootPath + @"\Resources\"+ consultant.FirstName + "_" + consultant.LastName + ".docx";
                WordprocessingDocument dossierTechniqueDocument =
                    WordprocessingDocument.Create(fileName,
                        WordprocessingDocumentType.Document);
                foreach (var part in template.Parts)
                {
                    dossierTechniqueDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
                }

                var body = dossierTechniqueDocument.MainDocumentPart.Document.Body;
                var paras = body.Elements();

                foreach (var para in paras)
                {
                    foreach (var run in para.Elements())
                    {
                        foreach (var text in run.Elements())
                        {
                            if (text.InnerText.Contains("{{prenom}}"))
                            {
                                var t = new Text(text.InnerText.Replace("{{prenom}}", consultant.FirstName));
                                run.RemoveAllChildren<Text>();
                                run.AppendChild(t);
                            }

                        }
                    }
                }
                dossierTechniqueDocument.MainDocumentPart.Document.Save();
                dossierTechniqueDocument.Close();
                var cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = consultant.FirstName + "_" + consultant.LastName + ".docx",
                    Inline = true
                };

                Response.Headers.Add("Content-Disposition", cd.ToString());

                Response.Headers.Add("X-Content-Type-Options", "nosniff");
                return File(System.IO.File.ReadAllBytes(fileName),"application/vnd.openxmlformats-officedocument.wordprocessingml.document","Dossier Technique");
        }
    }

乍一看,看起来好像保存得很好,但是当我尝试以文字形式打开它时,它说它由于某种原因已损坏。 当我尝试发送它时,这是同样的问题。发送后,我的客户端不会下载它(Ajax查询)。 你们当中有人知道如何解决吗?

1 个答案:

答案 0 :(得分:1)

这是从模板创建文档的功能:

static void GenerateDocumentFromTemplate(string inputPath, string outputPath)
    {
        MemoryStream documentStream;
        using (Stream stream = File.OpenRead(inputPath))
        {
            documentStream = new MemoryStream((int)stream.Length);
            CopyStream(stream, documentStream);
            documentStream.Position = 0L;
        }

        using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
        {
            template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = template.MainDocumentPart;
            mainPart.DocumentSettingsPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
               new Uri(inputPath, UriKind.Absolute));

            mainPart.Document.Save();
        }


        File.WriteAllBytes(outputPath, documentStream.ToArray());
    }