关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...
生活随笔
收集整理的這篇文章主要介紹了
关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
控制word表格單元格內(nèi)部文字樣式。我要將數(shù)據(jù)導(dǎo)出到word當(dāng)中,對于word表格一個(gè)單元格中的一段文字,要設(shè)置不同的樣式,比如第一行文字作為標(biāo)題要居中,加粗,第二行為正常的正文。
代碼如下
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;//文檔中創(chuàng)建表格Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 1, numcolumns, ref Nothing, ref Nothing);//設(shè)置表格樣式newTable.Borders.OutsideLineStyle = outStyle;newTable.Borders.InsideLineStyle = intStyle;newTable.Columns[1].Width = 100f;newTable.Columns[2].Width = 315f;string date = string.Empty;int rowcount = 1;List<int> listHeBing = new List<int>();//List<int> listLeft = new List<int>();for (int i = 0; i < l_tp.Count; i++){if (date != l_tp[i].Date){if (rowcount != 1){WordDoc.Content.Tables[1].Rows.Add(ref Nothing);}date = l_tp[i].Date;newTable.Cell(rowcount, 1).Range.Text = l_tp[i].Date;//合并單元格// newTable.Cell(rowcount, 1).Merge(newTable.Cell(rowcount, 2)); listHeBing.Add(rowcount);rowcount = rowcount + 1;}WordDoc.Content.Tables[1].Rows.Add();if (string.IsNullOrEmpty(l_tp[i].Locale)){newTable.Cell(rowcount, 1).Range.Text = l_tp[i].Time;}else{newTable.Cell(rowcount, 1).Range.Text = l_tp[i].Time + @"" + l_tp[i].Locale;}newTable.Cell(rowcount, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中newTable.Cell(rowcount, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中//newTable.Cell(rowcount, 1).Range.Font.Color = WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色 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, 2).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//水平居左rowcount = rowcount + 1;}foreach (var item in listHeBing){newTable.Cell(item, 1).Merge(newTable.Cell(item, 2));newTable.Cell(item, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體newTable.Cell(item, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中newTable.Cell(item, 1).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;//激活頁面內(nèi)容的編輯 Table.Cell(i, 2).Select();WordApp.Selection.Font.Name = "宋體";WordApp.Selection.Font.Size = 10.5f;WordApp.Selection.Font.Bold = 1;WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;WordApp.Selection.TypeText(tp.TOPIC);WordApp.Selection.TypeParagraph();//另起一段 WordApp.Selection.Font.Bold = 0;WordApp.Selection.TypeText(tp.Content);if (tp.L_Compere.Count > 0){WordApp.Selection.TypeParagraph();//另起一段 WordApp.Selection.Font.Bold = 1;WordApp.Selection.TypeText("主持:");WordApp.Selection.TypeParagraph();//另起一段 foreach (var item in tp.L_Compere){WordApp.Selection.Font.Bold = 0;WordApp.Selection.TypeText(item.Name + @" " + item.JobTitle);WordApp.Selection.TypeParagraph();//另起一段 }}if (tp.L_Speech.Count > 0){WordApp.Selection.TypeParagraph();//另起一段 WordApp.Selection.Font.Bold = 1;WordApp.Selection.TypeText("演講嘉賓:");WordApp.Selection.TypeParagraph();//另起一段 foreach (var item in tp.L_Speech){WordApp.Selection.Font.Bold = 0;WordApp.Selection.TypeText(item.Name + @" " + item.JobTitle);WordApp.Selection.TypeParagraph();//另起一段 }}}?
轉(zhuǎn)載于:https://www.cnblogs.com/laoyuanroom/p/4071427.html
總結(jié)
以上是生活随笔為你收集整理的关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络之应用层:4、电子邮件、SMT
- 下一篇: 计组之数据运算:5、加减运算与溢出判断