Fork me on GitHub

控制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, 1, numcolumns, ref Nothing, ref Nothing);
           //设置表格样式
           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;//设置单元格内字体颜色
               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;//设置单元格中字体为粗体
               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;//激活页面内容的编辑 
           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();//另起一段 
               }
           }
       }       

 

posted on 2014-11-03 16:02  乱花渐欲迷人眼  阅读(385)  评论(0编辑  收藏  举报