wincc报表到excel

本人做了个wincc的报表,使用的copy的别人的脚本,把变量输出到txt文件还可以。现在想把变量输出到excel中,请各位看一下下面的这段脚本该如何修改?脚本中的一段如下(在定义的10点42分把两个变量的值写到名字是report的txt记事本中)
if(hour_int==10&&minute_int==42)
{
ml1=GetTagFloat(

问题补充:
本人做了个wincc的报表,使用的copy的别人的脚本,把变量输出到txt文件还可以。现在想把变量输出到excel中,请各位看一下下面的这段脚本该如何修改?脚本中的一段如下(在定义的10点42分把两个变量的值写到名字是report的txt记事本中)
if(hour_int==10&&minute_int==42)
{
ml1=GetTagFloat("1PDC_ML_ACC");
ml2=GetTagFloat("2PDC_ML_ACC");

pFile=fopen("e:\\report.txt","a+");
fprintf(pFile,"\n%04dÄê%02dÔÂ%02dÈÕ%02d:%02d:%02d\t",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
fprintf(pFile,"%f\t     ",ml1);
fprintf(pFile,"%f\t",ml2);

fclose(pFile); 
SetTagFloat("1PDC_ML_ACC",0);
SetTagFloat("2PDC_ML_ACC",0);
现在我想把变量输出到excel中,请问该如何修改这段脚本?如果能输出到excel的话,如何定义两个变量写到excel中的哪行哪列呢?非常感谢!!

最佳答案

这是我做的vb读写excel的脚本做在一个按钮里面的,你可以修改一下就用了:
Sub OnClick(ByVal Item)                                     
Dim fso,myfile
Set fso = CreateObject("scripting.FileSystemObject")
Set MyFile = fso.GetFile("d:\\data.xlsx")
Dim ObjExcelApp
Set objExcelApp = CreateObject("Excel.Application")
‘objExcelApp.Visible = True
objExcelApp.Workbooks.Open MyFile
‘上面的程序段是为了打开d盘excel文件
Dim aa_data
objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue = HMIRuntime.Tags("aa").read
Dim i,j
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>1.0 Then
i=2
j=6
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.8 And  objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<1.0 Then
i=7
j=11
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.6 And  objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.8 Then
i=12
j=16
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.6Then
i=17
j=21
End If
‘判断数据保存位置
Do While i<j
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =objExcelApp.worksheets ("sheet1").Cells(i+1, 1).VAlue
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue = objExcelApp.worksheets ("sheet1").Cells(i+1, 2).VAlue
i=i+1 
Loop 
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =HMIRuntime.Tags("yy").read
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue =HMIRuntime.Tags("xx").read

‘写数据到excel表格
objExcelApp.ActiveWorkbook.Save
‘保存表格
Dim cons_data,ax1_data,ax2_data,ax3_data,ax4_data,ax5_data,ax6_data
Set cons_data=HMIRuntime.Tags("cons")
Set ax1_data=HMIRuntime.Tags("ax1")
Set ax2_data=HMIRuntime.Tags("ax2")
Set ax3_data=HMIRuntime.Tags("ax3")
Set ax4_data=HMIRuntime.Tags("ax4")
Set ax5_data=HMIRuntime.Tags("ax5")
Set ax6_data=HMIRuntime.Tags("ax6")


cons_data.Value = objExcelApp.worksheets ("sheet1").Cells(23,7).value
ax1_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 6).value
ax2_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 5).value
ax3_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 4).value
ax4_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 3).value
ax5_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 2).value
ax6_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 1).value
‘上面的作用是将Excel表格中的数据读到wincc,存到临时变量里面
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set ObjEXceLapp = Nothing
‘到这里为止,关闭刚才打开的excel程序了
cons_data.Write 
ax1_data.Write 
ax2_data.Write 
ax3_data.Write 
ax4_data.Write 
ax5_data.Write 
ax6_data.Write 
‘最后是把读到的excel值从临时变量中写入它对应的wincc变量中


End Sub

提问者对于答案的评价:
谢谢

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc265647.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2019年6月11日
下一篇 2019年6月11日

相关推荐