【问题标题】:Word Interop Cell custom text selectionWord 互操作单元自定义文本选择
【发布时间】:2013-09-20 15:21:07
【问题描述】:

在我的情况下,我需要在表格的某些单元格中包含自定义文本。所有单元格必须具有垂直和水平对齐。 Second-Last line 必须是斜体,并且此单元格中的最后一行必须是粗体和绿色。应该是这样的:

Some regular text: some text
Some other text: some text

Italic text:
BOLD GREEN TEXT

所有这些都必须在一个单元格中。我尝试编写第一行,然后更改 cell.Range 参数并添加下一行,但不幸的是,范围方法改变了所有单元格样式。 我的代码:(t1 - 我的桌子)

 Word.Table t1 = worddocument.Tables.Add(wordrange, 8, 3, ref defaultTableBehavior, ref autoFitBehavior);
                    t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    t1.Cell(1, 2).Range.Text = "Tester: " + ini.IniReadValue("Main", "Name") + "\nDate: " + DateTime.Today.ToShortDateString() + "\n";
                    t1.Cell(1, 2).Range.Italic = 1;
                    t1.Cell(1, 2).Range.Text = "Italic text"; 

我也尝试过创建一些段落,但没有任何好处。

private Word.Paragraphs wordparagraphs;
private Word.Paragraph wordparagraph;

object oMissing = System.Reflection.Missing.Value;
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
wordparagraphs = t1.Cell(1, 2).Range.Paragraphs;
wordparagraph = (Word.Paragraph)wordparagraphs[1];
t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordparagraph.Range.Text = "Tester: " + ini.IniReadValue("Main", "Name") + "\nDate: " + DateTime.Today.ToShortDateString() + "\nComponent: " + tabControl1.SelectedTab.Name + "\n";
wordparagraph = (Word.Paragraph)wordparagraphs[2];
wordparagraph.Range.Italic = 1;
wordparagraph.Range.Text = "Italic text"; 

在结果中,“斜体文本”与第一段的第二行重叠。 那么如何更改一个单元格中的一些文本呢?

【问题讨论】:

    标签: c# ms-word interop cell office-interop


    【解决方案1】:

    所以,我用段落来做这件事。 也许它对某人有用。

    t1.Cell(1,2).Range.ParagraphFormat.LineSpacing = 12; 
    t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
    t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    object oMissing = System.Reflection.Missing.Value;
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    wordparagraphs = t1.Cell(1, 2).Range.Paragraphs;
        wordparagraphs[1].Range.Text = "Tester: " + ini.IniReadValue("Main", "Name");
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
        wordparagraphs[2].Range.Text = "Date: " + DateTime.Today.ToShortDateString();
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
        wordparagraphs[3].Range.Text = "Component: " + tabControl1.SelectedTab.Text;
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
        //freespace?
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    wordparagraphs[5].Range.Italic = 1;
    wordparagraphs[5].Range.Text = "Task status:";
    
    t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    wordparagraphs[6].Range.Bold = 1;
    wordparagraphs[6].Range.Italic = 0;
    if (impStatus.SelectedIndex == 0)
        wordparagraphs[6].Range.Font.ColorIndex = Word.WdColorIndex.wdGreen;
                           else 
        wordparagraphs[6].Range.Font.ColorIndex = Word.WdColorIndex.wdRed;
        wordparagraphs[6].Range.Font.Size = 18;
        wordparagraphs[6].Range.Text = impStatus.Text.ToUpper() ;
    

    如果有人有更好的变种 - 你可以告诉我

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多