如何使用XlsReadWriteII在Delphi中讀取Excel文件
XLSReadWriteII v5.20.01a for Delphi XE5 x32下載地址:
http://download.csdn.net/detail/wozengcong/6878409
XLSReadWriteII v5.10.25 Cracked for XE2-XE4 (Win32)下載地址:
http://download.csdn.net/detail/wozengcong/6886915
XLSReadWriteII_v5.20.25〖D7,XE-XE6〗下載地址:
http://download.csdn.net/detail/wozengcong/7689003
安裝步驟:
1、在Embarcadero RAD Studio XE2主菜單中依次點擊"Component->Install Packages"添加(Add)XLSRWII5_DXE版本號.bpl
2、在Embarcadero RAD Studio XE4主菜單中依次點擊“Toosl->Options->Delphi Options->Library->Library Path”分別添加Library Path:Obj和XLSReadWriteII的完整路徑
安裝步驟:
1、在Embarcadero RAD Studio XE2主菜單中依次點擊"Component->Install Packages"添加(Add)XLSRWII5_DXE2.bpl
2、在Embarcadero RAD Studio XE4主菜單中依次點擊“Toosl->Options->Delphi Options->Library->Library Path”分別添加Library Path:
Obj和XLSReadWriteII的完整路徑
在Delphi中讀取Excel文件,使用CreateOleObject的方式挺討厭的,一直搞不定,輸出了文件之後,總會在系統中打開一個Excel,就算Quit也不行,一個程序中使用的多了,還不定搞出什麼事情來。狠狠心找個其它的東西來代替,於是發現了XlsReadWriteII。
使用之後發現這個東西真不錯,簡單好用。不管是讀還是寫均輕松搞定。下面是官方的Demo中的代碼。
寫文件代碼,包括對格式的定制:
[delphi] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
XLS.Filename := 'FormatSample.xls';
XLS.Clear;
// Add format #0
with XLS.Formats.Add do begin
FontIndex := XLS.Fonts.AddIndex;
XLS.Fonts[FontIndex].Name := 'Courier new';
XLS.Fonts[FontIndex].Size := 14;
XLS.Fonts[FontIndex].Color := xcRed;
end;
// Add format #1
with XLS.Formats.Add do begin
FontIndex := XLS.Fonts.AddIndex;
XLS.Fonts[FontIndex].AssignTFont(Font);
end;
// Add format #2
with XLS.Formats.Add do begin
FillPatternForeColor := xcLilac;
end;
// Add format #3
with XLS.Formats.Add do begin
BorderTopColor := xcBlue;
BorderBottomColor := xcBlue;
BorderTopStyle := cbsThin;
BorderBottomStyle := cbsThick;
end;
// Add format #4
// ShortDateFormat is a Delphi global variable for the local date format.
with XLS.Formats.Add do begin
NumberFormat := ShortDateFormat;
end;
XLS.Sheets[0].WriteString(1,2,0,'Format #0');
XLS.Sheets[0].WriteString(1,3,1,'Format #1');
XLS.Sheets[0].WriteString(1,4,2,'Format #2');
XLS.Sheets[0].WriteString(1,5,3,'Format #3');
XLS.Sheets[0].WriteNumber(1,6,4,Date);
XLS.Write; //如果需要另存為一個文件,改FileName之後再寫一次,如需更改工作簿的名稱,設置Sheets[0].Name就可以了。
讀取文件代碼:
[delphi] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
var
Col,Row: integer;
Sum: double;
begin
if edFilename.Text = '' then begin
ShowMessage('Filename is missing');
Exit;
end;
Sum := 0;
XLS.Filename := edFilename.Text;
XLS.Read; //此行不能省,否則讀取不到數據
XLS.Sheets[0].LastCol := 255;
XLS.Sheets[0].LastRow := 65535;
for Col := XLS.Sheets[0].FirstCol to XLS.Sheets[0].LastCol do begin
for Row := XLS.Sheets[0].FirstRow to XLS.Sheets[0].LastRow do begin
if (Row < Grid.RowCount) and (Col < Grid.ColCount) then
Grid.Cells[Col + 1,Row + 1] := XLS.Sheets[0].AsFmtString[Col,Row];
if XLS.Sheets[0].CellType[Col,Row] = ctFloat then
Sum := Sum + XLS.Sheets[0].AsFloat[Col,Row];
end;
end;
lblSum.Caption := Format('The sum of all numeric cells are: %.2f',[Sum]);
Grid.Invalidate;
end;
溫馨提示:
XE5報錯解決方案:
1、[dcc32 Error] Unit1.pas(51): E2003 Undeclared identifier: 'ctFloat'
[delphi] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
uses XLSSheetData5, XLSReadWriteII5, Xc12Utils5;//增加引用:Xc12Utils5
ctFloat修改為xctFloat
2、[dcc32 Error] Unit1.pas(42): E2064 Left side cannot be assigned to
[delphi] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
XLS.Sheets[0].LastCol := 255;
XLS.Sheets[0].LastRow := 65535;
刪除或者注釋掉即可
3、別名說明:
XLS是XLSReadWriteII5組件的Name,Grid是StringGrid組件的Name