相比较Asp.net而言,silverlight 导出word或者excel文档要麻烦的多。silverlight 4 只能在OOB模式下,使用Dynamic关键字(如 Dynamic Table)才能调用word组件导出文档。导出word有两种方法,第一种是导入一个word模版,然后在指定的位置填入文字信息;第二种是一格一格生成表格模版,但这样效率将会很底下。
首先,介绍第一种用模版的方式。这种方式是最简单,效率最高的方式。适用于导出的word表格有固定的格式。废话不多说,直接把代码贴出来。
- 1 string TemplatePath = @templatepath + "\\出国机票申请流程.doc"; //定义引用模版的路径
- 2 object missingValue = System.Reflection.Missing.Value; //使用反射定义默认参数
- 3 dynamic wordApplication = AutomationFactory.CreateObject("Word.Application"); //创建WORD进程,必须在OOB模式下才能运行。
- 4 dynamic document = wordApplication.Documents.Add(ref TemplatePath, ref missingValue,
- 5 ref missingValue);//添加一个WORD文档
- 6 wordApplication.Visible = false; //设置文档的可见性。
- 7 dynamic table = document.Tables(1); // 定义模版中的表格,1表示是模版中的第一个表格
- 8 table.Cell(1, 3).Range.Text = Group.ApprovalNumber + "," + Group.Name + "共" + Group.VisitNumber + "人"; //定义table 单元格的文本
- 9 string SavePath = @SavetemplatePath + "\\出国机票申请流程" + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmss"); //定义保存的路径
- 10 wordApplication.ActiveDocument.SaveAs(ref SavePath,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px">11 12 13 14 ref missingValue); //保存word文档
- 15
- 16 document.close(); // 关闭文档
- 17 wordApplication.Quit(//退出word进程。
到此一个word文档就导出成功了。
还有第二种方式,一格一格生成表格的格式,这是最麻烦,最费时的一种方式,我也是研究了好久才做出来的。话不多说,直接上代码。
copy
- object missingValue = System.Reflection.Missing.Value;
- dynamic wordApplication = AutomationFactory.CreateObject("Word.Application");
- wordApplication.Visible = false;
- 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,153); background-color:inherit; font-weight:bold">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,153); background-color:inherit; font-weight:bold">ref end1);
- rng1.Text = "自办单位名称:" + VistMeberInPermit.IUnion + "" + "团组号:" + "";
- 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,153); background-color:inherit; font-weight:bold">ref Tend);
- table = doc.Tables.Add(tableLocation, 5, 8, //在指定位置插入表格
- table.Borders.OutsideLinestyle = 1; //显示表格的边框线
- table.Borders.InsideLinestyle = 1;
- table.cell(1, 1).Range.Text = "姓";
- table.cell(1, 1).Range.font.size = 14;
- "楷体_GB2312";
- //设置单元格的宽度
- aragraphFormat.Alignment = 1; //设置单元格垂直的居中方式
- "楷体_GB2312";
- aragraphFormat.Alignment = 1;
- "名";
- "楷体_GB2312";
- aragraphFormat.Alignment = 1;
- "楷体_GB2312";
- aragraphFormat.Alignment = 1;
- string SavePath = @SavetemplatePath + "\\保存的名称" + "-" + System.DateTime.Now.ToString("yyyyMMdd HHmmss");
- wordApplication.ActiveDocument.SaveAs( //保存文档
- 如果想要换页,则要用到@R_537_6404@,代码如下:
- dynamic para;
- para = doc.Content.Paragraphs.Add(ref missingValue);
- object pBreak = 0;
- para.Range.InsertBreak(ref pBreak);