关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。

时间:2023-03-09 01:16:04
关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。

控制word表格单元格内部文字样式。我要将数据导出到word当中,对于word表格一个单元格中的一段文字,要设置不同的样式,比如第一行文字作为标题要居中,加粗,第二行为正常的正文。

代码如下

    public void AddSimpleTable(_Application WordApp, _Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle, List<Trip> l_tp)
{
Object Nothing = System.Reflection.Missing.Value;
//文档中创建表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , numcolumns, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = outStyle;
newTable.Borders.InsideLineStyle = intStyle;
newTable.Columns[].Width = 100f;
newTable.Columns[].Width = 315f;
string date = string.Empty;
int rowcount = ;
List<int> listHeBing = new List<int>();
//List<int> listLeft = new List<int>();
for (int i = ; i < l_tp.Count; i++)
{
if (date != l_tp[i].Date)
{
if (rowcount != )
{
WordDoc.Content.Tables[].Rows.Add(ref Nothing);
}
date = l_tp[i].Date;
newTable.Cell(rowcount, ).Range.Text = l_tp[i].Date; //合并单元格
// newTable.Cell(rowcount, 1).Merge(newTable.Cell(rowcount, 2));
listHeBing.Add(rowcount); rowcount = rowcount + ; }
WordDoc.Content.Tables[].Rows.Add();
if (string.IsNullOrEmpty(l_tp[i].Locale))
{
newTable.Cell(rowcount, ).Range.Text = l_tp[i].Time;
}
else
{ newTable.Cell(rowcount, ).Range.Text = l_tp[i].Time + @"
" + l_tp[i].Locale;
}
newTable.Cell(rowcount, ).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable.Cell(rowcount, ).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//newTable.Cell(rowcount, 1).Range.Font.Color = WdColor.wdColorDarkBlue;//设置单元格内字体颜色
SetContent(rowcount, l_tp[i], WordApp, WordDoc, newTable);
// newTable.Cell(rowcount, 2).Range.Text = GetContent(rowcount, l_tp[i], WordApp, WordDoc, newTable);
newTable.Cell(rowcount, ).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
rowcount = rowcount + ;
}
foreach (var item in listHeBing)
{
newTable.Cell(item, ).Merge(newTable.Cell(item, ));
newTable.Cell(item, ).Range.Bold = ;//设置单元格中字体为粗体
newTable.Cell(item, ).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable.Cell(item, ).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//newTable.Cell(item, 1).Range.Shading.ForegroundPatternColor = WdColor.wdColorGray40;//背景颜色
}
}
 private void SetContent(int i, Trip tp, _Application WordApp, _Document WordDoc, Microsoft.Office.Interop.Word.Table Table)
{
string rs = string.Empty;
WordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑
Table.Cell(i, ).Select();
WordApp.Selection.Font.Name = "宋体";
WordApp.Selection.Font.Size = 10.5f;
WordApp.Selection.Font.Bold = ;
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.TypeText(tp.TOPIC);
WordApp.Selection.TypeParagraph();//另起一段
WordApp.Selection.Font.Bold = ;
WordApp.Selection.TypeText(tp.Content);
if (tp.L_Compere.Count > )
{
WordApp.Selection.TypeParagraph();//另起一段
WordApp.Selection.Font.Bold = ;
WordApp.Selection.TypeText("主持:");
WordApp.Selection.TypeParagraph();//另起一段
foreach (var item in tp.L_Compere)
{
WordApp.Selection.Font.Bold = ;
WordApp.Selection.TypeText(item.Name + @" " + item.JobTitle);
WordApp.Selection.TypeParagraph();//另起一段
}
}
if (tp.L_Speech.Count > )
{
WordApp.Selection.TypeParagraph();//另起一段
WordApp.Selection.Font.Bold = ;
WordApp.Selection.TypeText("演讲嘉宾:");
WordApp.Selection.TypeParagraph();//另起一段
foreach (var item in tp.L_Speech)
{
WordApp.Selection.Font.Bold = ;
WordApp.Selection.TypeText(item.Name + @" " + item.JobTitle);
WordApp.Selection.TypeParagraph();//另起一段
}
}
}