全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:7105
推到 Plurk!
推到 Facebook!

Excel 如何使用 SaveAs 後不會自動開啟!!

缺席
goolddag0821
一般會員


發表:10
回覆:10
積分:3
註冊:2007-07-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-01-08 15:02:36 IP:219.87.xxx.xxx 訂閱
Dear 大大

我寫了一段code 如下!!


[code delphi]
procedure TForm1.MailFile();
var
aSheet : Variant;
sTemplate : String;
begin
GetDir(0,sTemplate);
sPath:=sTemplate '\MACHINE.XLS'; //讀進來Excel 規格檔路徑!!

ExcelApp.Visible[0] := true;
ExcelApp.Workbooks.open(sPath,0, false, 5, '*', '*', true, xlwindows, 6, false, true, null, false, 0);
aSheet:= ExcelApp.Worksheets.item[1]; //第K個工作表

aSheet.Cells[2,8] := 'AAA';
aSheet.Cells[3,2] := 'BBB';
aSheet.Cells[3,4] := 'CCC';

aSheet.SaveAs('c:\aaa.xls');

[/code]

儲存都沒有問題,但是每次都會自動開啟Excel 檔案
有沒有別的儲存方式可以自動存檔不會開啟 Excel 檔案!!!
ThANKS~
編輯記錄
taishyang 重新編輯於 2009-01-08 15:18:53, 註解 歸類成[問題]‧
Coffee
版主


發表:31
回覆:878
積分:561
註冊:2006-11-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-01-08 15:42:54 IP:59.124.xxx.xxx 訂閱
請說明缺席原因

===================引 用 goolddag0821 文 章===================
Dear 大大

我寫了一段code 如下!!


[code delphi]
procedure TForm1.MailFile();
var
aSheet : Variant;
sTemplate : String;
begin
GetDir(0,sTemplate);
sPath:=sTemplate '\MACHINE.XLS'; //讀進來Excel 規格檔路徑!!

ExcelApp.Visible[0] := true;
ExcelApp.Workbooks.open(sPath,0, false, 5, '*', '*', true, xlwindows, 6, false, true, null, false, 0);
aSheet:= ExcelApp.Worksheets.item[1]; //第K個工作表

aSheet.Cells[2,8] := 'AAA';
aSheet.Cells[3,2] := 'BBB';
aSheet.Cells[3,4] := 'CCC';

aSheet.SaveAs('c:\aaa.xls');

[/code]

儲存都沒有問題,但是每次都會自動開啟Excel 檔案
有沒有別的儲存方式可以自動存檔不會開啟 Excel 檔案!!!
ThANKS~
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-01-09 14:07:42 IP:59.125.xxx.xxx 未訂閱
Ref: http://www.swissdelphicenter.ch/torry/showcode.php?id=156

[code delphi]
unit EXCEL_OLE;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComOBJ, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ExcelApp: OleVariant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
// SheetType
xlChart = -4109;
xlWorksheet = -4167;
// WBATemplate
xlWBATWorksheet = -4167;
xlWBATChart = -4109;
// Page Setup
xlPortrait = 1;
xlLandscape = 2;
xlPaperA4 = 9;
// 98.1.9
// Format Cells
xlBottom = -4107;
xlLeft = -4131;
xlRight = -4152;
xlTop = -4160;
// Text Alignment
xlHAlignCenter = -4108;
xlVAlignCenter = -4108;
// Cell Borders
xlThick = 4;
xlThin = 2;
var
ColumnRange: OleVariant;
// Function to get the number of Rows in a Certain column
function GetLastLine(AColumn: Integer): Integer;
const
xlUp = 3;
begin
Result := ExcelApp.Range[Char(96 AColumn) IntToStr(65536)].end[xlUp].Rows.Row;
end;
begin
{ Start Excel }
// By using GetActiveOleObject, you use an instance of Word that's already running,
// if there is one.
//try
// ExcelApp := GetActiveOleObject('Excel.Application');
//except
try
// If no instance of Word is running, try to Create a new Excel Object
ExcelApp := CreateOleObject('Excel.Application');
except
ShowMessage('Cannot start Excel/Excel not installed ?');
Exit;
end;
//end;
// Add a new Workbook, Neue Arbeitsmappe öffnen
ExcelApp.Workbooks.Add(xlWBatWorkSheet);
// Open a Workbook, Arbeitsmappe öffnen YourFileName
ExcelApp.Workbooks.Open('c:\DELPHI\DB\TST.xls');

// Rename the active Sheet
ExcelApp.ActiveSheet.Name := 'This is Sheet 1';
// Rename
ExcelApp.Workbooks[1].WorkSheets[1].Name := 'This is Sheet 1';
// Insert some Text in some Cells[Row,Col]
ExcelApp.Cells[1, 1].Value :='SwissDelphiCenter.ch';//'http://www.swissdelphicenter.ch'
ExcelApp.Cells[1, 2].Value :='http://www.swissdelphicenter.ch/torry/showcode.php?id=156';
ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);
// Setting a row of data with one call
ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);
// Setting a formula
ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';
// Change Cell Alignement
ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;
// Change the Column Width.
ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;
ColumnRange.Columns[1].ColumnWidth := 20;
ColumnRange.Columns[2].ColumnWidth := 40;
// Change Rowheight / Zeilenhöhe ändern:
ExcelApp.Rows[1].RowHeight := 15.75;
// Merge cells, Zellen verbinden:
ExcelApp.Range['B3:D3'].Mergecells := True;
// Apply borders to cells, Zellen umrahmen:
ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; // Think line/ Dicke Linie
ExcelApp.Range['A14:M14'].Borders.Weight := xlThin; // Thin line Dünne Linie
// Set Bold Font in cells, Fettdruck in den Zellen
ExcelApp.Range['B16:M26'].Font.Bold := True;
// Set Font Size, Schriftgröße setzen
ExcelApp.Range['B16:M26'].Font.Size := 12;
//right-aligned Text, rechtsbündige Textausrichtung
ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;
// horizontal-aligned text, horizontale Zentrierung
ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;
// left-aligned Text, vertikale Zentrierung
ExcelApp.Range['B14:M26'].VerticalAlignment := xlVAlignCenter;

{ Page Setup }
ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;
// Left, Right Margin (Seitenränder)
ExcelApp.ActiveSheet.PageSetup.LeftMargin := 35;
ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;
// Set Footer Margin
ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);
// Fit to X page(s) wide by Y tall
ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1; // Y
ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y
// Zoom
ExcelApp.ActiveSheet.PageSetup.Zoom := 95;
// Method 'PageSetup' not supported by automation object
// Set Paper Size: PaperSize //.PaperSize = xlPaperLetter
//ExcelApp.PageSetup.PaperSize:=1;
//ExcelApp.PageSetup.PaperSize := xlPaperA4;
// Show/Hide Gridlines:
ExcelApp.ActiveWindow.DisplayGridlines := False;
// Set Black & White
ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;
// footers
ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte Fuzeile';
ExcelApp.ActiveSheet.PageSetup.LeftFooter := 'Left Footer / Linke Fuzeile';
// Show Excel Version:
ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));
// Show Excel:
//ExcelApp.Visible := True;
// Save the Workbook
//ExcelApp.SaveAs('c:\filename.xls');
//ExcelApp.Save;
// Save the active Workbook:
ExcelApp.ActiveWorkBook.SaveAs('c:\DELPHI\OLE\TstExcel.xls');
//ExcelApp.ActiveWorkBook.Save;
ExcelApp.DisplayAlerts := False;
ExcelApp.Quit
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Quit Excel
if not VarIsEmpty(ExcelApp) then
begin
ExcelApp.DisplayAlerts := False; // Discard unsaved files....
ExcelApp.Quit;
end;
end;
end.
[/code]
------
What do we live for if not to make life less difficult for each other?
系統時間:2024-04-27 8:38:37
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!