常驻内存的EXCEL文件读写

已锁定

RENHQ

版主 西门子1847工业学习平台

  • 帖子

    12238
  • 精华

    59
  • 被关注

    1941

论坛等级:至圣

注册时间:2008-12-28

钻石 钻石 如何晋级?

常驻内存的EXCEL文件读写

5580

24

2016-12-07 23:10:47

star star star

1、序

开始是从论坛上看到有人说想要excel一直打开这,然后记录输入到excel里面,当时在想是不是全局脚本里面定义一个全局变量就可以,后来没事在整理自己电脑上的资料的时候,突然发现一遍以前收集的文章,能够实现这个功能。

效果图:



2、参考资料

1)主要是来源于转的帖子:WinCC VBS利用EXCEL调用Windows API函数

2)脚本DATASet的用法说明,在WINCC帮助中,关于DATASET对象是这么说的,我的英语半吊子都不到,就不翻译了。

Using the DataSet object, data may be exchanged across several actions. 

A DataSet object is global and defined by the Screen object. Any VBS action may access the data. 

The DataSet object at the Screen object must be addressed according to picture hierarchy and shall persist as long as the picture is displayed. The global object persists over the entire Runtime time period. 

Access uses the DataItem object.   

Note:

Objects of type Screen, Screens, ScreenItem, ScreenItems, Tag and TagSet cannot be included in the DataSet list. 

The DataSet object does not support any classes.  

Usage:

Using the "DataSet" list, you may:

Output or process (enumerate) all objects in the list.

Output the number of elements contained ("Count" property).

To process a specific object in the list ("Item" method).

Add an object to the list ("Add" method).

Remove a specific object from the list ("Remove" method).

Remove all objects from the list ("RemoveAll" method).

Access to list elements uses: 

HMIRuntime.DataSet("Itemname")

For a picture-specific list, access uses: 

HMIRuntime.Screens("Screenname").DataSet("Itemname") 

In a picture, you may access the DataSet object of the picture by using: 

DataSet("Itemname")

If upon access the stated name does not exist in the list, VT_Empty is returned and an Exception is triggered. 

Example:

The example shows how to add a value to the list, how to read it and remove it. It make sense to perform this in several different actions.

'VBS162

HMIRuntime.DataSet.Add "motor1", 23

HMIRuntime.Trace "motor1: " & HMIRuntime.DataSet("motor1").Value & vbNewLine

HMIRuntime.DataSet.Remove("motor1") 


就是利用DataSet对象可以传送对象、变量等供在别的地方使用

3、实现功能和脚本:

测试的时候做了3个按钮,分别是创建EXCEL对象、插入数据和关闭EXCEL对象

创建:

自动检查表格是否已经打开,如果打开了可以直接添加数据;检查表格是否存在,如果不存在,自动打开模版并保存到固定路径,并以“年-月-日 时.分”格式保存表格,由于文件名不能有分号,所以用点代替分号。



 


数据插入:

自动查找数据最后填充的位置,然后在空白处记录新数据,如果是从表格中读取的话,改成:HmiRuntime.tags(“变量”).write oSheet.cells(a,b).value 即可,具体是变量还是对象,把前面内容替换。



 

关闭:自动保存表格,并关闭退出excel进程



 

4、结束

经测试,也可以创建多个EXCEL表格,在dataset内添加不同的项目名称就行;如果把关闭按钮的oexcel.quit注释掉,可以使退出时只保存关闭工作簿,不退出excel进程,如果是打开多个工作簿的话,这一行代码必须去掉。

关于EXCEL对象的用法,其属性和方法较多,不知道从哪里能够收集到完整的,正在整理,待整理完再进行分享


常驻内存的EXCEL文件读写 已锁定
编辑推荐: 关闭

请填写推广理由:

本版热门话题

SIMATIC WinCC / Panel

共有31084条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

top
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。