2005年08月07日

OLEによるEXCEL制御

今回はプログラムからエクセル(Microsoft Excel)を制御する方法を紹介する。
C++BuilderではEXCELを制御するためのコンポーネントが用意されているが、はっきり言って遅い。
そこで、標準のコンポーネントではなく、OLEによってEXCELを制御する方法を紹介する。

@フォームにボタンを貼り付ける。
 この際、画面はどうでもよい。
excel_form.jpg

A「Unit.h」を以下のように編集する。
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include   //追加
#include  //追加
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:  // IDE 管理のコンポーネント
  TButton *Button1;
  void __fastcall Button1Click(TObject *Sender);
private:  // ユーザー宣言
public:    // ユーザー宣言
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


B「Unit.cpp」を以下のように編集する。
//---------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  //-----------------------------------------------------------------------
  //エクセルファイル作成
  Variant excel = CreateOleObject("Excel.Application");
  excel.OlePropertySet("Visible", true);

  Variant books = excel.OlePropertyGet("Workbooks");
  books.OleProcedure("Add");
  Variant sheet = excel.OlePropertyGet("ActiveSheet");

  //-----------------------------------------------------------------------
  //中央寄せ
  Variant range = sheet.OlePropertyGet("Range", "A1:D2");
  range.OlePropertySet("HorizontalAlignment", xlCenterAcrossSelection);

  //-----------------------------------------------------------------------
  //タイトル:値
  Variant cell = sheet.OlePropertyGet("Cells", 1, 1);
  cell.OlePropertySet("Value", "かけ算のサンプル" );
  //タイトル:結合
  range = sheet.OlePropertyGet("Range", "A1:D1");
  range.OleFunction("Merge");
  //タイトル:罫線
  range.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);
  //タイトル:背景色
  Variant interior = range.OlePropertyGet("Interior");
  interior.OlePropertySet("Color", clAqua);

  //-----------------------------------------------------------------------
  //見出し1:値
  cell = sheet.OlePropertyGet("Cells", 2, 1);
  cell.OlePropertySet("Value", "No." );
  //見出し1:罫線
  cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);
  //見出し1:背景色
  interior = cell.OlePropertyGet("Interior");
  interior.OlePropertySet("Color", clLime);

  //見出し2:値
  cell = sheet.OlePropertyGet("Cells", 2, 2);
  cell.OlePropertySet("Value", "数値1" );
  //見出し2:罫線
  cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);
  //見出し2:背景色
  interior = cell.OlePropertyGet("Interior");
  interior.OlePropertySet("Color", clLime);

  //見出し3:値
  cell = sheet.OlePropertyGet("Cells", 2, 3);
  cell.OlePropertySet("Value", "数値3" );
  //見出し3:罫線
  cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);
  //見出し3:背景色
  interior = cell.OlePropertyGet("Interior");
  interior.OlePropertySet("Color", clLime);

  //見出し4:値
  cell = sheet.OlePropertyGet("Cells", 2, 4);
  cell.OlePropertySet("Value", "商" );
  //見出し4:罫線
  cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);
  //見出し4:背景色
  interior = cell.OlePropertyGet("Interior");
  interior.OlePropertySet("Color", clLime);

  //1〜4列目:値、罫線、文字色
  for (int col = 1; col <= 3; col++)
  {
    for (int row = 3; row <= 7; row++)
    {
      cell = sheet.OlePropertyGet("Cells", row, col);
      cell.OlePropertySet("Value", IntToStr(row - 2).c_str());
      cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);

      TColor color;
      switch (col)
      {
       case 1:  color = clBlack;  break;
       case 2:  color = clBlue;    break;
       case 3:  color = clRed;    break;
      }
      Variant font = cell.OlePropertyGet("Font");
      font.OlePropertySet("Color", color);
    }
  }

  //計算式
  AnsiString val;
  for (int row = 3; row <= 7; row++)
  {
    cell = sheet.OlePropertyGet("Cells", row, 4);
    val.sprintf("=B%d*C%d", row, row);
    cell.OlePropertySet("Value", val.c_str());
    cell.OleFunction("BorderAround", xlContinuous, xlThin, xlAutomatic, EmptyParam);

    interior = cell.OlePropertyGet("Interior");
    interior.OlePropertySet("Color", clYellow);
  }
}


Cコンパイルして実行すると、エクセルが起動して以下の画面を自動で生成する。
excel_result.jpg

D以下はソースと実行ファイルを示す。
 開発環境 (ole_excel_src.zip)
 実行ファイル (ole_excel_exe.zip)


・・・もし、これらの情報が少しでも役に立ったら、右上の広告をクリックしてほしいな・・・。m(__)m
posted by かねやん at 20:25| Comment(9) | TrackBack(3) | C++Builder6 | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
良い情報ありがとうございます。
参考にさせて頂きました。
Posted by ysk at 2005年10月26日 09:23
書き込むmature women amatuer qsm http://amatuer.bluesauce.com/mature-women-amatuer.html mature women amatuer
amatuer vidoe porn qsm http://amatuer.bluesauce.com/amatuer-vidoe-porn.html amatuer vidoe porn
amatuer teen sex videos qsm http://amatuer.bluesauce.com/amatuer-teen-sex-videos.html amatuer teen sex videos
amatuer nude women pics qsm http://amatuer.bluesauce.com/amatuer-nude-women-pics.html amatuer nude women pics
amatuer sex videos married couples qsm http://amatuer.bluesauce.com/amatuer-sex-videos--married-couples.html amatuer sex videos married couples
amatuer sex acts qsm http://amatuer.bluesauce.com/amatuer-sex-acts.html amatuer sex acts
amatuer series index sex qsm http://amatuer.bluesauce.com/amatuer-series-index-sex.html amatuer series index sex
absolute free amatuer porn sites qsm http://amatuer.bluesauce.com/absolute-free-amatuer-porn-sites.html absolute free amatuer porn sites
amatuer nude women community qsm http://amatuer.bluesauce.com/amatuer-nude-women-community.html amatuer nude women community
teen male str8 amatuer porn qsm http://amatuer.bluesauce.com/teen-male-str8-amatuer-porn.html teen male str8 amatuer porn
free movies of amatuer porn qsm http://amatuer.bluesauce.com/free-movies-of-amatuer-porn.html free movies of amatuer porn
private amatuer sex videos qsm http://amatuer.bluesauce.com/private-amatuer-sex-videos.html private amatuer sex videos
live amatuer sex qsm http://amatuer.bluesauce.com/live-amatuer-sex.html live amatuer sex
amatuer sex anal free qsm http://amatuer.bluesauce.com/amatuer-sex-anal-free.html amatuer sex anal free
amatuer first date porn video qsm http://amatuer.bluesauce.com/amatuer-first-date-porn-video.html amatuer first date porn video
dirty amatuer sex qsm http://amatuer.bluesauce.com/dirty-amatuer-sex.html dirty amatuer sex
video sex gay amatuer qsm http://amatuer.bluesauce.com/video-sex-gay-amatuer.html video sex gay amatuer
amatuer sex personals qsm http://amatuer.bluesauce.com/amatuer-sex-personals.html amatuer sex personals
amatuer housewives masturbating qsm http://amatuer.bluesauce.com/amatuer-housewives-masturbating.html amatuer housewives masturbating
amatuer video girls qsm http://amatuer.bluesauce.com/amatuer-video-girls.html amatuer video girls
amatuer porn audition qsm http://amatuer.bluesauce.com/amatuer-porn-audition.html amatuer porn audition
amatuer home made sex may qsm http://amatuer.bluesauce.com/amatuer-home-made-sex-may.html amatuer home made sex may
amatuer sex cam qsm http://amatuer.bluesauce.com/amatuer-sex-cam.html amatuer sex cam
free sick amatuer sex photos qsm http://amatuer.bluesauce.com/free-sick-amatuer-sex-photos.html free sick amatuer sex photos
famous amatuer porn stars qsm http://amatuer.bluesauce.com/famous-amatuer-porn-stars.html famous amatuer porn stars
amatuer women at home naked qsm http://amatuer.bluesauce.com/amatuer-women-at-home-naked.html amatuer women at home naked
amatuer blogs sex fun qsm http://amatuer.bluesauce.com/amatuer-blogs-sex-fun.html amatuer blogs sex fun
amatuer porn latina qsm http://amatuer.bluesauce.com/amatuer-porn--latina.html amatuer porn latina
skinny sex amatuer qsm http://amatuer.bluesauce.com/skinny-sex-amatuer.html skinny sex amatuer
free amatuer homemade porn video qsm http://amatuer.bluesauce.com/free-amatuer-homemade-porn-video.html free amatuer homemade porn video
Posted by mature women amatuer at 2006年10月03日 12:23
http://order-phentermine.buy-phentermine-mmx.info/en/where-to-order-phentermine.html where to order phentermine<br>http://order-phentermine.buy-phentermine-mmx.info/es/order-phentermine-diet-pill.html p?ldora de la dieta del phentermine de la orden<br>http://order-phentermine.buy-phentermine-mmx.info/fr/blue-order-phentermine.html phentermine bleu d\'ordre<br>http://order-phentermine.buy-phentermine-mmx.info/pt/where-to-order-phentermine-online.html onde requisitar em linha o phentermine<br>http://phentermine-diet-pill.buy-phentermine-mmx.info/es/career-diet-health-phentermine-pill.html p?ldora del phentermine de la salud de la dieta de la carrera<br>http://phentermine-diet-pill.buy-phentermine-mmx.info/pt/diet-hcl-phentermine-pill.html pill do phentermine do hcl da dieta<br>http://effects-phentermine-side.buy-phentermine-mmx.info/fr/effects-fastin-phentermine-side.html effectue le cコt? de phentermine de fastin<br>http://effects-phentermine-side.buy-phentermine-mmx.info/it/37-effects-phentermine-phentermine-search-side.html lato di ricerca di phentermine di phentermine di 37 effetti<br>http://effects-phentermine-side.buy-phentermine-mmx.info/pt/effects-negative-phentermine-side.html efetua o lado negativo do phentermine<br>http://effects-phentermine-side.buy-phentermine-mmx.info/pt/effects-phentermine-phentermine-search-side.html lado da busca do phentermine do phentermine dos efeitos<br>http://cheap-phentermine.buy-phentermine-mmx.info/en/buy-cheap-phentermine-free-fedex.html buy cheap phentermine free fedex<br>http://cheap-phentermine.buy-phentermine-mmx.info/es/book-buy-cheap-guest-phentermine-site-uk.html sitio barato Reino Unido del phentermine de la hu?ped de la compra del libro<br>http://cheap-phentermine.buy-phentermine-mmx.info/es/cheap-phentermine-yellow-online.html amarillo barato del phentermine en l?nea<br>http://cheap-phentermine.buy-phentermine-mmx.info/fr/cheap-phentermine-yellow-online.html jaune bon march? de phentermine en ligne<br>http://cheap-phentermine.buy-phentermine-mmx.info/fr/cheap-price-on-phentermine.html prix bon march? sur le phentermine<br>http://cheap-phentermine.buy-phentermine-mmx.info/it/ phentermine poco costoso<br>http://cheap-phentermine.buy-phentermine-mmx.info/pt/cheap-phentermine-for-sale.html phentermine barato para a venda<br>http://cheap-phentermine.buy-phentermine-mmx.info/pt/finding-cheap-phentermine.html encontrando o phentermine barato<br>http://buy-phentermine-online.buy-phentermine-mmx.info/en/buy-phentermine-online-com.html buy phentermine online com<br>http://buy-phentermine-online.buy-phentermine-mmx.info/it/buy-phentermine-online-with-pay-pal.html comprare il phentermine in linea con paga pal<br>http://online-phentermine.buy-phentermine-mmx.info/de/ on-line-phentermine<br>http://online-phentermine.buy-phentermine-mmx.info/de/buy-online-phentermine-prescription.html on-line-phentermine Verordnung kaufen<br>http://online-phentermine.buy-phentermine-mmx.info/es/online-order-phentermine-where.html phentermine en l?nea de la orden donde<br>http://online-phentermine.buy-phentermine-mmx.info/pt/best-diet-online-pharmacy-phentermine-pill.html pill em linha do phentermine do pharmacy da mais melhor dieta<br>http://online-phentermine.buy-phentermine-mmx.info/pt/get-online-phentermine-prescription.html come ?r a prescri?o em linha do phentermine<br>http://buy-phentermine.buy-phentermine-mmx.info/it/buy-buy-phentermine.html comprare il phentermine del buy<br>http://buy-phentermine.buy-phentermine-mmx.info/pt/buy-phentermine-online-pharmacy.html comprar o phentermine pharmacy em linha<br>http://phentermine-prescription.buy-phentermine-mmx.info/de/ phentermine Verordnung<br>http://phentermine-prescription.buy-phentermine-mmx.info/es/buy-no-phentermine-prescription.html no comprar ninguna prescripciハ del phentermine<br>http://phentermine.buy-phentermine-mmx.info/en/phentermine-side-affect.html phentermine side affect
Posted by order phentermine at 2006年10月10日 03:05
グッチ 通販
Posted by christian louboutin japan at 2013年07月13日 20:07
�サソ, <a href="http://bestbinaryoptions4u.com">binary options trading uk</a>, [url="http://bestbinaryoptions4u.com"]binary options trading uk[/url], http://bestbinaryoptions4u.com binary options trading uk, xehkm,
Posted by kUiPHuuo at 2014年02月11日 08:38
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:


この記事へのトラックバック

かっこわるい表示
Excerpt: このブログの記事エリアの幅を可変に設定したときから、記事タイトルの枠線が正常に表
Weblog: 日曜プログラマのブログ
Tracked: 2005-08-08 09:51

anal rape
Excerpt: <a href="http://www.accu-log.com/wwwboard/messages/756.htm">brutal dildos</a> fantasy forced sex <a href="http://www.accu-log.com/wwwboard/messages/757.htm">forced sex</a> peeing <a href="http://www.accu-log.com/wwwboard/messages/758.htm">brutal dildo</a> young models <a href="http://www.accu-log.com/wwwboard/messages/759.htm">brutal dildos</a> hidden ca..
Weblog:
Tracked: 2005-12-28 20:34

hairy and natural
Excerpt: <a href="http://www.hyperjump.net/aezia/wwwboard/6597.html">incest sex</a> kim possible hentai <a href="http://www.hyperjump.net/aezia/wwwboard/6599.html">insest</a> tights <a href="http://www.hyperjump.net/aezia/wwwboard/6604.html">mom and son</a> sublime directory <a href="http://www.hyperjump.net/aezia/wwwboard/6606.html">mother daughter sex</a> free6..
Weblog:
Tracked: 2005-12-28 20:34
×

この広告は1年以上新しい記事の投稿がないブログに表示されております。