クラス CPfExcel

CPfExcel.cppは次のとおりです。

//
// PfExcel.cpp
//
#include <vcl.h>
#pragma hdrstop

#include "PfExcel.h"

//---------------------------------------------------------------------------
// class CPfExcel
//---------------------------------------------------------------------------
CPfExcel::CPfExcel() {
Opened = false;
ColCount = RowCount = -1;
ObjExcel = Unassigned;
WorkBook = Unassigned;
UsedRange = Unassigned;
}
//---------------------------------------------------------------------------
CPfExcel::~CPfExcel() {
Close();
}
//---------------------------------------------------------------------------
bool CPfExcel::IsInstalled() {
bool rtn = false;
Variant obj;
try {
obj = Variant::CreateObject("Excel.Application");
}
catch (Exception &e) {
;//ErrMsg = e.Message;
}
if (obj.Type() == varDispatch)
rtn = true;
obj = Unassigned;
return rtn;
}
//---------------------------------------------------------------------------
bool CPfExcel::Open(const String &fname) {
Opened = false;
ColCount = RowCount = -1;
FileName = fname;
if (FileName.IsEmpty())
return false;
try {
Variant asheet;
ObjExcel = Variant::CreateObject("Excel.Application");
//Excel を表示
ObjExcel.OlePropertySet("Visible", false); // OK
// 警告やメッセージを表示しない
ObjExcel.OlePropertySet("DisplayAlerts", false);
// ファイルを読む
WorkBook = ObjExcel.OlePropertyGet("WorkBooks").
OleFunction("Open", (OleVariant)FileName);
//アクティブシート
asheet = WorkBook.OlePropertyGet("ActiveSheet");
//使用している範囲
UsedRange = asheet.OlePropertyGet("UsedRange");
asheet = Unassigned;
ColCount = UsedRange.OlePropertyGet("Columns").OlePropertyGet("Count");
RowCount = UsedRange.OlePropertyGet("Rows").OlePropertyGet("Count");
Opened = true;
}
catch (Exception &e) {
ErrMsg = e.Message;
}
return Opened;
}
//---------------------------------------------------------------------------
int CPfExcel::GetColCount() {
return ColCount;
}
//---------------------------------------------------------------------------
int CPfExcel::GetRowCount() {
return RowCount;
}
//---------------------------------------------------------------------------
bool CPfExcel::GetRow(int irow, TStringList *list) {
// irow は 0 から
int rtn = false;
if (!Opened)
return rtn;
try {
Variant var_array =
UsedRange.OlePropertyGet("Rows", irow+1).OlePropertyGet("Value2");
if (var_array.ArrayDimCount() == 2) {
int lx = var_array.ArrayLowBound(1);
int lb = var_array.ArrayLowBound(2);
int hb = var_array.ArrayHighBound(2);
for (int i = lb; i <= hb; i++) {
list->Add(var_array.GetElement(lx, i));
}
rtn = true;
}
}
catch (Exception &e) {
ErrMsg = e.Message;
}
return rtn;
}
//---------------------------------------------------------------------------
void CPfExcel::Close() {
if (Opened) {
try {
if (WorkBook.Type() == varDispatch) {
WorkBook.OleProcedure("Close");
ObjExcel.OleProcedure("Quit");
}
}
catch (Exception &e) {
ErrMsg = e.Message;
}
Opened = false;
}
UsedRange = Unassigned;
WorkBook = Unassigned;
ObjExcel = Unassigned;
}
//---------------------------------------------------------------------------


CPfExcel.hは次のとおりです。

//---------------------------------------------------------------------------
// PfExcel.h
#ifndef PfExcelH
#define PfExcelH
//---------------------------------------------------------------------------
class CPfExcel
{
protected:
Variant ObjExcel, WorkBook, UsedRange;
String FileName;
String ErrMsg;
bool Opened;
int ColCount, RowCount;
public:
CPfExcel();
~CPfExcel();
bool Open(const String &fname);
int GetColCount();
int GetRowCount();
bool GetRow(int irow, TStringList *list);
// irow は 0 から
void Close();

static bool IsInstalled();
};
//---------------------------------------------------------------------------
#endif