Labwindows_cvi creates Excel files based on C language

Table of contents

1. Preparatory work

(1) Find the path where the Excel library is located

(2) Build a simple interface

(3) Add external files (import files in the Excel library)

2. Start programming

3. Realize the effect


Recently, there is a project that needs to use Labwindows_cvi software as an instrument control program. When I use it, I know that the development language is C language. It took more than half a month to get familiar with Labwindows_cvi software and C language syntax, which barely meets the needs of the work. The project needs to create an Excel file. I am not very proficient in C language. I thought that I can create and use it with the fopen function. The file extension matches the format of the file". After reading the information, I know that the file formats that the fopen function can create include .txt, .dat, .csv, etc. Later, I asked a colleague who has used Labwindows. He said that he needed to call the Excel library, but he had never used it. Let me try it, find the Excel library under the installation path of the labwindows software, open it and see that there are various operation functions in excel, and it can be done directly. .

fopen(“D:\\S_试验结果\\test.xlsx”,w+)   //这样是不行的 

There is also a master who told me that there is no need to create it, just copy it directly, first create a new Excel table locally, and then use C to call the DOS copy command to copy it. I have not tried this, but it should be possible.

1. Preparatory work

(1) Find the path where the Excel library is located

Open the Labwindows_cvi software installation path, I installed it on the D drive, the path is "D:\Program Files (x86)\National Instruments\CVI2013\toolslib\activex\excel". As shown in Figure 1.

                                                                                              Figure 1 Excel library file path

(2) Build a simple interface

Create a text control and a button control. as shown in picture 2.

                                                                                         Figure 2 The built interface

(3) Add external files (import files in the Excel library)

Select the project "CreatExcelFile", click the right mouse button, and click "Add Existing file", as shown in Figure 3; find the path of the excel library file just now, select all the files, and click ok, as shown in Figure 4; the final imported file is shown in Figure 4 5.

                                                                               Figure 3 Adding external files to the project

                                                                             Figure 4 Add excel library file

                                                                Figure 5 The project list after adding the excel library file

2. Start programming

(1) Import the header files "excelreport.h" and "excel2000.h"; as shown in Figure 6.

                                                                            Figure 6 import header file

(2) Write the implementation code.

#include <cvirte.h>		
#include <userint.h>
#include "CreatExcelFile.h"
#include "excelreport.h"   
#include "excel2000.h"

static int excelpanel;

int main (int argc, char *argv[])
{
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((excelpanel = LoadPanel (0, "CreatExcelFile.uir", ExcelPANEL)) < 0)
		return -1;
	DisplayPanel (excelpanel);
	RunUserInterface ();
	DiscardPanel (excelpanel);
	return 0;
}

int CVICALLBACK ExcelPANEL_callback (int panel, int event, void *callbackData,
									 int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_GOT_FOCUS:

			break;
		case EVENT_LOST_FOCUS:

			break;
		case EVENT_CLOSE:
			QuitUserInterface (0);
			break;
	}
	return 0;
}

int CVICALLBACK CreatExcelFile_callback (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	//保存路径 
	char savePath[MAX_PATHNAME_LEN]; 
	//文件保存路径
	char fileSavePath[MAX_PATHNAME_LEN]; 
	// excelApp句柄	   
	static CAObjHandle appHandle=0; 
	// excel工作区间句柄	
	static CAObjHandle bookHandle=0; 
	// excel工作区间表格1句柄   
	static CAObjHandle sheetHandle=0;
	// excel工作区间表格1句柄
	static CAObjHandle sheetHandle1=0; 
 
	switch (event)
	{
		case EVENT_COMMIT:	  
			//弹出文件夹选择框
			if(DirSelectPopupEx ("", "请选择存储文件夹", savePath)<0)    
			{
				return 0;  	   	   
			}	  
			// 将路径显示在界面文本控件  
			SetCtrlVal (excelpanel, ExcelPANEL_excelFile_path, savePath);    
		    // 获取文本控件路径
			GetCtrlVal (excelpanel, ExcelPANEL_excelFile_path, fileSavePath);
		    // 生成文件保存路径
			strcat(fileSavePath, "\\Data.xlsx" ); 
		 
			 
			//如果没有创建应用程序,则创建excel  
			if(appHandle==0)	   
			{	  
				ExcelRpt_ApplicationNew(0,&appHandle); 
			}
			else	 //如果已经创建,则首先关闭前一次的book和sheet   
			{	   
				ExcelRpt_ApplicationQuit(appHandle);  
				CA_DiscardObjHandle(appHandle);
				ExcelRpt_ApplicationNew(0,&appHandle); 
			}	
			//新建一个excel文件
			ExcelRpt_WorkbookNew(appHandle,&bookHandle);
			//获取sheet1句柄
			ExcelRpt_GetWorksheetFromName( bookHandle,"Sheet1",&sheetHandle);  
			//将Sheet1的名称改为amp
			ExcelRpt_SetWorksheetAttribute (sheetHandle, ER_WS_ATTR_NAME, "amp"); 
			//在已有的sheet表后面增加一个sheet表格
			ExcelRpt_WorksheetNew(bookHandle , -1,  &sheetHandle1);	 
			//将新增加的sheet表格名称改为pha
			ExcelRpt_SetWorksheetAttribute (sheetHandle1, ER_WS_ATTR_NAME, "pha");
	   		//excel 文件保存													
			ExcelRpt_WorkbookSave(bookHandle,fileSavePath,ExRConst_DefaultFileFormat  );
	 		//将文件设为可见  
		    //ExcelRpt_SetApplicationAttribute (appHandle, ER_AP_ATTR_VISIBLE, 1);  
			//退出excel文件
			ExcelRpt_ApplicationQuit(appHandle); 
			//释放excel资源
			CA_DiscardObjHandle(appHandle);  
			appHandle=0; 
			//弹出提示框
			MessagePopup("提示", "Excle文件已经创建!");   
			break;
	}
	return 0;
}

3. Realize the effect

Run the program, click the button to create an excel file, the effect is shown in Figure 7 and Figure 8.

                                                                    Figure 7 Program interface effect

                                                                   Figure 8 Created Excel table

Guess you like

Origin blog.csdn.net/caimeihua5369/article/details/114681741