C++ Builder CE 実用プログラミング

#1 Scan Projectを作ろう の2 Excelとの連携

最初に初期化から、

#define WSP(s) WideString(s)

#include <ComObj.hpp>
...
...
...
	Variant excelApp;

	Variant	WorkBooks;
	Variant	WorkBook;
	Variant	WorkSheets;
	Variant	WorkSheet;
   //	Variant excelSheet = excelApp.OlePropertyGet("Sheet");
	Variant excelCell;
	String titles[] = { "","machine","kind","name","path","date","git status","clone_url","binaries","description"};

void Excel_Setup()
{
     	try {
						excelApp = CreateOleObject("Excel.Application");

		}
		catch ( EOleSysError& ){
				ShowMessage("Excelはありませんね?");
				//excel = NULL;
		}
 	excelApp.OlePropertySet("Visible", (Variant)true);

	WorkBooks = excelApp.OlePropertyGet("Workbooks");
	WorkBook = WorkBooks.OleFunction("Add");
	WorkSheets = WorkBook.OlePropertyGet("WorkSheets");
	WorkSheet = WorkSheets.OlePropertyGet("Item",WSP("Sheet1"));
	//WorkSheet = WorkBook.OlePropertyGet("Sheets").OleFunction("Item", 1);
   //	Variant excelSheet = excelApp.OlePropertyGet("Sheet");
	excelCell = WorkSheet.OlePropertyGet("Cells");
	SetTitle();



}

ほとんど定番ですね。VariantとかOlePropertyGetやらOlePropertyFunctionとかが錯綜します。クラス作れればいいんですけどね。今一。SetTitle()で第1行を作ります。

void SetTitle() // so far ok
{
	int i = 1;

	Variant range;
	Variant font;
	Variant numberformat;

	double width;

	for( int j = 1 ; j < 11 ; j++ ) {
		 range = WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item",(Variant) i,(Variant)j);
		 range.OlePropertySet("Value",WSP(titles[j-1].c_str()));
	}

    range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,2); // kind width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 3.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,3); // kind width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 3.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,5); // path width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 10.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?

		range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,4); // name width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 3.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,6); // date width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 2.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?

	//range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 2,4);
	//range.OlePropertySet("NumberFormatLocal",WSP("yyyymmdd"));

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 1,8); // clone_url width
	width = range.OlePropertyGet("ColumnWidth");
	width *= 10.0;
	range.OlePropertySet("ColumnWidth",(Variant)width); // x3?


	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 2,7); // git status
	//Variant interior = range.OlePropertyGet("Interior");
	font = range.OlePropertyGet("Font");
	font.OlePropertySet("Size",(Variant)18);
	font.OlePropertySet("Name",WSP("Wingdings"));

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", 2,9); // binaries
	//Variant interior = range.OlePropertyGet("Interior");
	font = range.OlePropertyGet("Font");
	font.OlePropertySet("Size",(Variant)18);
	font.OlePropertySet("Name",WSP("Wingdings"));


}

一つエントリーを追加するのは、

void Dumpaentry(std::string kind,std::string path,std::string date,std::string gitstatus,std::string remoteurl,
std::string binarystatus)
{
	 wchar_t check[4] = { 254,0,0,0};
	std::string nstring = fixpath(path);
	std::filesystem::path apath = nstring;
	std::string name(apath.filename().string());
	std::string hyperlinks = "=HYPERLINK(\"" + nstring + "\",\"" + nstring + "\")";

	//Form1->Memo1->Lines->Add(hyperlinks.c_str());

	//name =  apath.filename().c_str();
	Variant font;

	Variant range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,2);
	Form1->Memo1->Lines->Add("machine " + Form1->Label5->Caption);
	range.OlePropertySet("Value",WSP(Form1->Label5->Caption));


	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,3);
	Form1->Memo1->Lines->Add("kind " + UnicodeString(name.c_str()));
	range.OlePropertySet("Value",WSP(kind.c_str()));


	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,4);
	Form1->Memo1->Lines->Add("name " + UnicodeString(name.c_str()));
	range.OlePropertySet("Value",WSP(name.c_str()));

	//range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,3);

	Form1->Memo1->Lines->Add("path " + UnicodeString(nstring.c_str()));
	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,5);
	//range.OlePropertySet("Value",WSP(nstring.c_str()));
	range.OlePropertySet("Formula",WSP(hyperlinks.c_str()));

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,6);
	Form1->Memo1->Lines->Add("date " + UnicodeString(date.c_str()));
	range.OlePropertySet("Value",WSP(date.c_str()));

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,7);
	Form1->Memo1->Lines->Add("git status " + UnicodeString(gitstatus.c_str()));
	if( gitstatus.find(".git found") != std::string::npos ){
		font = range.OlePropertyGet("Font");
		font.OlePropertySet("Size",(Variant)18);
		font.OlePropertySet("Name",WSP("Wingdings"));
		range.OlePropertySet("value",WSP(check));    ;
	}

	range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,8);
	Form1->Memo1->Lines->Add("remote url " + UnicodeString(remoteurl.c_str()));
	range.OlePropertySet("Value",WSP(remoteurl.c_str()));

   range =  WorkSheet.OlePropertyGet("Cells").OlePropertyGet("Item", entry,9);
	Form1->Memo1->Lines->Add("bin status " + UnicodeString(binarystatus.c_str()));
	//range.OlePropertySet("Value",WSP(binarystatus.c_str()));
	if( binarystatus.find("exists") != std::string::npos ){
		font = range.OlePropertyGet("Font");
		font.OlePropertySet("Size",(Variant)18);
		font.OlePropertySet("Name",WSP("Wingdings"));
		range.OlePropertySet("value",WSP(check));
	}

//Form1->Memo1->Lines->Add(UnicodeString(line.c_str()));
   entry++;

}

このプログラムのプロジェクトが在る場所をscanした様子が、

で、出力行の例は、

になります。

コメント