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 5 years have passed since last update.

C++ Builder XE4 > Excel + OLE > 5行5列に3.14を代入 | 5行5列の内容をコピー・ペースト

Last updated at Posted at 2018-06-28
動作環境
- 実装
    - C++ Builder XE4
- 確認
    - Windows 8.1 Pro
    - Office 2016

関連

処理概要

  • base.xlsxを読込む
  • 5行5列に"3.14"と記載する
  • 上記の行列を別の行列にコピー&ペーストする
  • out.xlsxというファイル名で保存する

参考 (コピー&ペースト処理)

code v0.1

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

#include <vcl.h>
#pragma hdrstop

#include <System.Win.ComObj.hpp>  // EXCEL処理用

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::btnConvertClick(TObject *Sender)
{
	String curDir = ExtractFileDir(Application->ExeName);

	WideString srcFile = curDir + "\\base.xlsx";
	WideString dstFile = curDir + "\\out.xlsx";

	//--- Excel処理
	bool closeExcel = false;

	Variant ExcelApp = CreateOleObject("Excel.Application");

	try
    {
		Variant xls_books;
        Variant xls_abook;
        Variant xls_sheets;
		Variant xls_asheet;

		try {
			ExcelApp.OlePropertySet("Visible", false); // Excel not shown
			ExcelApp.OlePropertySet("DisplayAlerts", false); // No dialog for overwrite

			xls_books = ExcelApp.OlePropertyGet("Workbooks");
			xls_abook = xls_books.OleFunction("Open", srcFile);
			// 最初のシート選択
			xls_abook.OlePropertyGet("Sheets", 1).OleProcedure("Select");
			xls_asheet = xls_abook.OlePropertyGet("ActiveSheet");
			// 1. Fill
			for (int ci = 1; ci <= 5; ci++) { // ci: column index
				for (int ri = 1; ri <= 5; ri++) { // ri: row index
					xls_asheet.OlePropertyGet("Cells", ri, ci).OlePropertySet("Value", WideString("3.141592"));
				}
			}
			// 2. Copy and Paste
			Variant wrkRange = xls_asheet.OlePropertyGet("Range", WideString("A1:E5"));
			wrkRange.OleProcedure("Copy");
			xls_asheet.OlePropertyGet("Range", WideString("F7:J11")).OleProcedure("Select");
			xls_asheet.OleProcedure("Paste");
			// 3. Save
			xls_abook.OleProcedure("SaveAs", dstFile); //開いた*.xlsxを別名保存
			closeExcel = true;
			ExcelApp.Exec(Procedure("Quit"));

			String msg = L"Save to [" + dstFile + L"]";
			ShowMessage(msg);
		}
		__finally
        {
            xls_asheet = Unassigned(); // 変数を初期状態に
            xls_sheets = Unassigned();
            xls_abook = Unassigned();
            xls_books = Unassigned();
        }
    }
    __finally
    {
        ExcelApp = Unassigned();
	}
}
//---------------------------------------------------------------------------

結果

out.xlsxファイル。

qiita.png

今回の結果では"3.141592"という数値が代入された。
base.xlsxではフォーマットを指定していない。

コピー先の選択

コピー先はRange選択でなくCells選択でも可能。

xls_asheet.OlePropertyGet("Cells", 7, 7).OleProcedure("Select");
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?