delphiでエクセルを操作したいのですが、ヘルプがないために
どのように操作すればいいのかわかりません。
1.新規のエクセルを作ってデータを入力して保存
2.概存のエクセルを開いてデータを入力して保存
どちらでもいいので分かる方がいらしたら教えてください。
ヘルプの取得方法でもいいです。
よろしくお願いします。
以下のサンプルは、「http://www.nifty.ne.jp/forum/fdelphi/samples/index.htm」の
「http://www.nifty.ne.jp/forum/fdelphi/samples/00389.html」以下のスレッドに
書いてあったものを、Win2000 + Delphi6Personal + Excel2000で検証したものです。
//-----ここから-----------
Usesに ComObjを追加します。
//新規作成
procedure TForm1.Button1Click(Sender: TObject);
var
Excel: Variant;
WorkBook: Variant;
WorkSheet: Variant;
begin
//仮に三枚のシートを使うと仮定
Excel := CreateOleObject('Excel.Application');
WorkBook := Excel.Workbooks.Add;
while Workbook.sheets.count < 3 do
begin
Workbook.sheets.add;
end;
WorkSheet := WorkBook.WorkSheets[1];
WorkSheet.name:='poo';
WorkSheet.Cells[1,1].Value:='pooo';//セル"A1"に文字を入力
WorkSheet.Columns[1].EntireColumn.AutoFit;//セル幅の調整
WorkBook.SaveAs( Filename:='c:\poo.xls');
WorkBook.close;
WorkBook:=unAssigned;
WorkSheet:=unAssigned;
Excel.Quit;
Excel:=unAssigned;
end;
//開く
procedure TForm1.Button2Click(Sender: TObject);
var
Excel: Variant;
WorkBook: Variant;
WorkSheet: Variant;
begin
//仮に三枚のシートを使うと仮定
Excel := CreateOleObject('Excel.Application');
WorkBook := Excel.Workbooks.Add;
{もしExcelを表示させたいなら Excel.Visible:=True;}
WorkBook :=Excel.WorkBooks.Open(FileName := 'c:\poo.xls', readOnly := False);
WorkSheet := WorkBook.WorkSheets[1];
WorkSheet.name:='woo';
WorkSheet.Cells[1,1].Value:='woopooofooo';
WorkSheet.Columns[1].EntireColumn.AutoFit;
WorkBook.Save;//上書き保存。別名ならsaveAs( Filename:='c:\poo.xls');
WorkBook.close;
WorkBook:=unAssigned;
WorkSheet:=unAssigned;
Excel.Quit;
Excel:=unAssigned;
end;
長いのですべては載せませんが、Classを作成しました。
これを利用すると解消できると思います。
もし欲しい場合は直接メール下さい。
添付して送ります。
type
TWorkSheet = class
private
E_WorkSheet: Variant;
E_Charts : Variant;
FFormat : string;
FColor : TColor;
FPattern : TPattern;
FFont : TFont;
FAlign : TAlign;
FCol,FRow: integer;
function GetCells(ACol, ARow: Integer): Variant;
function GetName:string;
function GetColor:TColor;
function GetPattern:TPattern;
function GetFont:TFont;
function GetAlign:TAlign;
procedure SetCells(ACol, ARow: Integer; const Value: Variant);
procedure SetName(NewName:string);
procedure SetFormat(NewFormat:string);
procedure SetColor(Color:TColor);
procedure SetPattern(SelPattern:TPattern);
procedure SetFont(Font:TFont);
procedure SetAlign(Align:TAlign);
function Alpha(I: integer):string;
function ExcelRange(Rect:TRect):string;
public
constructor Create(WorkSheet:Variant);
destructor Destroy;override;
procedure AutoFit( ColCount:integer );
procedure Activate;
procedure PrintPreview;
procedure SetHeader( strL,strC,strR:string );
procedure SetFooter( strL,strC,strR:string );
procedure GridToExcel( SG:TStringGrid );
procedure ListToExcel( LV:TListView );
procedure SetStyleCell(Col,Row:integer);
procedure SetStyleRect(Rect:TRect);
procedure MakeTableTemplate(ColumnRect,ItemRect:TRect; LineWidth:integer);
procedure MakeGraph(Rect:TRect ;GraphKind:integer ;HasLegend:Boolean ;Title:string);
property Cells[ACol, ARow: Integer]: Variant read GetCells write SetCells;
property Name :string read GetName write SetName;
property Format :string write SetFormat;
property Color :TColor read GetColor write SetColor;
property Pattern :TPattern read GetPattern write SetPattern;
property Font :TFont read GetFont write SetFont;
property Align :TAlign read GetAlign write SetAlign;
end;
type
TExcel = class
private
E_Excel : Variant;
E_Application : Variant;
E_WorkBook : Variant;
E_Error_Msg : string;
public
WorkSheets : array of TWorkSheet; //すべてのSheetを配列保持
//ActiveSheet : TWorkSheet; //アクティブなSheetを保持
constructor Create(FileName:string='');
destructor Destroy;override;
procedure ExcelClose;
procedure SaveAs(FileName:string;FileFormatType:integer); //
function GetSheetsCount:integer;
// ErrorMessage参照の為のproperty
property ErrorMsg : string read E_Error_Msg;
end;
aikoさん、kazuさん、Lupin3rdさん こんにちは、ToshiqnZです。
済みませんがお邪魔します。
毎日掲示板を見てます。今取り組んでいるプログラムで今後エクセ
ルへのデータの受け渡しをしたいのですが、方法がまだ判っていな
いです。Lupin3rd さんのエクセル操作クラスですが、私にも頂け
ないでしょうか。もし宜しければメールさせていただきます。
kazuさん、Lupin3rdさん、ありがとうございます。
お返事遅くなりまして、ごめんなさい。
結局delphiからエクセルを操作するよりも、
エクセルから保存したデータを読み込んだほうがいいということになりましたために、その作業におわれていました。
コンポーネントを使ってやる場合には、どのコンポーネントを使いどのようにやるのでしょう?例えばdelphiからエクセルの起動する方法など
今回は関係ないのですが今後絶対に必要になってきそうなので、よろしくお願いします。