关于.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();//另起一段
}
}
}
关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。的更多相关文章
- C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件
项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...
- Microsoft.Office.Interop.Excel Find 操作
public void SearchLoactions(string filepath, int start, int end ,string expectvalue) { if (end >= ...
- (转)无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此操作失败的原因是对 IID 为“{00020970-
HRESULT:0x80030002 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft ...
- 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Wor ...
- System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
报错:System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接 ...
- 调用Microsoft.Office.Interop.Word生成自定义Word文档
具体思路: 1.先制作Word模版,使用文本框+书签的方式来设计模版: 2.模版制作完之后,根据模版生成新文件,使用File.Copy方法,生成.doc格式新文件: 3.后台取得数据,参照网页渲染的方 ...
- C#用Microsoft.Office.Interop.Word进行Word转PDF的问题
之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...
- Microsoft.Office.Interop.Word 创建word
Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...
- VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE
在使用vs2005. vs2008. vs2010 制作包含 word等office的应用程序时,有时找不到对Microsoft.Office.Core. Microsoft.Office.Inter ...
随机推荐
- javascript 利用匿名函数对象给你异步回调方法传参数
先来创建一个匿名函数对象: /*** * 匿名函数 */ var callChangeBtn=new function(bugBtn){ this.chage=function(json){ bugB ...
- 读取其他软件listview控件的内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- asp.net gridview 鼠标悬浮提示信息
使用场景: gridview绑定数据,某列数据太多,故超过一定字符,隐藏起来,同时鼠标移到指定列显示其明细信息: 知识点: 1,oderListTbl_DataBound事件中,添加,oderList ...
- access生成sql脚本,通过VBA调用ADOX
access生成sql脚本,通过VBA调用ADOX. 使用 MS Access 2016 的VBA,读取mdb文件中的所有表结构(数据类型/长度/精度等),生成对应的SQL create table语 ...
- Mysql大范围分页优化案例
在BBS线上业务抓到如下分页SQL: meizu_bbs meizu_bbs Query Sending data , meizu_bbs meizu_bbs Query Sending data , ...
- Map以及Set的遍历(EntrySet方法,补充enumeration和Iterator的区别)
public void mearge(Map map) { Map returnMap = new HashMap<>(); // 转换为Entry Set<Map.Entry< ...
- Stanford NLP 学习笔记2:文本处理基础(text processing)
I. 正则表达式(regular expression) 正则表达式是专门处理文本字符串的正式语言(这个是基础中的基础,就不再详细叙述,不了解的可以看这里). ^(在字符前): 负选择,匹配除括号以外 ...
- jdbc数据源配置
initCtx.lookup("java:comp/env") 在 容器的conf/ xxxconf.xml http://yuxiatongzhi.iteye.com/blog/ ...
- Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)
用我的方法来控制其他程序窗体上的窗口控件,必须先了解什么是 回调函数.我的理解是这样的: 回 调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等 ...
- 作业总结(一):IE6下面的那些坑
考完试就来实习的公司实习了,大概最近有两周时间就一直在做公司给新人布置的大作业.虽然只是很简单的一个小的项目,但却从其中总结到了不少有用的东西.计划将其发出来一系列文章,算是对这两周时间的总结.也算是 ...