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

相比较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, ref missingValue, ref missingValue, ref missingValue,
12 ref missingValue, ref missingValue, ref missingValue, ref missingValue,
13 ref missingValue, ref missingValue, ref missingValue, ref missingValue,
14 ref missingValue, ref missingValue, ref missingValue); //保存word文档
15
16 document.close(); // 关闭文档
17 wordApplication.Quit(ref missingValue, 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, 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, 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);

导出word表格的方法就写到这,导出excel的下次再分享。

转载于:https://www.cnblogs.com/Champion-jie/archive/2012/01/10/Champion_Jie.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值