也能动态生成文档并填充数据物业
要使用C#操作word,首先要添加引用: 1、添加引用-COM-Microsoft Word 11.0 Object Library
2、在.cs文件中添加
usingWord;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:
(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)
publicstringCreateWordFile(stringCheckedInfo)
...{
stringmessage=\"\";
try
...{
ObjectNothing=lue;
eateDirectory(\"C:/CNSI\");//创建文件所在目录
stringname=\"CNSI_\"+ShortString()+\".doc\";
objectfilename=\"
//创建Word文档
plicationWordApp=plicationClass();
cumentWordDoc=d(refNothing,refNothing,refNothing,refNothing);
//添加页眉
pe=OutlineView;
ekView=SeekPrimaryHeader;
sertAfter(\"[页眉内容]\");
ignment=AlignParagraphRight;//设置右对齐
ekView=SeekMainDocument;//跳出页眉设置
neSpacing=15f;//设置文档的行间距
//移动焦点并换行
objectcount=14;
objectWdLine=Line;//换一行;
veDown(refWdLine,refcount,refNothing);//移动焦点
peParagraph();//插入段落
//文档中创建表格
blenewTable=d(nge,12,3,refNothing,refNothing);
//设置表格样式
tsideLineStyle=LineStyleThickThinLargeGap;
sideLineStyle=LineStyleSingle;
lumns[1].Width=100f;
lumns[2].Width=220f;
lumns[3].Width=105f;
//填充表格内容
ll(1,1).xt=\"产品详细信息表\";
ll(1,1).ld=2;//设置单元格中字体为粗体
//合并单元格
ll(1,1).Merge(ll(1,3));
rticalAlignment=CellAlignVerticalCenter;//垂直居中
ignment=AlignParagraphCenter;//水平居中
//填充表格内容
ll(2,1).xt=\"产品基本信息\";
ll(2,1).lor=ColorDarkBlue;//设置单元格内字体颜色
//合并单元格
ll(2,1).Merge(ll(2,3));
rticalAlignment=CellAlignVerticalCenter;
//填充表格内容
ll(3,1).xt=\"品牌名称:\";
ll(3,2).xt=BrandName;
//纵向合并单元格
ll(3,3).Select();//选中一行
objectmoveUnit=Line;
objectmoveCount=5;
objectmoveExtend=Extend;
veDown(refmoveUnit,refmoveCount,refmoveExtend);
一站式购物趋势难挡 rge();
//插入图片
stringFileName=Picture;//图片所在路径
objectLinkToFile=false;
objectSaveWithDocument=true;
objectAnchor=nge;
dPicture(FileName,refLinkToFile,refSaveWithDocument,refAnchor);
lineShapes[1].Width=100f;//图片宽度
lineShapes[1].Height=100f;//图片高度
//将图片设置为四周环绕型
apes=lineShapes[1].ConvertToShape();
pe=WrapSquare;
ll(12,1).xt=\"产品特殊属性\";
ll(12,1).Merge(ll(12,3));
//在表格中增加行
bles[1].d(refNothing);
xt=\"文档创建时间:\"+String();//“落款”
ignment=AlignParagraphRight;
//文件保存
veAs(reffilename,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
ose(refNothing,refNothing,refNothing);
it(refNothing,refNothing,refNothing);
message=name+\"文档生成成功,以保存到C:CNSI下\";
}
catch
...{
message=\"文件导出异常!\";
}
returnmessage;
}
查看本文来源
天津白癜风医院哪家较好昆明白癜风医院
丹媚左炔诺孕酮肠溶片一盒多少片
- 上一页:之静态联编和动态联编
- 下一页:事件机制归纳上计划
-
九分裤+乐福鞋,夏日这样穿,时髦又显高,很适合小个子
夏日傍晚,前往篇名,乐声徐徐下劝教蛙声一片。时节,冬天更为替,当夏日...
2024-12-24
-
iPhone上的这个间谍触控板可以使打字更容易
iPhone和iPad都装载了很酷的隐秘机能,除此以外来自除此以外 iOS 15.5 越来越新...
2024-11-12
-
本月有趣 +1!中底「带闪电」的三叶草三方联名曝光!
日本国知名街头服装品牌 NEIGHBORHOOD 一直以来都在日潮迷不禁占据极佳的位置...
2024-11-03
-
心机流浪猫刮坏保时捷碰瓷姿总,以为死定了,没想到...太甜了吧!
“我当初以为我都会被固执董事拳背脊强暴,没想到他莫名其妙把我宠上了天...
2024-10-21
-
契约精神不在!西蒙斯的幺蛾子,或许才在此之后
众所周知,本赛季联盟最大的两个拉,一个是米切尔的疫苗多事件,一个是安...
2024-09-15
-
蜡烛的光源和设计哪个更重要?南卡台蜡烛L1体验:二者可兼得
不管是像我这种打铁路工人,还是教职员党在,平时夜里难免要加个班或者写...
2024-09-05