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

Delphi 7 讀取Excel內容,然後以此內容作為word的檔案名稱

答題得分者是:christie
aAlan
一般會員


發表:38
回覆:24
積分:12
註冊:2004-07-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-07 10:29:23 IP:163.19.xxx.xxx 訂閱
現製作好一word 2003檔案,要將之複製100份,每一份的檔案名稱假設是 "班級+姓名.doc"
現在,檔案名稱都已經存在一個excel 2003檔案內了!
excel有以下欄位:
sheet1: A班級 姓名
sheet2: B班級 姓名
sheet3: C班級 姓名
要如何透過delphi 7讀取該excel 2003面的內容,然後以所讀取的excel內容作為
word 2003 複製100份的檔案名稱呢?
可否幫幫忙可否提示一下?
有一年沒碰delphi 7了 ,不知道應如何下手....
謝謝! 真的謝謝...
Coffee
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-07 11:24:30 IP:220.130.xxx.xxx 訂閱
STFW (delphi.ktop.com.tw)Yourself.
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
編輯記錄
Coffee 重新編輯於 2007-11-07 11:25:00, 註解 無‧
christie
資深會員


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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-11-07 11:59:09 IP:203.73.xxx.xxx 未訂閱
after STFW delphi.ktop
//請常用請支持請愛用DELPHI.

[code delphi]
uses ComObj;
...
function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
// Create Excel-OLE Object
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Open the Workbook
XLApp.Workbooks.Open(AXLSFile);
// Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
// In order to know the dimension of the WorkSheet, i.e the number of rows
// and the number of columns, we activate the last non-empty cell of it
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
// Get the value of the last row
x := XLApp.ActiveCell.Row;
// Get the value of the last column
y := XLApp.ActiveCell.Column;
// Set Stringgrid's row &col dimensions.
AGrid.RowCount := x;
AGrid.ColCount := y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
// Define the loop for filling in the TStringGrid
k := 1;//1
repeat
for r := 1 to y do
AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
Inc(k, 1);
AGrid.RowCount := k 1;
until k > x;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
// XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;
...
//Add a Button
//Add a StringGrid
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
if Xls_To_StringGrid(StringGrid1,'D:\ListAll.xls') then
ShowMessage('Table has been exported!');
for i:=0 to StringGrid1.RowCount-1 do
begin
// 班級<>'' and 姓名<>''
if(StringGrid1.Cells[0,i]<>'')and(StringGrid1.Cells[1,i]<>'') then
copyfile(pchar('MyDOC.DOC'),pchar(format('%s-%s.DOC',[trim(StringGrid1.Cells[0,i]),trim(StringGrid1.Cells[1,i])])), False);

end;
end;

[/code]
------
What do we live for if not to make life less difficult for each other?
系統時間:2024-04-20 4:53:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!