Here are already packaged Excel class
The header file:
# pragma once
//excel. H
# pragma once
# include "CApplication. H"
# include "CRange. H"
# include "CWorkbook. H"
# include "CWorkbooks. H"
# include "CWorksheet. H"
# include "CWorksheets. H"
The class Excel
{
Private:
Cstrings openFileName;
CWorkbook workBook.//the current file
CWorkbooks books;//ExcelBook collections, more files using the
CWorksheet workSheet.//the current use of sheet
CWorksheets sheets;//Excel sheet collection
CRange currentRange;//the current operation area
Bool isLoad;//is already loaded a data sheet
COleSafeArray safeArray.
Protected:
The static CApplication application;//Excel process instance
Public:
Excel ();
Virtual ~ Excel ();
Void show (bool bShow);
//check whether a cell as a string
Bool isCellString (long iRow, long iColumn);
//check whether a cell is the numerical
Bool isCellInt (long iRow, long iColumn);
//get a cell string
Cstrings getCellString (long iRow, long iColumn);
//get a cell integer
Int getCellInt (long iRow, long iColumn);
//get a double cell data
Double getCellDouble (long iRow, long iColumn);
//total number of rows
Int getRowCount ();
//integer column
Int getColumnCount ();
//use a shell
Bool loadSheet (long tableId, bool preLoaded=false).
Bool loadSheet (cstrings sheet, bool preLoaded=false).
//by the name of the serial number has a shell
Cstrings getSheetName (long tableID);
//get the total number of sheel
Int getSheetCount ();
//open the excel file
Bool open (const char * fileName);
/open/close excel file
Void close (bool ifSave=false);
//save as a excel file
Void saveAsXLSFile (const cstrings & amp; XlsFile);
//the name of the open file
Cstrings getOpenFileName ();
//open the sheel the name of the
Cstrings getOpenSheelName ();
//to write an int value cell
Void setCellInt (long iRow, long iColumn, int newInt);
//to write a string cell
Void setCellString (long iRow, long iColumn, cstrings newString);
Public:
//initialize Excel_OLE
The static bool initExcel ();
//release Excel_OLE
The static void release ();
//the name of the column of
The static char * getColumnName (long iColumn);
Protected:
Void preLoadSheet ();
};
CodePudding user response:
Source:# include "stdafx. H"
#include
# include "Excel. H"
COleVariant
CovTrue (TRUE) (short),
CovFalse ((short) FALSE),
CovOptional ((long) DISP_E_PARAMNOTFOUND VT_ERROR);
CApplication Excel: : application;
Excel: : Excel () : isLoad (false)
{
}
Excel: : ~ Excel ()
{
//close ();
}
Bool Excel: : initExcel ()
{
//create Excel 2000 server (start Excel)
if (! Application. CreateDispatch (_T (" Excel. Application "), nullptr))
{
MessageBox (nullptr, _T (" failed to create Excel services, you may not have to install Excel, please check!" ), _T (" error "), MB_OK);
return FALSE;
}
Application. Put_DisplayAlerts (FALSE);
return true;
}
Void Excel: : release ()
{
Application. The Quit ();
Application. ReleaseDispatch ();
Application=nullptr;
}
Bool Excel: : open (const char * fileName)
{
//to close the file
Close ();
//template is used to establish the new document
Books. AttachDispatch (application. Get_Workbooks (), true);
LPDISPATCH lpDis=nullptr;
LpDis=books. The Add (COleVariant (cstrings (fileName)));
If (lpDis)
{
WorkBook. AttachDispatch (lpDis);
Sheets. AttachDispatch (workBook. Get_Worksheets ());
OpenFileName=fileName;
return true;
}
return false;
}
Void Excel: : close (bool ifSave)
{
//if the file is already open and close the file
if (! OpenFileName. IsEmpty ())
{
//if the preservation, to the user control, let the users themselves, if you SAVE, there will be a strange wait
If (ifSave)
{
//show (true);
}
The else
{
WorkBook. Close (COleVariant (short) (FALSE), COleVariant (openFileName), covOptional);
Books. The Close ();
}
//empty open file name
OpenFileName. Empty ();
}
Sheets. ReleaseDispatch ();
WorkSheet. ReleaseDispatch ();
CurrentRange. ReleaseDispatch ();
WorkBook. ReleaseDispatch ();
Books. ReleaseDispatch ();
}
Void Excel: : saveAsXLSFile (const cstrings & amp; XlsFile)
{
WorkBook. SaveAs (COleVariant (xlsFile),
CovOptional,
CovOptional,
CovOptional,
CovOptional,
CovOptional,
0,
CovOptional,
CovOptional,
CovOptional,
CovOptional,
CovOptional);
return;
}
Int Excel: : getSheetCount ()
{
The return sheets. Get_Count ();
}
Cstrings Excel: : getSheetName (long tableID)
{
CWorksheet sheet;
Sheet. AttachDispatch (sheets. Get_Item (COleVariant ((long) tableID)));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull