Je suis nouveau sur le développement C# et je suis censé générer un document Word par programmation.Mettre en forme des mots spécifiques à l'intérieur de la cellule du tableau Word
À un certain moment, je fais ceci:
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;
Ma sortie dans le document ressemble à ceci:
cellule est d'abord ce que je veux formater (le item.FullName
).
Cependant, il devrait ressembler à ceci:
Toute pensée?
Edit: Voici comment je crée cette chaîne que je dois donner des couleurs:
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: Je l'ai déjà mis en œuvre une méthode comme ceci:
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;
}
il n'y a aucune raison de s'excuser pour votre anglais, ce n'est pas mal du tout! – FrankK
Utilisez-vous Word Interop ou une autre bibliothèque? – Arul
Il est basé sur Interop, mais j'utilise la bibliothèque NetOffice. La raison pour laquelle je fais ceci est parce que la seule condition pour l'utilisateur est d'avoir le mot installé, il ne mater quelle version. –