⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 report.c

📁 这是我编写的深圳地铁S335型司机控制器的检测程序
💻 C
📖 第 1 页 / 共 2 页
字号:
// #########################################################################
// *************************************************************************

#include <cvirte.h>     
#include <userint.h>
#include "DriverController.h"
#include <cviauto.h>
#include "excel.h"
#include "inifile.h"
#include "..\include\global.h"
#include "MyFile.h"

//------------------------------------------------------------------------------------------------
extern int              plMain;
extern int              plTable;
extern PT_SystemData    g_SystemData;
//------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------
// Excel Report Variables / Print & Save
//------------------------------------------------------------------------------------------------
static ExcelObj_App               ExcelAppHandle = 0;       
static ExcelObj_Workbooks         ExcelWorkbooksHandle = 0; 
static ExcelObj_Workbook          ExcelWorkbookHandle = 0;  
static ExcelObj_Sheets            ExcelSheetsHandle = 0;    
static ExcelObj_Worksheet         ExcelWorksheetHandle = 0; 
static ExcelObj_Range             ExcelRangeHandle = 0;
//------------------------------------------------------------------------------------------------
#define APP_AUTOMATION_ERR       "Error:  Microsoft Excel Automation"
#define EXCEL_ARRAY_OF_CELLS     "A1:I40"    
//------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------
// Excel Report Functions
//------------------------------------------------------------------------------------------------
long __stdcall TBExcelOpen( void );
long __stdcall TBExcelClose( void );
long __stdcall TBExcelShow( void );
long __stdcall TBExcelHide( void );
long __stdcall TBExcelCellSetValue( int nLine, int nColumn, char* pchValue );
long __stdcall TBExcelSave( void );
long __stdcall TBExcelCreate( char* pchTemp, char* pchName );
//------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------
// ClearObjHandle
//----------------------------------------------------------------------------
HRESULT ClearObjHandle(CAObjHandle *objHandle)
{
    HRESULT error = 0;
    if ((objHandle) && (*objHandle))
    {
        error = CA_DiscardObjHandle (*objHandle);
        *objHandle = 0;
    }
    return error;    
}    
//----------------------------------------------------------------------------
// ReportWordAutomationError
//----------------------------------------------------------------------------
static void ReportAppAutomationError( HRESULT hrStatus )
{
    char errorBuf[256];
    
    if (hrStatus < 0)
    {
        CA_GetAutomationErrorString (hrStatus, errorBuf, sizeof (errorBuf));
        MessagePopup (APP_AUTOMATION_ERR, errorBuf);
    }
    return;
}

//------------------------------------------------------------------------------------------------
long __stdcall ExcelOpen( void )
{
   HRESULT hrStatus = 0;
   // Launch App
   // Connect to existing application if available
   hrStatus = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
   if (hrStatus >= 0) 
      return 0;
   
   // Launch App
   hrStatus = Excel_NewApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
   if ( hrStatus < 0 ) 
      goto Error;
    
   return 0;
   
Error:
   ReportAppAutomationError (hrStatus);
	return hrStatus;
}
//------------------------------------------------------------------------------------------------
long __stdcall ExcelClose( void )
{
    HRESULT hrStatus = 0;
    
    ERRORINFO ErrorInfor;

    ClearObjHandle (&ExcelRangeHandle);
    ClearObjHandle (&ExcelWorksheetHandle);
    ClearObjHandle (&ExcelSheetsHandle);
    
    if (ExcelWorkbookHandle) 
    {
        // Close workbook without saving
        hrStatus = Excel_WorkbookClose (ExcelWorkbookHandle, NULL, CA_VariantBool (VFALSE), 
            CA_DEFAULT_VAL, CA_VariantBool (VFALSE));
        if (hrStatus < 0)
            goto Error;
        
        ClearObjHandle (&ExcelWorkbookHandle);
    }
    
    ClearObjHandle (&ExcelWorkbooksHandle);
        
    if (ExcelAppHandle)
    {   
         // Quit the Application
         hrStatus = Excel_AppQuit (ExcelAppHandle, &ErrorInfor);
         if (hrStatus < 0) goto Error;
        
         ClearObjHandle (&ExcelAppHandle);
    } 
    
    return 0;   
Error:    
    ReportAppAutomationError (hrStatus);
        
    return hrStatus;                    
}

//------------------------------------------------------------------------------------------------
long __stdcall ExcelShow( void )
{
    HRESULT hrStatus = 0;
    // Launch App
    // Connect to existing application if available
    //hrStatus = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
    //if (hrStatus < 0) 
    //   goto Error;
    
    // Make App Visible
    hrStatus = Excel_Set_ApplicationProperty (ExcelAppHandle, NULL, Excel_AppVisible, CAVT_BOOL, VTRUE);
    if (hrStatus < 0) 
       goto Error;
            
    MakeApplicationActive ();
    return 0;   
Error:    
    ReportAppAutomationError (hrStatus);
        
    return 0;
}
//------------------------------------------------------------------------------------------------
long __stdcall ExcelHide( void )
{
    HRESULT hrStatus = 0;
    // Launch App
    // Connect to existing application if available
    hrStatus = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
    if (hrStatus < 0) 
       goto Error;
    
    // Make App Visible
    hrStatus = Excel_Set_ApplicationProperty (ExcelAppHandle, NULL, Excel_AppVisible, CAVT_BOOL, VFALSE);
    if (hrStatus < 0) 
       goto Error;
            
    MakeApplicationActive ();
    return 0;   
Error:    
    ReportAppAutomationError (hrStatus);
        
    return 0;
}
//------------------------------------------------------------------------------------------------
long __stdcall ExcelCellSetValue( int nLine, int nColumn, char* pchValue )
{
   HRESULT error = 0;
   VARIANT CellData;

   //----------------------------------------------------------------
   // 1) Set cell in Range one at a time using an offset from 
   //    range's top left cell
   //----------------------------------------------------------------
   CA_VariantSetCString (&CellData, pchValue );
   error = Excel_RangeSetItem (ExcelRangeHandle, NULL, CA_VariantInt (nLine), CA_VariantInt (nColumn), CellData );
   if (error < 0)
      goto Error;
   
   CA_VariantClear (&CellData);
   return 0;

Error:
    CA_VariantClear (&CellData);
    ReportAppAutomationError (error);
        
    return error;
}
//------------------------------------------------------------------------------------------------
long __stdcall ExcelSave( void )
{
   HRESULT error = 0;

   if (ExcelWorkbookHandle) 
   { 
      error = Excel_WorkbookSave (ExcelWorkbookHandle, NULL);
                
      if (error < 0)
         goto Error;
   }
   return 0;   
Error:    
   if (error < 0) 
      ReportAppAutomationError (error);
   return error;
}
//------------------------------------------------------------------------------------------------
long __stdcall ExcelPrint( void )
{
   HRESULT error = 0;

   if (ExcelWorkbookHandle) 
   { 
      error = Excel_WorkbookPrintOut (ExcelWorkbookHandle, NULL, CA_DEFAULT_VAL,
         CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL,
         CA_DEFAULT_VAL, CA_DEFAULT_VAL);
                
      if (error < 0)
         goto Error;
   }
   return 0;   
Error:    
   if (error < 0) 
      ReportAppAutomationError (error);
   return error;
}

//------------------------------------------------------------------------------------------------
long __stdcall ExcelCreate( char* pchTemp, char* pchName )
{
   HRESULT error = 0;
   VARIANT FileName;
   VARIANT CellRange;

   error = Excel_Get_ApplicationProperty (ExcelAppHandle, NULL, Excel_AppWorkbooks, CAVT_OBJHANDLE, &ExcelWorkbooksHandle);
   if (error < 0)goto Error;
   error = CA_VariantSetCString(&FileName, pchTemp);
   error = Excel_WorkbooksAdd (ExcelWorkbooksHandle, NULL, FileName, &ExcelWorkbookHandle);
   CA_VariantClear(&FileName);
   error = CA_VariantSetCString(&FileName, pchName);

   if (error < 0)
      goto Error;
   // if file exists then delete it
   if (FileExists (pchName, 0))
   {
      error = DeleteFile (pchName);
      if (error < 0)
         return error;
   }
   error = Excel_Workbook_SaveAs (ExcelWorkbookHandle, NULL, FileName,
                     CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                     CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                     CA_DEFAULT_VAL, ExcelConst_xlNoChange,
                     CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                     CA_DEFAULT_VAL, CA_DEFAULT_VAL);
   CA_VariantClear(&FileName);
                
   if (error < 0)
      goto Error;

   error = Excel_Get_WorkbookProperty (ExcelWorkbookHandle, NULL, Excel_WorkbookActiveSheet, CAVT_OBJHANDLE, &ExcelWorksheetHandle);
   if (error < 0)goto Error;

   // Open new Range for Worksheet
   error = CA_VariantSetCString (&CellRange, EXCEL_ARRAY_OF_CELLS);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -