LoginSignup
1
1

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-04-13
実装環境
C++ Builder XE4
動作環境
Windows 8.1 Pro (32bit)
Excel 2016

XE4でExcelファイルの読込みと書出しができないか探していた。

@mojeld さんの記事が見つかった。
Access to Excel cells in "C++ Builder XE4"

情報感謝です。

ファイル

以下のExcelファイルを用意する。

  • Name: あいうえお.xlsx
  • シート名: チェック
  • 以下のようなデータを用意する
    • セルC5に値が入っていること (例: 43)

処理前

qiita.png

処理後
qiita.png

qiita.png

code v0.2

セルへの読書の処理を関数にしてみた。
また、書き方を自分のスタイルに変更している。

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.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 String 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定義
        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);

            this->Caption = getCellValue(xls_asheet, 5, 3);
            setCellValue(xls_asheet, 1, 1, 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();
    }
}
//---------------------------------------------------------------------------
1
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
1
1