WordProcessing文档

时间:2016-02-29 15:22:14

标签: c# openxml

我正在使用openxml中的WordprocessingDocument类在C#中工作。我的情况:我得到了一些需要转换成docx的mht文件。问题在于表格。在某些情况下,数据不会正确分解为新页面,在一个页面中留下一个空标题,在下一页中留下表格行。

enter image description here

Word中的神奇修复似乎是一个名为“与下一步保持”的检查。如果我将它应用于表格标题行,一切都很好,但这不是我们的客户每次都想做的事情。

我已经发现我需要在代码中执行以下操作:

new TableRow(
    new TableRowProperties(
        new CantSplit()),

但是我的问题是我将一些外部页面作为AltChunk导入到文档中(在示例中整个docx是从代码构建的),所以我不知道如何在我的应用程序中应用CanSplit()类表。关于如何从AltChunk获取表格并应用CanSplit()的任何想法?!

我已经花了很长时间找出使这项工作正常的方法,但无可救药地无济于事。顺便说一句,我也试过添加page-break-inside:避免样式到我的表头,但不出意外:它没有按预期工作。对此事有何想法?

当前代码如下所示:

var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["UploadPath"]),
            Guid.NewGuid().ToString());
        if (!System.IO.File.Exists(path))
            Directory.CreateDirectory(path);

        var template = GetUserTemplate(personId);
        var mhtmlMessage = GetMhtml(path, template, personId);
        var fileFullPath = WriteFile(path, template + ".mht", mhtmlMessage);
        var person = _personService.GetById(personId);
        var filename = ConfigurationManager.AppSettings["CustomerName"] + "_" + person.FirstName + "_" + person.SurName + ".docx";

 using (var generatedDocument = new MemoryStream()){
            using (var myDoc = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)){
                var mainPart = myDoc.MainDocumentPart;
                if (mainPart == null){
                    mainPart = myDoc.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
                }
                const string altChunkId = "AltChunkId1";
                var chunk = mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.Mht, altChunkId);
                chunk.FeedData(System.IO.File.Open(fileFullPath, FileMode.Open));

                var altChunk = new AltChunk{Id = altChunkId};

                // ReSharper disable once PossiblyMistakenUseOfParamsMethod
                mainPart.Document.Body.Append(altChunk);

                // I would like to access the tables inside the mht (altchunk) here and apply CantSplit() to each row.
                myDoc.MainDocumentPart.Document.Save();
            }

0 个答案:

没有答案
相关问题