LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > Excel + OLE > Rangeクリア > ClearとClearContents

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

関連

参考

処理概要

  • Rangeで行列を指定してクリアする

上記以外には下記を行っている。

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

code

下記の処理3においてRangeクリアしている。

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");

    DWORD stTim = GetTickCount();

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

            // 3 Range Clear
            xls_asheet.OlePropertyGet("Range", WideString("A1:E5")).OleProcedure("Clear");

            // 4. Save
            xls_abook.OleProcedure("SaveAs", dstFile); //開いた*.xlsxを別名保存
            closeExcel = true;
            ExcelApp.Exec(Procedure("Quit"));

            DWORD edTim = GetTickCount();
            String msg = IntToStr((int)(edTim - stTim)) + L"(msec) has passed";

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

処理結果

A1からE5の範囲の値が消えるようになった。
また、設定していた書式も「標準」に戻っている。

書式だけ残して値を消すのは別の方法のようだ。

書式を残したクリア > ClearContents

書式だけ残して値を消すのはClearContents。

参考: https://stackoverflow.com/questions/11137636/can-i-clear-cell-contents-without-changing-styling
参考: Range.ClearContents メソッド (Excel)

// 3 Range Clear
xls_asheet.OlePropertyGet("Range", WideString("A1:E5")).OleProcedure("ClearContents");
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