新手刚学wincc,现在遇到一个问题,向各位请教.
怎样在wincc中用C脚本判断excel报表是否存在,vbs只懂皮毛,最好不要说用vbs做.
#pragma code ("kernel32.dll")
VOID GetLocalTime(LPSYSTEMTIME lpSystemTime);
#pragma code ()
char FileName[20] = "",Date[2] = "";
static int i,j;
SYSTEMTIME sysTime;
__object* pExcel = NULL;
GetLocalTime(&sysTime);
sprintf(FileName,"e:\\%d年%d月.xls",sysTime.wYear,sysTime.wMonth);
sprintf(Date,"%d",sysTime.wDay);
i=atoi(Date)+2;
j++;
if(j==3) j=0;
pExcel = __object_create("Excel.Application");
pExcel->Visible = 0;
if(在这里判断)
{
pExcel->Workbooks ->Open ("e:\\book1.xls");
pExcel->ActiveWorkbook->SaveAs(FileName);
}
pExcel->Workbooks ->Open (FileName);
pExcel->WorkSheets("sheet1")->Cells(i,j+2)->Value=GetTagFloat("供水");
pExcel->WorkSheets("sheet1")->Cells(i,j+6)->Value=GetTagFloat("补水");
pExcel->ActiveWorkbook->Save();
pExcel->Workbooks->Close();
pExcel->Quit();
__object_delete(pExcel);
最佳答案
呵呵,这个问题居然在亿万上也看到了。
fso有FileExists方法来判断文件是否存在
__object* fso=NULL;
fso=__object_create("Scripting.FileSystemObject");
if (fso->FileExists("E:\\myfile.xls"))
{
printf("OK");
}
__object_delete(fso);
提问者对于答案的评价:
亿万上也是我问的,现已解决,不过还是谢谢啊.
#pragma code ("kernel32.dll")
VOID GetLocalTime(LPSYSTEMTIME lpSystemTime);
#pragma code ()
char FileName[20] = "",Date[2] = "";
static int i,j;
SYSTEMTIME sysTime;
__object* pExcel = NULL;
__object* fso=NULL;
GetLocalTime(&sysTime);
sprintf(FileName,"e:\\%d年%d月.xls",sysTime.wYear,sysTime.wMonth);//以当前年月作为文件名
sprintf(Date,"%d",sysTime.wDay);//日期赋值给字符串Date
i=atoi(Date)+2;//日期转化为整形,加2的原因是表格上面要留2行写其他东西
j++;
if(j==3) j=0;//换列用,每次前进一列,输入三次后返回
pExcel = __object_create("Excel.Application");
fso=__object_create("Scripting.FileSystemObject");
pExcel->Visible = 0;
if(!(fso->FileExists(FileName)))//判断文件夹内是否存在当前年月为名称的excle文件
{
pExcel->Workbooks ->Open ("e:\\book1.xls");
pExcel->ActiveWorkbook->SaveAs(FileName);//不存在则复制,做好的模板book1.xls,另存名称为当前年月
}
__object_delete(fso);
pExcel->Workbooks ->Open (FileName);
pExcel->WorkSheets("sheet1")->Cells(i,j+2)->Value=GetTagFloat("供水");//把变量写入第i行,j+2列
pExcel->WorkSheets("sheet1")->Cells(i,j+6)->Value=GetTagFloat("补水");//把变量写入第i行,j+6列
pExcel->ActiveWorkbook->Save();//保存
pExcel->Workbooks->Close();
pExcel->Quit();
__object_delete(pExcel);
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc275312.html