【问题标题】:How to insert a beautified JSON string into table cell in a Word document using Open XML如何使用 Open XML 将美化的 JSON 字符串插入 Word 文档中的表格单元格
【发布时间】:2021-03-25 18:45:55
【问题描述】:

我正在使用 Open XML 生成 Word 文档。该文档包含一个表格,其中一些单元格包含一个 JSON 字符串。

在插入 JSON 字符串之前,我用以下几行美化它:

Newtonsoft.Json.Linq.JToken parsedJson = 
      Newtonsoft.Json.Linq.JToken.JToken.Parse(unformattedJsonString);

string formattedJsonString = parsedJson.ToString(Formatting.Indented);

对于我的 OpenXML 代码,我从 OpenXML SDK 2.5 中的“反映代码”开始。它看起来像这样:

TableCell tableCell2 = new TableCell();

TableCellProperties tableCellProperties2 = new TableCellProperties();
TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "8640", Type = TableWidthUnitValues.Dxa };
Shading shading2 = new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "auto" };

tableCellProperties2.Append(tableCellWidth2);
tableCellProperties2.Append(shading2);

Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00006AF4", RsidParagraphProperties = "00006AF4", RsidRunAdditionDefault = "00006AF4" };

ParagraphProperties paragraphProperties2 = new ParagraphProperties();
SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines() { After = "0" };

paragraphProperties2.Append(spacingBetweenLines2);

Run run2 = new Run();

Text textSecondColumn = new Text();
textSecondColumn.Text = formattedJsonString;  // see the snippet above

run2.Append(textSecondColumn);

paragraph2.Append(paragraphProperties2);
paragraph2.Append(run2);

tableCell2.Append(tableCellProperties2);
tableCell2.Append(paragraph2);

tableRow.Append(tablePropertyExceptions);
tableRow.Append(tableCell1);
tableRow.Append(tableCell2);

所以问题来了——尽管字符串“formattedJsonString”确实被美化了,但最终文档中的所有换行符和缩进都丢失了。

我曾经在 Word Interop 中执行此操作,并且效果很好。知道如何使用 Open XML 处理这个问题吗?

顺便说一句,SDK 不会打开带有格式化/美化 Json 文本的 document.xml。

修正:我刚刚想到格式会添加换行符 - 也许每一行都需要是一个单独的段落。会试试看,如果需要,会再次修改这篇文章。

【问题讨论】:

    标签: c# json openxml


    【解决方案1】:

    你在正确的轨道上。您确实需要转换换行符,但我不认为每一行都需要是一个单独的段落(尽管我想如果你愿意的话你可以这样做)。相反,我会为每一行创建一个Run,在除第一行之外的每一行的Text 元素之前插入一个Break 元素。还要确保将每个Text 元素上的Space 属性设置为SpaceProcessingModeValues.Preserve,以确保不会丢失前导空格。 将所有Runs 添加到Paragraph 内的TableCell 中就可以了。

    我想这就是你要找的:

    static void AddJsonToParagraph(string json, Paragraph paragraph)
    {
        string[] lines = json.Split(Environment.NewLine);
        bool isFirstLine = true;
    
        foreach (var line in lines)
        {
            Run run = new Run();
    
            if (!isFirstLine)
            {
                run.Append(new Break());
            }
            isFirstLine = false;
    
            Text text = new Text
            {
                Space = SpaceProcessingModeValues.Preserve,
                Text = line
            };
            run.Append(text);
            paragraph.Append(run);
        }
    }
    

    在您的代码中,将添加run2paragraph2 的行替换为:

    AddJsonToParagraph(formattedJsonString, paragraph2);
    

    【讨论】:

    • 布赖恩 - 太棒了!谢谢。我特别感谢您注意到 SpaceProcessingModeValues.Preserve 值 - 这当然会产生巨大的差异。这里的附带好处之一是行数允许我估计大型 json 将使用多少页。这对我来说很重要,因为我将一堆这些文档合并在一起,发现我需要将合并后的文档限制在 10,000 页左右 - 否则我的最终用户很难在没有单词挂起或崩溃的情况下使用它们。
    • 没问题;很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多