微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 导出各种格式的Word和Excel表格 .

相比较Asp.net而言,silverlight 导出word或者excel文档要麻烦的多。silverlight 4 只能在OOB模式下,使用Dynamic关键字(如 Dynamic Table)才能调用word组件导出文档。导出word有两种方法,第一种是导入一个word模版,然后在指定的位置填入文字信息;第二种是一格一格生成表格模版,但这样效率将会很底下。

首先,介绍第一种用模版的方式。这种方式是最简单,效率最高的方式。适用于导出的word表格有固定的格式。废话不多说,直接把代码贴出来。

   
   
[csharp] view plain copy print ?
  1.  1                string TemplatePath = @templatepath + "\\出国机票申请流程.doc";  //定义引用模版的路径                          
  2.  2                object missingValue = System.Reflection.Missing.Value;  //使用反射定义认参数   
  3.  3                dynamic wordApplication = AutomationFactory.CreateObject("Word.Application");  //创建WORD进程,必须在OOB模式下才能运行。   
  4.  4                dynamic document = wordApplication.Documents.Add(ref TemplatePath, ref missingValue,  
  5.  5                                   ref missingValue, ref missingValue);//添加一个WORD文档   
  6.  6                wordApplication.Visible = false//设置文档的可见性。   
  7.  7                dynamic table = document.Tables(1); // 定义模版中的表格,1表示是模版中的第一个表格   
  8.  8                table.Cell(1, 3).Range.Text = Group.ApprovalNumber + "," + Group.Name + "共" + Group.VisitNumber + "人";  //定义table 单元格的文本   
  9.  9                string SavePath = @SavetemplatePath + "\\出国机票申请流程" + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmss"); //定义保存的路径   
  10. 10                wordApplication.ActiveDocument.SaveAs(ref SavePath,  
  11. 11                            ref missingValue,  
  12. 12                            ref missingValue,  
  13. 13                            ref missingValue,  
  14. 14                            ref missingValue, ref missingValue); //保存word文档   
  15. 15   
  16. 16                document.close(); // 关闭文档   
  17. 17                wordApplication.Quit(ref missingValue, ref missingValue); //退出word进程。  
                  到此一个word文档就导出成功了。 

 还有第二种方式,一格一格生成表格的格式,这是最麻烦,最费时的一种方式,我也是研究了好久才做出来的。话不多说,直接上代码

                                   
   
   
[csharp] view plain copy print ?
  1. object missingValue = System.Reflection.Missing.Value;  
  2.                                    dynamic wordApplication = AutomationFactory.CreateObject("Word.Application");  
  3.                                    wordApplication.Visible = false;  
  4.                                    dynamic doc = wordApplication.Documents.Add();  
  5.                                    wordApplication.ActiveDocument.pagesetup.topmargin = 13.3; //设置word文档的上边距   
  6.                                    wordApplication.ActiveDocument.pagesetup.bottommargin = 53.3;//设置word文档的下边距   
  7.                                    wordApplication.ActiveDocument.pagesetup.leftmargin = 56.7;//设置word文档的左边距   
  8.                                    wordApplication.ActiveDocument.pagesetup.rightmargin = 56.7;//设置word文档的右边距   
  9.                                    dynamic rng = wordApplication.Range;  
  10.                                    int start = doc.Characters.Count - 1; //定义文本的坐标   
  11.                                    int end = doc.Characters.Count - 1;  
  12.                                    rng = doc.content;  
  13.                                    rng = doc.Range(ref start, ref end);  
  14.                                    rng.Text = "文档标题" + "\r\n";  
  15.                                    rng.font.size = 22;  
  16.                                    rng.font.name = "黑体"//设置字体   
  17.                                    rng.ParagraphFormat.Alignment = 1; //设置水平居中   
  18.                                    dynamic rng1 = wordApplication.Range;  
  19.                                    int start1 = doc.Characters.Count - 1;  
  20.                                    int end1 = doc.Characters.Count - 1;  
  21.                                    rng1 = doc.content;  
  22.                                    rng1 = doc.Range(ref start1, ref end1);  
  23.                                    rng1.Text = "自办单位名称:" + VistMeberInPermit.IUnion + "" + "团组号:" +  "";  
  24.                                    rng1.font.size = 12;  
  25.                                    rng1.font.name = "楷体_GB2312";  
  26.                                    dynamic table;  
  27.                                    int Tstart = doc.Characters.Count - 1;  
  28.                                    int Tend = doc.Characters.Count - 1;  
  29.                                    Object tableLocation = doc.Range(ref Tstart, ref Tend);  
  30.                                    table = doc.Tables.Add(tableLocation, 5, 8, ref missingValue); //在指定位置插入表格   
  31.                                    table.Borders.OutsideLinestyle = 1; //显示表格的边框线   
  32.                                    table.Borders.InsideLinestyle = 1;  
  33.   
  34.                                    table.cell(1, 1).Range.Text = "姓";  
  35.                                    table.cell(1, 1).Range.font.size = 14;  
  36.                                    table.cell(1, 1).Range.font.name = "楷体_GB2312";  
  37.                                    table.cell(1, 1).width = 45.1f; //设置单元格的宽度   
  38.                                    table.cell(1, 1).Range.ParagraphFormat.Alignment = 1; //设置单元格垂直的居中方式   
  39.   
  40.                                    table.cell(1, 2).width = 58.8f;  
  41.                                    table.cell(1, 2).Range.Text = VistMeberInPermit.AFName;  
  42.                                    table.cell(1, 2).Range.font.size = 14;  
  43.                                    table.cell(1, 2).Range.font.name = "楷体_GB2312";  
  44.                                    table.cell(1, 2).Range.ParagraphFormat.Alignment = 1;  
  45.   
  46.                                    table.cell(1, 3).Range.Text = "名";  
  47.                                    table.cell(1, 3).Range.font.size = 14;  
  48.                                    table.cell(1, 3).Range.font.name = "楷体_GB2312";  
  49.                                    table.cell(1, 3).width = 45.1f;  
  50.                                    table.cell(1, 3).Range.ParagraphFormat.Alignment = 1;  
  51.   
  52.                                    table.cell(1, 4).width = 90.3f;  
  53.                                    table.cell(1, 4).Range.Text = VistMeberInPermit.BLName;  
  54.                                    table.cell(1, 4).Range.font.size = 14;  
  55.                                    table.cell(1, 4).Range.font.name = "楷体_GB2312";  
  56.                                    table.cell(1, 4).Range.ParagraphFormat.Alignment = 1;      
  57.                                    string SavePath = @SavetemplatePath + "\\保存的名称" + "-" + System.DateTime.Now.ToString("yyyyMMdd HHmmss");    
  58.                                    wordApplication.ActiveDocument.SaveAs(ref SavePath,  
  59.                                            ref missingValue,  
  60.                                            ref missingValue, ref missingValue); //保存文档      
  61.                                    如果想要换页,则要用到@R_188_6404@,代码如下:  
  62.                                    dynamic para;  
  63.                                    para = doc.Content.Paragraphs.Add(ref missingValue);  
  64.                                    object pBreak = 0;  
  65.                                    para.Range.InsertBreak(ref pBreak);  
导出word表格的方法就写到这,导出excel的下次再分享

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐