在工控测试领域,经常需要操作excel,用于保存测试的数据及测试结果。本章将介绍LabwindowsCVI对Excel操作的支持。LabwindowsCVI 对excel的支持是基于 windows的excel库,简单点说就是对windows的excel库进行了一层封装,这样我们就能更好的操作excel了。下面将介绍操作Excel的常用API:
- ExcelRpt_ApplicationNew
ExcelRpt_ApplicationNew用于启动excel程序,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_ApplicationNew (int makeVisible, CAObjHandle *applicationHandle);
其中makeVisible为加载的excel程序是否显示的标志,当其值为1时,显示excel程序界面,如下图所示,applicationHandle为获取的excel程序句柄。
- ExcelRpt_WorkbookOpen
ExcelRpt_WorkbookOpen用于打开excel的文件,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_WorkbookOpen (CAObjHandle applicationHandle, const char *fileName,CAObjHandle *workbookHandle);
其中applicationHandle为excel程序的句柄,fileName为要打开的excel文件路径,workbookHandle为获取的excel文件句柄。当程序执行成功后,excel程序将显示excel文件内容,如下所示:
3、ExcelRpt_GetWorksheetFromIndex
ExcelRpt_GetWorksheetFromIndex为获取excel文件中某个sheet句柄,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_GetWorksheetFromIndex (CAObjHandle workbookHandle, int Index,CAObjHandle *worksheetHandle);
其中workbookHandle为excel文件的句柄,Index为excel文件中sheet的索引,其索引值从1开始,worksheetHandle为获得的sheet句柄。
4、ExcelRpt_GetCellValue
ExcelRpt_GetCellValue为获取sheet中某cell的内容,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_GetCellValue (CAObjHandle worksheetHandle, const char *cellRange,enum ExREnum_ExDataType dataType, void *dataValue);
其中worksheetHandle为sheet句柄,cellRange为cell的索引,dataType为数据类型,通常情况下,该值设置为CAVT_CSTRING,这样我们可以读取各种数据,然后进行转换,dataValue为读取的数据指针。
5、ExcelRpt_SetCellValue
ExcelRpt_SetCellValue用于设置sheet中某cell内容,其函数原型如下所示:
HRESULT ExcelRpt_SetCellValue (CAObjHandle worksheetHandle, char cellRange[], enum ExREnum_ExDataType dataType, ...);
其中worksheetHandle为sheet句柄,cellRange为cell的索引,dataType为数据类型。
6、ExcelRpt_SetCellRangeAttribute
ExcelRpt_SetCellRangeAttribute为设置cell的属性值,其函数原型如下所示:
HRESULT CVIFUNC_C ExcelRpt_SetCellRangeAttribute (CAObjHandle worksheetHandle, const char* cellRange,int attribute, ...);
其中worksheetHandle为sheet句柄,cellRange为cell的索引,attribute为属性的名称,…为属性的值。在测试中,我们经常使用该函数用于设置cell的背景颜色,用于直观感受是否测试通过。
7、ExcelRpt_WorkbookClose
ExcelRpt_WorkbookClose用于关闭打开的excel文件,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_WorkbookClose (CAObjHandle workbookHandle, int saveChanges);
其中workbookHandle为excel打开文件的句柄,saveChanges为关闭时,是否保存excel文件的标志,通常我们设置该值为1,否则测试的数据将保存不了。
8、ExcelRpt_ApplicationQuit
ExcelRpt_ApplicationQuit用于关闭打开的excel程序,其函数原型如下所示:
HRESULT CVIFUNC ExcelRpt_ApplicationQuit (CAObjHandle applicationHandle);
其中applicationHandle为打开的excel程序实例句柄。
9、ExcelRpt_GetErrorInfo
ExcelRpt_GetErrorInfo用于得到操作excel异常原因,其函数原型如下所示:
ERRORINFO * CVIFUNC ExcelRpt_GetErrorInfo(void);
该函数返回ERRORINFO的指针,我们可以观察ERRORINFO-> description的异常描述。
10、CA_DiscardObjHandle
CA_DiscardObjHandle用于释放objHandle对象,其函数原型如下所示:
HRESULT CVIFUNC CA_DiscardObjHandle(CAObjHandle objHandle);
项目实践:
新建一个LabwindowsCVI工程,然后往工程添加Excel库文件,如下图所示:
这些库文件可以在LabwindowsCVI安装路径下找到,其路径为:D:\Program Files (x86)\National Instruments\CVI2013\toolslib\activex\excel。
本工程excel测试代码如下所示:
#include <cvirte.h>
#include <userint.h>
#include "ExcelTest.h"
#include "excel2000.h"
#include "excelreport.h"
#include <ansi_c.h>
static CAObjHandle applicationHandle = 0;
static CAObjHandle workbookHandle = 0;
static CAObjHandle worksheetHandle = 0;
ERRORINFO *pExcelErrorInfo;
char excelStringValue[256];
void ExcelTest(void)
{
HRESULT error;
if(applicationHandle == 0)
{
error = ExcelRpt_ApplicationNew(1, &applicationHandle);
if (error<0)
{
pExcelErrorInfo = ExcelRpt_GetErrorInfo();
printf("ExcelRpt_ApplicationNew error: %s\r\n",pExcelErrorInfo->description);
}
}
if(workbookHandle == 0)
{
error = ExcelRpt_WorkbookOpen(applicationHandle,"D:\\lujinming\\个人资料\\Labwindows编程\\LabwindowsCVI_Excel\\ExcelTest.xlsx",&workbookHandle);
if(error < 0)
{
pExcelErrorInfo = ExcelRpt_GetErrorInfo();
printf("ExcelRpt_WorkbookOpen error: %s\r\n",pExcelErrorInfo->description);
}
}
if(worksheetHandle == 0)
{
error = ExcelRpt_GetWorksheetFromIndex(workbookHandle,1,&worksheetHandle);
if(error < 0)
{
pExcelErrorInfo = ExcelRpt_GetErrorInfo();
printf("ExcelRpt_GetWorksheetFromIndex error: %s\r\n",pExcelErrorInfo->description);
}
}
ExcelRpt_SetCellValue(worksheetHandle,"A1",CAVT_CSTRING,"Pass");
ExcelRpt_GetCellValue(worksheetHandle,"A1",CAVT_CSTRING,excelStringValue);
printf("A1=%s\r\n",excelStringValue);
ExcelRpt_SetCellValue(worksheetHandle,"A2",CAVT_CSTRING,"Fail");
ExcelRpt_SetCellRangeAttribute(worksheetHandle,"A2",ER_CR_ATTR_BGCOLOR,0xFF0000);
if (worksheetHandle)
CA_DiscardObjHandle(worksheetHandle);
if (workbookHandle)
{
ExcelRpt_WorkbookClose(workbookHandle, 1);
CA_DiscardObjHandle(workbookHandle);
}
if (applicationHandle)
{
ExcelRpt_ApplicationQuit(applicationHandle);
CA_DiscardObjHandle(applicationHandle);
}
}
static int panelHandle;
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "ExcelTest.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
ExcelTest();
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
int CVICALLBACK Quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
执行效果如下所示: