OpenXml sdk 损坏文件

OpenXml sdk corrupt document

我正在使用 open xml sdk 来替换占位符,在 Whosebug 帖子的许多帮助下,我找到了如何替换我的占位符。更改这些占位符似乎可行,我可以检查更改后的 xml,但出于某种原因,无论如何文档似乎都已损坏。

我尝试了什么:

更改我的 docx zo ip 提取并手动替换一些文本并返回到 zip -> docx 也已损坏。

我的代码:

public void GenerateInvoice(TemplateViewModel viewModel)
        {
            //step, copy file -> read document return body -> change body -> save body -> convert to pdf
            
            
            //read document
            var wordReader = new DocumentReader();
            //var result = wordReader.Reader(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx", @"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx");
            
            if (!File.Exists(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx"))
            {
                throw new FileNotFoundException("tbesta ni kloot");
            }

            File.Copy(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx", @"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", false);

            var stream = File.Open(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", FileMode.Open);
            
            List<string> innerTexts = new List<string>();
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
            {
                //replace item
                var elements = wordDocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().ToList();

                foreach (var item in elements)
                {
                    switch (item.InnerText)
                    {
                        case nameof(TemplateOptions.UserDivision):
                            //replace alias
                            //var alias = item.Descendants<SdtAlias>().FirstOrDefault();
                            //alias.Val.Value = viewModel.Category.Name;

                            //replace child element
                            //var run = item.Descendants<SdtContentRun>().FirstOrDefault();

                            //run.Parent.ReplaceChild(new Run(new Text(viewModel.Category.Name)),run);

                            //var innerRun = run.Descendants<Run>().FirstOrDefault();
                            //var text = run.Descendants<Text>().FirstOrDefault();
                            //text.Text = viewModel.Category.Name;


                            var runs = wordDocument.MainDocumentPart.Document.Body.Descendants<Run>().FirstOrDefault();

                            var text = runs.Descendants<Text>().FirstOrDefault();
                            text.Text =  text.Text.Replace("DEIGON", "TEST");
                            
                            break;

                        case nameof(TemplateOptions.ClientAddress):
                            break;

                        case nameof(TemplateOptions.ClientCompany):
                            break;

                        case nameof(TemplateOptions.ClientVat):
                            break;

                        case nameof(TemplateOptions.HourlyRate):
                            break;

                        case nameof(TemplateOptions.InvoiceComments):
                            break;

                        case nameof(TemplateOptions.InvoiceDate):
                            break;

                        case nameof(TemplateOptions.InvoiceHours):
                            break;

                        case nameof(TemplateOptions.InvoiceId):
                            break;

                        case nameof(TemplateOptions.InvoicePrice):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalExclusive):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalInclusive):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalVat):

                            break;

                        case nameof(TemplateOptions.UserBIC):
                            break;

                        case nameof(TemplateOptions.UserIBAN):
                            break;

                        case nameof(TemplateOptions.UserRightsPolicy):
                            break;

                        default:
                            break;

                    }
                }

                wordDocument.Save();
            }            

            //wordReader.SaveChanges(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", result);


        }

简单的文本替换有效: 差异检查:

有谁知道为什么最后我的文档损坏了?

使用 .net 5.0 和 blazor 前端

您永远不会关闭或处理 stream

最简单的解决方案是使用 WordprocessingDocument.Open(<fileName>, ...) 重载并消除流。否则,给流它自己的 using() { } 块..