在我的情况下,我需要在表格的某个单元格中包含自定义文本。所有单元格必须具有垂直和水平对齐。第二个最后一行必须是斜体,此单元格中的最后一行必须是粗体和绿色。它必须像:
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";
结果“斜体文本”与第一段的第二行重叠。 那么如何在一个单元格中更改一些文本呢?
答案 0 :(得分: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() ;
如果有人有更好的变体 - 你可以告诉我