【问题标题】:Format specific words inside Word Table cell格式化字表单元格中的特定字词
【发布时间】:2023-03-15 19:12:01
【问题描述】:

我是 C# 开发的新手,我应该以编程方式生成 Word 文档。

在某些时候我正在这样做:

Paragraph p1 = document.Paragraphs.Add();
Table t1 = p1.Range.Tables.Add(p1.Range, 2, 1, Missing.Value, Missing.Value);
t1.Range.Font.Size = 11;
t1.Style = style;
t1.Rows[1].Alignment = WdRowAlignment.wdAlignRowCenter;
t1.Cell(1, 1).Range.Select();
document.Application.Selection.TypeText(item.FullName + "");
t1.Cell(2, 1).Range.Select();
document.Application.Selection.TypeText(item.swca_description + "");
t1.Cell(2, 1).Range.Bold = 0;

我在文档中的输出如下所示:

第一个单元格是我打算格式化的(item.FullName)。

但是它应该看起来像这样:

有什么想法吗?

编辑: 这就是我创建需要赋予颜色的字符串的方式:

private string GetFullName()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append(this.swc_datatype == null ? "void" : this.swc_datatype.swcdt_name);
        sb.Append($" {this.swca_name}(");

        foreach (swc_api_parameter inputParameter in this.swc_api_parameter)
            sb.Append($"{inputParameter.swc_datatype?.swcdt_name} {inputParameter.swcap_name},");

        if (swc_api_parameter.Any())
            sb.Length = sb.Length - 1;

        sb.Append(")");
        return sb.ToString();
    }

LE:我已经实现了这样的方法:

        public static Paragraph AddRtfTextFromFile(this Document document, string rtfPath)
    {
        Paragraph p = document.Paragraphs.Add();
        p.Range.InsertFile(rtfPath, Missing.Value, false);
        p.Range.Font.Bold = 0;
        p.Range.Font.Name = "Calibri";
        p.Range.Font.Size = 12;
        p.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
        p.Range.PageSetup.VerticalAlignment = WdVerticalAlignment.wdAlignVerticalTop;
        p.Range.InsertParagraphAfter();

        return p;
    }

【问题讨论】:

  • 没有理由为你的英语道歉,一点也不差!
  • 您使用的是 Word Interop 还是任何其他库?
  • 它基于 Interop,但我使用的是 NetOffice 库。我这样做的原因是因为对用户的唯一要求是安装 word,它与哪个版本无关。
  • 该单元格中的输出看起来像带有保留字的类定义。 “Void”有一种颜色,类名另一种颜色,参数类型等等。这些字段是在我的应用程序中的某个时候引入的。这是一个组合字符串。我可以把它分成单独的词......但我的问题是给它们特定的颜色。
  • 如果您可以将item.FullName 拆分为单词,一种方法是键入单词,选择单词,应用颜色,折叠选择,键入下一个单词并重复。

标签: c# .net ms-word


【解决方案1】:

如果您可以将item.FullName 拆分为单词,一种方法是键入单词,选择单词,应用颜色,折叠选择,键入下一个单词并重复。我不知道是否还有其他方法可以解决此问题。但这可能是实现您需要的一种方式。


以下代码的结果

        Document document = new Document();
        Paragraph p1 = document.Paragraphs.Add();
        Table t1 = p1.Range.Tables.Add(p1.Range, 2, 1, Missing.Value, Missing.Value);
        t1.Range.Font.Size = 11;
        //t1.Style = style;
        t1.Rows[1].Alignment = WdRowAlignment.wdAlignRowCenter;
        t1.Cell(1, 1).Range.Select();

        document.Application.Selection.TypeText("void ");
        document.Application.Selection.MoveLeft(WdUnits.wdCharacter, "void ".Length, true);
        document.Application.Selection.Font.Color = WdColor.wdColorSkyBlue;
        document.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);

        document.Application.Selection.TypeText("item.FullName");
        document.Application.Selection.MoveLeft(WdUnits.wdCharacter, "item.FullName".Length, true);
        document.Application.Selection.Font.Color = WdColor.wdColorRed;
        document.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);


        t1.Cell(2, 1).Range.Select();
        document.Application.Selection.TypeText("item.swca_description");            
        document.Application.Selection.MoveLeft(WdUnits.wdCharacter, "swca_description".Length, true);
        document.Application.Selection.Font.Color = WdColor.wdColorBlack;
        document.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
        t1.Cell(2, 1).Range.Bold = 0;
        document.SaveAs(@"D:\Test.docx");

另外,请注意我已经注释掉了t1.Style = style;。根据需要取消注释。

【讨论】:

  • 开始这个方法听起来是个好主意。我将尝试以这种方式实现。我会让你知道它是否对我有用。 “item.FullName”将被分割成单词,我会将格式化的文本输出到“.rtf”文件中,然后我会将文件作为段落插入单元格中。之后我将删除这个临时文件。感谢您的建议!
  • 为什么需要将格式化文本输出到“.rtf”文件中,然后将文件作为段落插入单元格中。之后删除临时文件?
  • 看看我的 LE。我可以将 .rtf 文件的内容直接插入到段落中,而且我从文件中格式化文本比从单元格中获取每个单词更方便。如果我想错了,请纠正我。
  • 我有一个非常复杂的文档作为输出,其中包含多个动态创建的表格、图像、不同的缩进、标题、标题、包含表等。
  • 好的。我明白了。
猜你喜欢
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2014-01-10
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 2021-12-14
相关资源
最近更新 更多