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

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

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

  
  
[csharp] view plain copy
  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, ref missingValue, ref missingValue, ref missingValue,  
  12. 12                            ref missingValue, ref missingValue, ref missingValue, ref missingValue,  
  13. 13                            ref missingValue, ref missingValue, ref missingValue, ref missingValue,  
  14. 14                            ref missingValue, ref missingValue, ref missingValue); //保存word文档  
  15. 15   
  16. 16                document.close(); // 关闭文档  
  17. 17                wordApplication.Quit(ref missingValue, ref missingValue, ref missingValue); //退出word进程。  
                  到此一个word文档就导出成功了。 

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

                                   
  
  
[csharp] view plain copy
  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, 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, ref missingValue, ref missingValue, ref missingValue,  
  60.                                            ref missingValue, ref missingValue, ref missingValue, ref missingValue,  
  61.                                            ref missingValue, ref missingValue, ref missingValue, ref missingValue,  
  62.                                            ref missingValue, ref missingValue, ref missingValue); //保存文档     
  63.                                    如果想要换页,则要用到分页符,代码如下:  
  64.                                    dynamic para;  
  65.                                    para = doc.Content.Paragraphs.Add(ref missingValue);  
  66.                                    object pBreak = 0;  
  67.                                    para.Range.InsertBreak(ref pBreak);  
导出word表格的方法就写到这,导出excel的下次再分享。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来示数据库,使用类的实例中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 达式语言: SQLAlchemy 提供了一个丰富的 SQL 达式语言,允许开发者以 Python 达式的方式编写复杂的 SQL 查询。 达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值