Accessing Excel Cells from Visual C++
-
I am just learning VC++. I am trying to read from an Excel file using VC++ but I can't find any good help on how to do it. Does anyone have any ideas? :confused: :confused:
G'Day Havoc, You should be able to find enough information in MSDN to get you going. Here is one link to a few articles http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dno97ta/html/msoautovc.asp failing that try searching with the key words 'excel' and 'autmation' Good luck Richard.
-
G'Day Havoc, You should be able to find enough information in MSDN to get you going. Here is one link to a few articles http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dno97ta/html/msoautovc.asp failing that try searching with the key words 'excel' and 'autmation' Good luck Richard.
#ifndef _EXCELSPACE_H #define _EXCELSPACE_H #include "excel9.h" class CExcelSpace : public CObject { public: CExcelSpace(); ~CExcelSpace(); BOOL OpenExcel(LPCTSTR lpszFileName,LPCTSTR lpszSheetName, BOOL m_bReadOnly = TRUE, BOOL bNewFile = FALSE, BOOL bNewSheet = FALSE ); void Close(); void GetRowColCount(int &row, int &col); COleVariant GetCellValue(int row, int col); BOOL SetCellValue(int row, int col,COleVariant var); BOOL DeleteRow(int row); CString GetFileName() { return m_szFileName; }; BOOL IsReadOnly() { return m_bReadOnly; }; void PrepareFetchData(); void EndFetchData(); BOOL ActiveSheet(LPCTSTR lpszSheetName); BOOL IsSheetExist(LPCTSTR lpszSheetName); BOOL DeleteSheet(LPCTSTR lpszSheetName); BOOL NewSheet(LPCTSTR lpszSheetName); _Application GetApplication() { return excelApp; } protected: CString m_szFileName; CString m_szSheetName; BOOL m_bNewFile; BOOL m_bReadOnly; BOOL m_bValidate; BOOL m_bServiceStarted; BOOL bPreparedFectch; CStringArray fldNames; _Application excelApp; int rowCount, colCount; int curRow; COleSafeArray sa; BOOL StartExcelService(); CString InnerGetColumnName(int col); CString GetCellName(int row, int col); int NameToIndex(LPCTSTR lpszFldName); // Dataset function public: int GetRecordCount() { return rowCount; }; int GetColCount() { return colCount; }; BOOL AddColumn(LPCTSTR lpszColName, int nColType); CString GetColumnName(int col); int GetColumnType(int col); BOOL Delete(); BOOL InsertRow(BOOL bAppend); BOOL Update(); COleVariant GetFieldValue(int col); COleVariant GetFieldValue(LPCTSTR lpszFldName); void SetFieldValue(int col,COleVariant var); void SetFieldValue(LPCTSTR lpszFldname,COleVariant var); BOOL MoveFirst(); BOOL MoveNext(); BOOL MoveBefore(); BOOL IsEOF(); BOOL IsBOF(); public: static BOOL NewExcelSheet(LPCTSTR lpszFilename, LPCTSTR lpszSheetName); static BOOL IsSheetExist(LPCTSTR lpszFilename, LPCTSTR lpszSheetName); static BOOL DeleteSheet(LPCTSTR lpszFilename, LPCTSTR lpszSheetName); static CString OleVariantToStr( const COleVariant & Var); }; #endif ------------------------------------- CExcelSpace::CExcelSpace() { m_szFileName = ""; m_bReadOnly = TRUE; m_bValidate = FALSE; bPreparedFectch = FALSE; curRow = -1; rowCount = colCount = 0; m_bServiceStarted = FALSE; } CExcelSpace::~CExcelSpace() { fldNames.RemoveAll(); Close(); } void CExcelSpac