相比较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, 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,
- 11 ref missingValue,
- 12 ref missingValue,
- 13 ref missingValue,
- 14 ref missingValue, ref missingValue); //保存word文档
- 15
- 16 document.close(); // 关闭文档
- 17 wordApplication.Quit(ref missingValue, ref missingValue); //退出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,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,11 ref missingValue,12 ref missingValue,13 ref missingValue,14 ref missingValue,ref missingValue); //保存word文档 15 16 document.close(); // 关闭文档 17 wordApplication.Quit(ref missingValue,ref missingValue); //退出word进程。
到此一个word文档就导出成功了。
还有第二种方式,一格一格生成表格的格式,这是最麻烦,最费时的一种方式,我也是研究了好久才做出来的。话不多说,直接上代码。
- 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, 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 = "自办单位名称:" + 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, ref Tend);
- table = doc.Tables.Add(tableLocation, 5, 8, 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 = VistMeberInPermit.AFName;
- 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 = VistMeberInPermit.BLName;
- 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 = @SavetemplatePath + "\\保存的名称" + "-" + System.DateTime.Now.ToString("yyyyMMdd HHmmss");
- wordApplication.ActiveDocument.SaveAs(ref SavePath,
- ref missingValue,
- ref missingValue, ref missingValue); //保存文档
- 如果想要换页,则要用到@R_188_6404@,代码如下:
- dynamic para;
- para = doc.Content.Paragraphs.Add(ref missingValue);
- object pBreak = 0;
- para.Range.InsertBreak(ref pBreak);
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,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 = "自办单位名称:" + 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,ref Tend); table = doc.Tables.Add(tableLocation,5,8,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 = VistMeberInPermit.AFName; 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 = VistMeberInPermit.BLName; 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 = @SavetemplatePath + "\\保存的名称" + "-" + System.DateTime.Now.ToString("yyyyMMdd HHmmss"); wordApplication.ActiveDocument.SaveAs(ref SavePath,ref missingValue); //保存文档 如果想要换页,则要用到@R_188_6404@,代码如下: dynamic para; para = doc.Content.Paragraphs.Add(ref missingValue); object pBreak = 0; para.Range.InsertBreak(ref pBreak);导出word表格的方法就写到这,导出excel的下次再分享。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。