public void dateToWord()
{
string TemplatePath = @"http://aaa.doc"; //定义引用模版的路径
//string TemplatePath = @"C:/1234.doc"; //定义引用模版的路径
object missingValue = System.Reflection.Missing.Value; //使用反射定义默认参数
dynamic wordApplication = AutomationFactory.CreateObject("Word.Application"); //创建WORD进程,必须在OOB模式下才能运行。
wordApplication.Visible = true;
dynamic document = wordApplication.Documents.Add(ref TemplatePath, ref missingValue, ref missingValue, ref missingValue);//添加一个WORD文档
//wordApplication.Visible = false; //设置文档的可见性。
dynamic table = document.Tables(1); // 定义模版中的表格,1表示是模版中的第一个表格
//定义table 单元格的文本
//
table.Cell(1, 3).Range.Text = "";
table.Cell(2, 3).Range.Text = "";
table.Cell(3, 3).Range.Text = "";
//
table.Cell(4, 4).Range.Text = "";
table.Cell(5, 4).Range.Text = "";
table.Cell(6, 4).Range.Text = "";
table.Cell(7, 4).Range.Text = "";
table.Cell(8, 4).Range.Text = "";
table.Cell(9, 4).Range.Text = "";
table.Cell(10, 4).Range.Text = "";
table.Cell(11, 4).Range.Text = "";
table.Cell(12, 4).Range.Text = "";
table.Cell(13, 4).Range.Text = "";
//
table.Cell(14, 3).Range.Text = "";
table.Cell(14, 5).Range.Text = "";
table.Cell(14, 7).Range.Text = "";
//
table.Cell(15, 4).Range.Text = "";
table.Cell(16, 4).Range.Text = "";
table.Cell(17, 4).Range.Text = "";
table.Cell(18, 5).Range.Text = "23";
table.Cell(18, 7).Range.Text = "21";
table.Cell(18, 9).Range.Text = "20";
table.Cell(19, 4).Range.Text = "";
table.Cell(20, 5).Range.Text = "11";
table.Cell(20, 7).Range.Text = "12";
table.Cell(20, 9).Range.Text = "13";
//
table.Cell(21, 3).Range.Text = "";
table.Cell(22, 3).Range.Text = "";
//
table.Cell(23, 3).Range.Text = "";
table.Cell(24, 3).Range.Text = "";
//
table.Cell(25, 2).Range.Text = "";
UpdateWordContent(document, 1, 1, 3, "aoe");
}
第二种:手动画表格
private void button1_Click(object sender, RoutedEventArgs e)
{
// object missingValue = System.Reflection.Missing.Value; //使用反射定义默认参数
// dynamic word = AutomationFactory.CreateObject("Word.Application");
//word.Visible = true;
//dynamic doc = word.Documents.Add();
//dynamic table = doc.Tables(1); // 定义模版中的表格,1表示是模版中的第一个表格
//table.Cell(1, 3).Range.Text = "123";
//string Insertxt = "这是Silverlight操作Office Word测试。欢迎大家访问我的博客 http://jv9.cnblogs.com";
//dynamic range = doc.Range(0, 0);
// range.Text = Insertxt;
object missingValue = System.Reflection.Missing.Value;
dynamic wordApplication = AutomationFactory.CreateObject("Word.Application");
wordApplication.Visible = true;
dynamic doc = wordApplication.Documents.Add();
wordApplication.ActiveDocument.pagesetup.topmargin = 13.3; //设置word文档的上边距
wordApplication.ActiveDocument.pagesetup.bottommargin = 53.3;//设置word文档的下边距
wordApplication.ActiveDocument.pagesetup.leftmargin = 56.7;//设置word文档的左边距
wordApplication.ActiveDocument.pagesetup.rightmargin = 56.7;//设置word文档的右边距
dynamic rng = wordApplication.Range;
int start = doc.Characters.Count - 1; //定义文本的坐标
int end = doc.Characters.Count - 1;
rng = doc.content;
rng = doc.Range(ref start, ref end);
rng.Text = "文档标题" + "\r\n";
rng.font.size = 22;
rng.font.name = "黑体"; //设置字体
rng.ParagraphFormat.Alignment = 1; //设置水平居中
dynamic rng1 = wordApplication.Range;
int start1 = doc.Characters.Count - 1;
int end1 = doc.Characters.Count - 1;
rng1 = doc.content;
rng1 = doc.Range(ref start1, ref end1);
rng1.Text = "自办单位名称:" + "ck168" + "" + "团组号:" + "";
rng1.font.size = 12;
rng1.font.name = "楷体_GB2312";
dynamic table;
int Tstart = doc.Characters.Count - 1;
int Tend = doc.Characters.Count - 1;
Object tableLocation = doc.Range(ref Tstart, ref Tend);
table = doc.Tables.Add(tableLocation, 5, 8, ref missingValue, ref missingValue); //在指定位置插入表格
table.Borders.OutsideLineStyle = 1; //显示表格的边框线
table.Borders.InsideLineStyle = 1;
table.cell(1, 1).Range.Text = "姓";
table.cell(1, 1).Range.font.size = 14;
table.cell(1, 1).Range.font.name = "楷体_GB2312";
table.cell(1, 1).width = 45.1f; //设置单元格的宽度
table.cell(1, 1).Range.ParagraphFormat.Alignment = 1; //设置单元格垂直的居中方式
table.cell(1, 2).width = 58.8f;
table.cell(1, 2).Range.Text = "ck";
table.cell(1, 2).Range.font.size = 14;
table.cell(1, 2).Range.font.name = "楷体_GB2312";
table.cell(1, 2).Range.ParagraphFormat.Alignment = 1;
table.cell(1, 3).Range.Text = "名";
table.cell(1, 3).Range.font.size = 14;
table.cell(1, 3).Range.font.name = "楷体_GB2312";
table.cell(1, 3).width = 45.1f;
table.cell(1, 3).Range.ParagraphFormat.Alignment = 1;
table.cell(1, 4).width = 90.3f;
table.cell(1, 4).Range.Text = "caokai";
table.cell(1, 4).Range.font.size = 14;
table.cell(1, 4).Range.font.name = "楷体_GB2312";
table.cell(1, 4).Range.ParagraphFormat.Alignment = 1;
string SavePath = "c:" + "\\保存的名称" + "-" + System.DateTime.Now.ToString("yyyyMMdd HHmmss");
wordApplication.ActiveDocument.SaveAs(ref SavePath,
ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue); //保存文档
//如果想要换页,则要用到分页符,代码如下:
//dynamic para;
//para = doc.Content.Paragraphs.Add(ref missingValue);
//object pBreak = 0;
//para.Range.InsertBreak(ref pBreak);
}