0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++ Builder XE4 > Excel処理 > 1つめのシートを選択する (シート名は任意) | getCellValue() | setCellValue()

Last updated at Posted at 2018-04-13
動作環境
C++ Builder XE4

C++ Builder XE4 > Link > Access to Excel cells in "C++ Builder XE4" | +入力ファイル情報
の続き。

最初のシートの選択

最初のシートを選択するには。

void __fastcall TForm1::Button2Click(TObject *Sender)
...
    Sheet1 = WorkBook1.PG("ActiveSheet");
    WorkBook1.PG("Sheets", 1).PR("Select");

上記を逆順に実行すると、Sheet1には最初のシートが入るのだろう。

code v0.3, v0.4

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <System.Win.ComObj.hpp>  // EXCEL処理用
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

/*
v0.4 Jan. 22, 2020
        - remove String declaration for setCellValue() as a return value
v0.3 Apr. 13, 2018
	- write text to 1st and 2nd sheets
v0.2 Apr. 13, 2018
	- add setCellValue()
	- add getCellValue()
v0.1 Apr. 13, 2018
	imported from
	http://mojelder.hatenablog.com/entry/2015/06/29/112326
*/


__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

static String getCellValue(Variant asheet, int row, int col)
{
	return asheet.OlePropertyGet("Cells").
			OlePropertyGet("Item", row, col).OlePropertyGet("Value");
}

static void setCellValue(Variant asheet, int row, int col, WideString val)
{
	asheet.OlePropertyGet("Cells").
		OlePropertyGet("Item", row, col).OlePropertySet("Value", val);
}

void __fastcall TForm1::B_convertClick(TObject *Sender)
{
	static const WideString kXlsFile = "あいうえお.xlsx";

	Variant ExcelApp = CreateOleObject("Excel.Application");
	try
	{
		WideString inFilename = ExtractFileDir(ParamStr(0)) + "\\" + kXlsFile; // Openに使う文字列はWideString定義
		// not used: WideString inSheetname = L"チェック"; // シート名もWideString (または番号)
		Variant xls_books;
		Variant xls_abook;
		Variant xls_sheets;
		Variant xls_asheet;
		WideString writeText = "文字";  // 書き込む文字列 // WideString型で定義

		try
		{
			ExcelApp.OlePropertySet("Visible", false); // Excel not shown

			xls_books = ExcelApp.OlePropertyGet("Workbooks");
			xls_abook = xls_books.OleFunction("Open", inFilename);
			xls_sheets = xls_abook.OlePropertyGet("WorkSheets");

			//xls_asheet = xls_sheets.OlePropertyGet("Item", inSheetname);  // 固定シート名

			// 1. 最初のシート
			// シート取得
			xls_abook.OlePropertyGet("Sheets", 1).OleProcedure("Select");
			xls_asheet = xls_abook.OlePropertyGet("ActiveSheet");
			// 読込み、書換え
			this->Caption = getCellValue(xls_asheet, 5, 3);
			setCellValue(xls_asheet, 1, 1, writeText);

			// 2. 次のシート
			// シート取得
			xls_abook.OlePropertyGet("Sheets", 2).OleProcedure("Select");
			xls_asheet = xls_abook.OlePropertyGet("ActiveSheet");
			// 書換え
			setCellValue(xls_asheet, 2, 2, writeText);

			xls_abook.OleProcedure("Save"); //開いた*.xlsxを保存
			ExcelApp.OleProcedure("Quit"); //Excel終了。
		}
		__finally
		{
			xls_asheet = Unassigned(); // 変数を初期状態に
			xls_sheets = Unassigned();
			xls_abook = Unassigned();
			xls_books = Unassigned();
		}
	}
	__finally
	{
		ExcelApp = Unassigned();
	}
}
//---------------------------------------------------------------------------

実行

下記のエクセルファイルを用意する。

  • あいうえお.xlsx
  • 2つのシートを持つ
  • シート名は任意 (例: サンプル1、サンプル2)
  • 1つめのシートのC5セルに値を入れる (例: 43)

処理実施後、以下のようになる。

qiita.png

qiita.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?