掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
エクセルファイルのシートを別ファイルに形式を指定してコピーするには? (ID:12888)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
以下にソースを記載します。 ------------------------------- private { Private 宣言 } Excel : Variant; NewExcel : Variant; FromBook : Variant; ToBook : Variant; FromSheet : Variant; ToSheet : Variant; ------------------------------- ・・・ ------------------------------------------------------------------------------------- procedure TForm1.FormShow(Sender: TObject); begin try Excel := CreateOleObject('Excel.Application'); Excel.DisplayAlerts := False ; Excel.Visible := false; NewExcel := GetActiveOleObject('Excel.Application'); NewExcel.DisplayAlerts := False ; NewExcel.Visible := false; except //Excelがインストールされてなかった場合の終了処理 on EOleSysError do begin showmessage('Excelがインストールされていないと使えません'); WindowState := wsMinimized; Application.Terminate(); Exit; end; end; end; ------------------------------------------------------------------------------------- ・・・ ------------------------------------------------------------------------------------- {コピー開始実行時処理} procedure TForm1.Button2Click(Sender: TObject); var sFileName : string; sFileDir : string; Aftertmp : Variant; sPath : string; sSaveFile : string; begin sPath := 'D:\Before.xls'; try //コピー元のファイルの読み込み FromBook := Excel.WorkBooks.Open(FileName := sPath, readOnly := False); FromSheet := FromBook.WorkSheets[2]; //コピー先のファイルの新規作成 ToBook := NewExcel.WorkBooks.Add; ToSheet := ToBook.WorkSheets[1]; Aftertmp := ToSheet; //シートのコピー FromBook.WorkSheets[2].Copy(After := Aftertmp); sSaveFile := 'D:\after.xls'; if FileExists(sSaveFile) then DeleteFile(sSaveFile); ToBook.SaveAs(sSaveFile); finally ToSheet.Columns[2].EntireColumn.AutoFit; ToSheet:=unAssigned; ToBook.close; ToBook:=unAssigned; FromSheet.Columns[2].EntireColumn.AutoFit; FromSheet:=unAssigned; FromBook.close; FromBook:=unAssigned; Excel.Quit; Excel:=unAssigned; NewExcel.Quit; NewExcel:=unAssigned; end; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.