2013-09-16 25 views
0

在我的情况下,我需要在表格的某个单元格中具有自定义文本。所有单元必须具有垂直和水平对齐。倒数第二行必须是斜体,并且此单元格中的最后一行必须是粗体和绿色。它一定是这样的:Word互操作单元格自定义文本选择

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"; 

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

回答

4

所以,我这样做了段落。 也许这对别人有用。

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() ; 

还,如果有人有更好的方法 - 你能告诉我

相关问题