DELPHI中,操作word表格时,我们经常需要进行1行中的2个单元格合并与1列中的2个单元格合并.如果第1次合并1行中的2个单元格,没有问题,但是,第2次合并1行中的另外2个单元格时,运行出错:"无法访问此集合中单独的列,因为表格中有混合的单元格宽度".请问怎么解决?
解决方案 »
- delphi 中如何调试filter
- 复兴科技高薪诚聘DELPHI开发工程师(深圳南山科技园)
- 如何在客户端判断 TcpClient.SendBuf 内容 全部到达 服务器
- Delphi编译提示[fatal error]file not found:'ExceptionLog.dcu'
- 如何将6个edit中的数据排序后,在他们的下面对应显示IMAGE格式的第一....第六的图图片???
- DBGird数据绑定的问题!
- 一个小问题(添加interface的method)?(请高手指点)
- splash+password+mainForm,它们几个的包含顺序处理?
- 初学者怎样才能学好DELPHI?介绍些好书吧!
- 请问单击托盘图标的消息是什么,如何截取之?
- 如何判断webbrowser对象是否为空
- ----散分--
start,e:Integer;
r:WordXP.Range;
begin
WordApplication1.Connect;
WordApplication1.Visible:=True;
WordApplication1.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam);
WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
//新建文档、插入表格、设置边框
WordDocument1.Tables.Add(WordDocument1.Range,10,20,EmptyParam,EmptyParam);
WordDocument1.Tables.Item(1).Borders.InsideLineStyle:=wdLineStyleSingle;
WordDocument1.Tables.Item(1).Borders.OutsideLineStyle:=wdLineStyleSingle;
r:=WordDocument1.Range; myRow:=WordDocument1.Tables.Item(1).Rows.Item(1);
//合并 10-13
start:= myRow.Cells.Item(10).Range.Start;
e:=myRow.Cells.Item(13).Range.end_;
r.Start:=start;
r.end_:=e;
r.Cells.merge;
//合并 6-8
start:= myRow.Cells.Item(6).Range.Start;
e:=myRow.Cells.Item(8).Range.end_;
r.Start:=start;
r.end_:=e;
r.Cells.merge;
//合并单元格 1-3
start:= myRow.Cells.Item(1).Range.Start;
e:=myRow.Cells.Item(3).Range.end_;
r.Start:=start;
r.end_:=e;
r.Cells.merge;
WordDocument1.Disconnect;
WordApplication1.Disconnect;
end;
经测试:1.合并后索引号确实变了,合并的单元格以新的一个格起算,起始值为1
2.2007和XP的调用没有任何区别,可直接使用
你能不能利用OLE在Delphi7中调用Word2003多次合并单元格的例子,再写一下代码好吗?我给你加分.
FDoc := FWord.Documents.Add;
...
(接下来,就请你帮忙了...)
begin
WordApp:=CreateOleObject('Word.application');
WordApp.Visible:=True;
WordApp.DOcuments.add(EmptyParam,EmptyParam,EmptyParam,EmptyParam);
WordDoc:=WordApp.ActiveDocument;
tb:=WordDoc.Tables.Add(WordDoc.Range,10,20,EmptyParam,EmptyParam );
tb.Borders.InsideLineStyle:=1;//wdLineStyleSingle;
tb.Borders.OutsideLineStyle:=1;//wdLineStyleSingle;
r:=WordDoc.Range;
myRow:=WordDoc.Tables.Item(1).Rows.Item(1);
//合并 10-13
start:= myRow.Cells.Item(10).Range.Start;
e:=myRow.Cells.Item(13).Range.end;
r.Start:=start;
r.end:=e;
r.Cells.merge;
//合并 6-8
start:= myRow.Cells.Item(6).Range.Start;
e:=myRow.Cells.Item(8).Range.end;
r.Start:=start;
r.end:=e;
r.Cells.merge;
//合并单元格 1-3
start:= myRow.Cells.Item(1).Range.Start;
e:=myRow.Cells.Item(3).Range.end;
r.Start:=start;
r.end:=e;
r.Cells.merge; WordDoc:=null;
WordApp:=null;
end;
这种东西比较讨厌的地方在于,根本没有办法在Code时就判定代码正误,只有在执行时摸石头过河:)这跟使用Server面板中的组件有天壤之别。我在写代码时也是一行一行试过来。如果您在公司的话,还是尽量建议您的领导少用CreateOleObject,开发效率巨低,到软件升级时会哭滴:),如果您是个人开发的话,直接用Server有什么不好的?我从97到2007全这么用过来,office97的代码照样兼容!
cell(1,3)与CELL(1,6)行合并、cell(1,7)与CELL(1,10)行合并;
var tb:wordXP.Table;
begin
wordApp1.Connect;
wordApp1.Visible:=True;
wordApp1.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam);
WordDoc1.ConnectTo(WordApp1.ActiveDocument);
//新建文档、插入表格、设置边框
tb:=WordDoc1.Tables.Add(WordDoc1.Range,10,20,EmptyParam,EmptyParam);
WordDoc1.Tables.Item(1).Borders.InsideLineStyle:=wdLineStyleSingle;
WordDoc1.Tables.Item(1).Borders.OutsideLineStyle:=wdLineStyleSingle;
tb.Cell(1,1).Merge(tb.Cell(2,1));
tb.Cell(1,6).Merge(tb.Cell(1,9)); WordDoc1.Disconnect;
WordApp1.Disconnect;
end;
现在还有一个问题是,怎么让数据在单元格内靠左、靠右、居中,请列出代码。
这个问题之后就散分了。
tb.Cell(1,1).Merge(tb.Cell(2,1));
tb.cell(1,1).Range.Text:='真逗';
tb.Cell(1,6).Merge(tb.Cell(1,9));
tb.Cell(1,6).Range.Text:='现在人都懒成这样了吗?';
干嘛还要移动光标呢?直接赋值就OK!