上一篇讲到,一个Document可能会有多个Rang对象。Rang由起始和结束字符来定他的位置。
以下代码为先清空Document里的内容,再在第一行写入内容。
// Clear out any existing information.
Object start = Type.Missing;
Object end = Type.Missing;
Object unit = Type.Missing;
Object count = Type.Missing;
ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);
// Set up the header information.
start = 0;
end = 0;
rng = ThisDocument.Range(ref start, ref end);
rng.InsertBefore("Xiaopai");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();//输入回车
以下为在刚写入的内容后添加一个表格。
object missingValue = Type.Missing;
object location = 8; //注:若location超过已有字符的长度将会出错。
Word.Range rng = ThisDocument.Range(ref location, ref location);
ThisDocument.Tables.Add(rng, 3, 4, ref missingValue, ref missingValue);
以下为在刚创建的表格里添加一行
Word.Table tbl = ThisDocument.Tables[1]; //第一个表格为1,而不是0
Object beforeRow = Type.Missing;
tbl.Rows.Add(ref beforeRow); //在表格的最后添加一行
填充表格内容
tbl.Cell(1, 1).Range.Text = "shuai"; //在表格的第一行第一列填入内容。
设置单元格风格
Word.Range rngCell;
rngCell = tbl.Cell(1, 2).Range;
rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
rngCell.Font.Size = 8;
rngCell.Font.Name = "Verdana";
当时没找到合并单元格的方法。有谁知道的共享一下哈。
参考资料:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtocreatewordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoaddrowscolumnstowordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTWordtbl.asp
以下代码为先清空Document里的内容,再在第一行写入内容。
// Clear out any existing information.
Object start = Type.Missing;
Object end = Type.Missing;
Object unit = Type.Missing;
Object count = Type.Missing;
ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);
// Set up the header information.
start = 0;
end = 0;
rng = ThisDocument.Range(ref start, ref end);
rng.InsertBefore("Xiaopai");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();//输入回车
以下为在刚写入的内容后添加一个表格。
object missingValue = Type.Missing;
object location = 8; //注:若location超过已有字符的长度将会出错。
Word.Range rng = ThisDocument.Range(ref location, ref location);
ThisDocument.Tables.Add(rng, 3, 4, ref missingValue, ref missingValue);
以下为在刚创建的表格里添加一行
Word.Table tbl = ThisDocument.Tables[1]; //第一个表格为1,而不是0
Object beforeRow = Type.Missing;
tbl.Rows.Add(ref beforeRow); //在表格的最后添加一行
填充表格内容
tbl.Cell(1, 1).Range.Text = "shuai"; //在表格的第一行第一列填入内容。
设置单元格风格
Word.Range rngCell;
rngCell = tbl.Cell(1, 2).Range;
rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
rngCell.Font.Size = 8;
rngCell.Font.Name = "Verdana";
当时没找到合并单元格的方法。有谁知道的共享一下哈。
参考资料:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtocreatewordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoaddrowscolumnstowordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTWordtbl.asp