2017-10-13 5 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ライブラリを使用しています。私はこれをやっている理由は、ユーザーのための唯一の要件は、単語がインストールされていることです、それはどのバージョンのmaterされていません。 –

答えて

0

:私はすでにこのようなメソッドを実装しています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"); 

Result of the below code
結果また、私はt1.Style = style;をコメントアウトしていることに注意してください。必要に応じてコメントを外します。

+0

アプローチを開始するのは良い考えです。私はこの方法で実装しようとします。それが私のために働くかどうかを知らせます。 "item.FullName"は単語に分割され、フォーマットされたテキストを ".rtf"ファイルに出力します。その後、ファイルを段落としてセルに挿入します。その後、私はこの一時ファイルを削除します。提案ありがとう! –

+0

なぜフォーマットされたテキストを ".rtf"ファイルに出力する必要がありますか?その後、そのファイルをセルの段落として挿入します。その後、一時ファイルを削除しますか? – Arul

+0

私のLEを見てください。段落に.rtfファイルの内容を直接挿入することができます。セルから各単語を取得するよりも、ファイルのテキストを書式設定する方が便利です。私が間違っていると思えば私を訂正してください。 –

関連する問題