wincc和excel之间的数据读写Wincc的脚本学习2009-08-2410:11:08阅读643评论0字号:大中小订阅SubX6309X94AE9X0000X6539_OnClick(ByvalItem)Dimfso,myfileSetfso=CreateObject(scripting.FileSystemObject)SetMyFile=fso.GetFile(d:\Excelcode.xls)DimObjExcelAppSetobjExcelApp=CreateObject(Excel.Application)'objExcelApp.Visible=TrueobjExcelApp.Workbooks.OpenMyFile'上面的程序段是为了打开d盘下叫Excelcode.xls名字的excel文件objExcelApp.worksheets(sheet1).Cells(1,1).VAlue=HMIRuntime.Tags(usercodenew1).readobjExcelApp.ActiveWorkbook.Save'上面两行的作用是将wincc变量usercodenew1的值写到Excel表中的A1格子里面,并且保持一下DimcoldoldSetcoldold=HMIRuntime.Tags(usercodeold1)coldold.Value=objExcelApp.worksheets(sheet1).Cells(1,1).value'上面这两句的作用是将刚才保持到Excel表格中的数据重新读回wincc,存到usercodeold1变量中,当然现在其实并没有存入usercodeold1变量,只是存在了叫coldold的临时变量里面objExcelApp.Workbooks.CloseobjExcelApp.QuitSetObjEXceLapp=Nothing'到这里为止,我们可以通过上面的语句关闭刚才打开的excel程序了coldold.Write'最后是把读到的excel值从临时变量coldold中写入它对应的usercodeold1变量中EndSub楼主想要的恐怕不是你的这个吧!???把wincc的变量值写到excel中去,用C脚本或VBS脚本吧!#pragmacode(kernel32.dll)VOIDGetLocalTime(LPSYSTEMTIMElpSystemTime);#pragmacode()charFileName[20]=;SYSTEMTIMEsysTime;__object*pExcel=NULL;GetLocalTime(&sysTime);sprintf(FileName,d:\\%d-%d-%d.xls,sysTime.wYear,sysTime.wMonth,sysTime.wDay);pExcel=__object_create(Excel.Application.9);//9表示用的是office2000pExcel-Visible=1;pExcel-Workbooks-Open(d:\\book1.xls);pExcel-Worksheets(sheet1)-Range(A1)-Value=GetTagFloat(wincctag1);//写wincc的变量实时值到格A1中pExcel-Worksheets(sheet1)-Range(A2)-Value=GetTagFloat(wincctag2);//写wincc的变量实时值到格A2中pExcel-ActiveWorkbook-SaveAs(FileName);pExcel-Workbooks-Close();pExcel-Quit();__object_delete(pExcel);本人做的源代码:SubOnClick(ByValItem)Dimfso,myfileSetfso=CreateObject(scripting.FileSystemObject)SetMyFile=fso.GetFile(d:\\data.xlsx)DimObjExcelAppSetobjExcelApp=CreateObject(Excel.Application)'objExcelApp.Visible=TrueobjExcelApp.Workbooks.OpenMyFile'上面的程序段是为了打开d盘excel文件Dimaa_dataobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue=HMIRuntime.Tags(aa).readDimi,jIfobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue1.0Theni=2j=6EndIfIfobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue0.8AndobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue1.0Theni=7j=11EndIfIfobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue0.6AndobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue0.8Theni=12j=16EndIfIfobjExcelApp.worksheets(sheet1).Cells(2,9).VAlue0.6Theni=17j=21EndIf'判断数据保存位置DoWhileijobjExcelApp.worksheets(sheet1).Cells(i,1).VAlue=objExcelApp.worksheets(sheet1).Cells(i+1,1).VAlueobjExcelApp.worksheets(sheet1).Cells(i,2).VAlue=objExcelApp.worksheets(sheet1).Cells(i+1,2).VAluei=i+1LoopobjExcelApp.worksheets(sheet1).Cells(i,1).VAlue=HMIRuntime.Tags(yy).readobjExcelApp.worksheets(sheet1).Cells(i,2).VAlue=HMIRuntime.Tags(xx).read'写数据到excel表格objExcelApp.ActiveWorkbook.Save'保存表格Dimcons_data,ax1_data,ax2_data,ax3_data,ax4_data,ax5_data,ax6_dataSetcons_data=HMIRuntime.Tags(cons)Setax1_data=HMIRuntime.Tags(ax1)Setax2_data=HMIRuntime.Tags(ax2)Setax3_data=HMIRuntime.Tags(ax3)Setax4_data=HMIRuntime.Tags(ax4)Setax5_data=HMIRuntime.Tags(ax5)Setax6_data=HMIRuntime.Tags(ax6)cons_data.Value=objExcelApp.worksheets(sheet1).Cells(23,7).valueax1_data.Value=objExcelApp.worksheets(sheet1).Cells(23,6).valueax2_data.Value=objExcelApp.worksheets(sheet1).Cells(23,5).valueax3_data.Value=objExcelApp.worksheets(sheet1).Cells(23,4).valueax4_data.Value=objExcelApp.worksheets(sheet1).Cells(23,3).valueax5_data.Value=objExcelApp.worksheets(sheet1).Cells(23,2).valueax6_data.Value=objExcelApp.worksheets(sheet1).Cells(23,1).value'上面的作用是将Excel表格中的数据读到wincc,存到临时变量里面objExcelApp.Workbooks.CloseobjExcelApp.QuitSetObjEXceLapp=Nothing'到这里为止,关闭刚才打开的excel程序了cons_data.Writeax1_data.Writeax2_data.Writeax3_data.Writeax4_data.Writeax5_data.Writeax6_data.Write'最后是把读到的excel值从临时变量中写入它对应的wincc变量中EndSub