Vlisp向word里面的表格写入数据
1 . 获取word进程对象(setq Rtn (vlax-get-or-create-object "Word.Application"))
2 . 获取Documents对象
(setq doc (vlax-invoke-method
(vlax-get-property Rtn 'Documents)
'Open
wjm
))
3 . 获取excel中所有的表格对象
(vlax-get-property doc 'Tables)
4 . 取第一个表格对象
(setq Table (vlax-invoke-method
(vlax-get-property doc 'Tables)
'item
1
))
5 . 获取指定行和列的cell对象
(setq cell (vlax-invoke-method
Table
'cell
(car row)
(cadr row)
)
)
6 . 获取cell的range对象
(setq Rge (vlax-get-property cell 'Range))
7 . 给range赋值
(vlax-put-property
Rge
'Text
value
)
示例:
(defun $xiu-gai-bao-cun$
(wjm data / doc rtn table)
(setq Rtn (vlax-get-or-create-object "Word.Application"))
(setq doc (vlax-invoke-method
(vlax-get-property Rtn 'Documents)
'Open
wjm
)
)
(setq Table (vlax-invoke-method
(vlax-get-property doc 'Tables)
'item
1
)
)
(mapcar (function (lambda (a / cell rge row value)
(and
(setq row (car a))
(setq value (cdr a))
(car row)
(cadr row)
(progn
(setq cell (vlax-invoke-method
Table
'cell
(car row)
(cadr row)
)
)
(setq Rge (vlax-get-property cell 'Range))
(vlax-put-property
Rge
'Text
value
)
)
)
)
)
data
)
(vlax-invoke-method doc "SaveAs2")
(progn
(vlax-invoke-method
doc
'Close
)
(vl-catch-all-apply 'vlax-invoke-method (list Rtn 'Quit))
(gc)
)
)
(setq wjm "D:\\测试文件.doc");要写入的文件路径
(setq data (list (cons (list 3 1) "张三")
(cons (list 3 2) "李四")
(cons (list 5 2) "王五")
(cons (list 5 3) "赵六")
)
)
($xiu-gai-bao-cun$ wjm data)
有没有替换所有指定文字并保留原有格式的 tryhi 发表于 2024-7-9 11:19
有没有替换所有指定文字并保留原有格式的
Documents对象下有一个text是一个字符串 , 获取到之后替换完再复制回去应该是可以的
word表格里面的文字都是通过制表符(\t)实现换行和对其的
页:
[1]