2017-10-13 175 views
0

我是C#开发新手,我应该以编程方式生成Word文档。在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; 

我的文档输出看起来是这样的: enter image description here

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

然而,它应该是这样的: enter image description here

有什么想法?

编辑: 这是我如何创建一个字符串,我需要给颜色:

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; 
    } 
+0

没有理由为你的英语道歉,它一点也不坏! – FrankK

+0

您是使用Word Interop还是其他库? – Arul

+0

它基于Interop,但我使用的是NetOffice库。我这样做的原因是因为用户的唯一要求是安装字,它不适用于哪个版本。 –

回答

0

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

Result of the below code
下面的代码

 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;。根据需要取消注释。

+0

听起来像是一个好主意,开始这种方法。我会尽力以这种方式实施。如果它适合我​​,我会让你知道。 “item.FullName”将被分成单词,我将输出格式化文本到“.rtf”文件中,然后我将文件作为段落插入到单元格中。之后,我将删除此临时文件。感谢您的建议! –

+0

为什么需要将格式化文本输出到“.rtf”文件中,然后将文件作为段落插入单元格中。之后,删除临时文件? – Arul

+0

看看我的LE。我可以直接插入段落中的.rtf文件的内容,对于我来说,从文件格式化文本比从单元格中获取每个单词更方便。纠正我,如果我想错了。 –

相关问题