这是自带的一个例子,看懂这一点东西,基本的操作应该没问题了….
unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, XLSReadWriteII4, XLSFonts4, CellFormats4, BIFFRecsII4;

type
TfrmMain = class(TForm)
Label1: TLabel;
Button1: TButton;
edFilename: TEdit;
Button2: TButton;
Button3: TButton;
dlgSave: TSaveDialog;
XLS: TXLSReadWriteII4;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
procedure AddFormats;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.AddFormats;
begin
(Format a single cell)
(* 显示字符串 )
XLS.Sheets[0].AsString[0,1] := ‘Cell 1’;
(
字符串颜色 )
XLS.Sheets[0].Cell[0,1].FillPatternForeColor := xcYellow;
(
字体 *)
XLS.Sheets[0].Cell[0,1].FontStyle := [xfsBold,xfsItalic];

(Format a number cell (3 decimals and thousand separator)
(
格式化字符串,显示小数点后三位,千位和百位之间加个空格 *)
XLS.Sheets[0].AsFloat[0,0] := 12335985394.895634;
XLS.Sheets[0].Cell[0,0].NumberFormat := ‘# ##0.000’;

(* Write a string cell. )
XLS.Sheet[0].AsStringRef[‘C2’] := ‘Hello’;
(
单元格赋值 )
(
Set the font size of the cells in the area.
(* 改变区域内的字体大小 )
XLS.Sheet[0].Range.Items[1,0,3,3].FontSize := 14;
(
Set the color of the cells. )
(
设置区域的颜色 )
XLS.Sheet[0].Range.ItemsRef[‘B1:D4’].FillPatternForeColor := xcYellow;
(
Set a outline border. )
(
设置外框线的外形、颜色 )
XLS.Sheet[0].Range.ItemsRef[‘B1:D4’].BorderOutlineStyle := cbsThick;
(
Set color of the outline border. )
XLS.Sheet[0].Range.ItemsRef[‘B1:D4’].BorderOutlineColor := xcRed;
(
Make a copy of the cells. )
(
区域复制 )
XLS.Sheet[0].Range.ItemsRef[‘B1:D4’].Copy(8,10);
(
Move the cells. )
(
区域移动 *)
XLS.Sheet[0].Range.ItemsRef[‘B1:D4’].Move(8,2);
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
AddFormats;
XLS.Filename := edFilename.Text;
(* 保存文件 *)
XLS.Write;
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
dlgSave.FileName := edFilename.Text;
if dlgSave.Execute then
edFilename.Text := dlgSave.FileName;
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
Close;
end;

end.

/////////////////////////////////////////////////////////////////////////////////////
Delphi中使用XlsReadWriteII
在Delphi中读取Excel文件,使用CreateOleObject的方式挺讨厌的,一直搞不定,输出了文件之后,总会在系统中打开一个Excel,就算Quit也不行,一个程序中使用的多了,还不定搞出什么事情来。狠狠心找个其它的东西来代替,于是发现了XlsReadWriteII。
使用之后发现这个东西真不错,简单好用。不管是读还是写均轻松搞定。下面是Demo中的代码。
写文件代码,包括对格式的定制:
XLS.Filename := ‘FormatSample.xls’;
XLS.Clear;
// Add format #0//先添加格式即ForMat:控件有一个默认的格式,序号为0,以后每添加一个,序号就加1
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就可以了。
读取文件代码:
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;
/////////////////////////////////////////////////////////////////////////

XLsReadWrite 制作Excel时控制列的的格式
分类: delphi2011-08-06 14:35116人阅读评论(0)收藏举报
最近研究下XlsReadWrite控件的使用。
其中,学会了在制作Excel时如何控制一整列的格式,如文本格式
先添加格式即ForMat:控件有一个默认的格式,序号为0,以后每添加一个,序号就加1
with XLS.Formats.Add do begin
FormatOptions := [foWrapText];
HorizAlignment := chaCenter;//对齐方式
NumberFormat := ‘@’;//主要设置该属性的值
VertAlignment := cvaCenter;
end;
然后添加当前Sheet中列的格式,语句如下:
with XLS.Sheets[0].ColumnFormats.Add do
begin
Col1:=2;//起始列的位置
Col2:=3;//结束列的位置
FormatIndex:=4;//ForMat对应的序号
end;
///////////////////////////////////////////////////////////////

用XlsReadWrite控件的时候怎样合并单元格?
我查到了,XLSReadWriteII.Sheets[0].MergedCells.Add(Col1,Row1,Col2,Row2);
/////////////////////////////////////////////////////////////////////////////////

Delphi编程保存数据到Excel文件(3):使用XLSReadWriteII控件
今天采用XLSReadWriteII控件来完成10×10的乘法表,发现它很灵活,实现的功能比较多,基本能满足所以的需求。
代码片断如下:

procedure TForm1.Button1Click(Sender: TObject);
Var
i,j:Integer;
begin
XLS.Sheets[0].Name:= ‘Ten times table(乘法表)’; //给工作表命名
//General settings 常规的格式设置
With XLS.Sheet[0].Range.ItemsRef[‘B2:M14’] do begin
FontSize := 12;
FontStyle:= [xfsBold];
FillPatternForeColor := TExcelColor(45);
HorizAlignment:=chaCenter;
end;
//Title 标题
With XLS.Sheet[0].Range.ItemsRef[‘B2:M2’] do begin
Merged:=true;
HorizAlignment:=chaCenter;
FontSize := 14;
end;
XLS.Sheet[0].AsStringRef[‘B2’] := ‘Ten Times Table(乘法表)’;
//columns header 列头
With XLS.Sheet[0].Range.ItemsRef[‘C3:M3’] do begin
FillPatternForeColor := TExcelColor(53); //Interior.ColorIndex := 53;
FontColor := xcWhite;
end;
//rows header 行头
With XLS.Sheet[0].Range.ItemsRef[‘B4:B14’] do begin
FillPatternForeColor := TExcelColor(53);
FontColor := xcWhite;
end;
//Table values 表内值
for j := 0 to 10 do begin //循环各列
//column header 列头 (先列后行 基于0开始)
XLS.Sheets[0].AsInteger[2+j, 2] := j;
//row header 行头 (先列后行 基于0开始)
XLS.Sheets[0].AsInteger[1,3+j]:= j;
//time table 表内值
for i := 0 to 10 do begin //循环各行
XLS.Sheets[0].AsInteger[2+j,3+i]:= i * j;
end;
end;
XLS.Filename := ‘TenTimesTable.xls’; //保存到文件
XLS.Write;
MessageDlg(‘TenTimesTable.xls is created’, mtInformation, [mbOk], 0);
end;
/////////////////////////////////////////////////////////////////////////////////////////////

使用NativeExcel2控件
这个控件非常好,非常的简捷,尤其是Range对象操作使用起来非常方便,值得推荐。
代码片断如下:
procedure TForm1.Button1Click(Sender: TObject);
Var Book: IXLSWorkbook; //申明工作薄
ws: IXLSWorksheet; //申明工作表
i, j: integer;
begin
//Create workbook 创建工作薄
Book := TXLSWorkbook.Create;
//Add new sheet 在工作薄中加一个新的工作表
ws := Book.Sheets.Add;
ws.Name := ‘Ten times table(乘法表)’; //给工作表命名
//General settings 常规的格式设置
With ws.Range[‘B2’, ‘M14’] do begin
Font.Size := 12;
Font.Bold := true;
ColumnWidth := 5.71;
Interior.ColorIndex := 45;
Borders.ColorIndex := xlColorIndexAutomatic;
HorizontalAlignment := xlHAlignCenter;
end;
//Title 标题
With ws.Range[‘B2’, ‘M2’] do begin
Merge(false);
Font.Size := 14;
Value := ‘Ten Times Table(乘法表)’;
end;
//columns header 列头
With ws.Range[‘C3’, ‘M3’] do begin
Interior.ColorIndex := 53;
Font.Color := clWhite;
end;
//rows header 行头
With ws.Range[‘B4’, ‘B14’] do begin
Interior.ColorIndex := 53;
Font.Color := clWhite;
end;
//Table values 表内值
for i := 0 to 10 do begin
//column header 列头
ws.Cells.Item[3, i + 3].Value := i;
//row header 行头
ws.Cells.Item[i + 4, 2].Value := i;
//time table 表内值
for j := 0 to 10 do begin //先行后列
ws.Range[‘C4’, ‘M14’].Item[i + 1, j + 1].Value := i * j;
end;
end;
Book.SaveAs(‘TenTimesTable.xls’); //保存到文件
MessageDlg(‘TenTimesTable.xls is created’, mtInformation, [mbOk], 0);
end;
/////////////////////////////////////////////////////////////////////
下面附带了我写的一个DBGridToExcel控件,功能是把DBGrid中的数据导出到Excel中。大家可以学习一下,做为参考。为个控件中包括了使用属性,方法和我们将在下一篇中讲到的“事件”。

附1: 安装自制控件的方法:
(1).在Component菜单中,选择”Install Component…”.
(2).在Unit File name 后面单击“…”,选择”*.pas”控件的单元文件,再点击OK。在出现的窗口中单击”install”,即安装完毕。
新装的控件即出现在你的面板中。

附2: TDBGridToExcel控件的全部源码,把它拷贝到记事本中,存为.pas文件即可。
unit DBGridToExcel;

{}
{* }
{
Export Grid To Word VCL Control for D5 & D6 }
{
Copyright(C) xiangding 2003.10.1 All rights reserved }
{
Bug Report: boyxd@163.net }
{
Author : 小熊 }
{
*}
{
}
{* }
{
This is a Simple Version }
{
}
{***********************************************************************}
{
}
{
Install: }
{
Please Save as file GridToExcel.pas then open the file }
{
Click the menu item [Component] –> [Install Component] }
{
Click [Install] button in the Install Component dialog }
{
after install ,you can find the control at component }
{
page [sample] }
{
}
{***********************************************************************}
{
}
{
安装: }
{
把附件保存,然后用Delphi打开这个GridToExcel.Pas文件, }
{
选择Delphi菜单–〉Component–>Install Component, }
{
然后选择Install即可。安装之后,在控件面板的Samples页面上面, }
{
熟悉之后,你可以试着设置一些复杂的属性,其他的自己摸索吧, *}
{***********************************************************************}
interface

uses
Windows, StdCtrls, ComCtrls, Messages, DBGrids, Graphics, ExtCtrls,
Forms, DB, ComObj, Controls, SysUtils, Classes;

ResourceString
SPromptExport = ‘请等待,正在导出数据……’;
SConnectExcel = ‘正在启动Excel,请稍候……’;
SConnectExcelError= ‘连接Excel失败,可能没有安装Excel,无法导出.’;
SCancel = ‘取消(&C)’;
SError = ‘错误’;
SConfirm = ‘真的要终止数据的导出吗?’;
SCaption = ‘确认’;
SGridError = ‘没有指定数据集,请指定数据集控件!’;

type
TDBGridToExcel = class(TComponent)
private
ProgressForm: TForm;
FShowProgress: Boolean;
ExcelApp : Variant;
FTitle: String;
Quit: Boolean;
FOnProgress: TNotifyEvent;
FGrid: TDBGrid; {The Source Grid}
ProgressBar: TProgressBar;
Prompt: TLabel;
FAutoExit: Boolean;
FAutoSize: Boolean;
FDBGrid: TDBGrid;
procedure SetShowProgress(const Value: Boolean);
procedure CreateProgressForm;
procedure ButtonClick(Sender: TObject);
Function ConnectToExcel: Boolean;
procedure ExportDBGrid;
{ Private declarations }
protected
{ Protected declarations }
public
Constructor Create(AOwner: TComponent); override;
Destructor Destroy(); override;
Procedure ExportToExcel; {Export Grid To Excel}
{ Public declarations }
published
{ Published declarations }
property DBGrid: TDBGrid read FDBGrid write FDBGrid;
property Title: String read FTitle write FTitle;
property ShowProgress: Boolean read FShowProgress write SetShowProgress;

property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents(‘Samples’, [TDBGridToExcel]);
end;

{ TDBGridToExcel }

procedure TDBGridToExcel.ButtonClick(Sender: TObject);
begin
Quit := MessageBox(ProgressForm.Handle, pchar(SConfirm), pchar(SCaption),
MB_OKCANCEL + MB_ICONINFORMATION) = IDOK;
end;

function TDBGridToExcel.ConnectToExcel: Boolean;
begin
Result := true;
Try
ExcelApp := CreateOleObject(‘Excel.Application’);
ExcelApp.Visible := False;
if Title<>” then ExcelApp.Caption := Title;
ExcelApp.WorkBooks.Add;
except
MessageBox(GetActiveWindow,PChar(SConnectExcelError),PChar(SError),Mb_OK+MB_IconError);
result := false;
end;
end;

constructor TDBGridToExcel.Create(AOwner: TComponent);
begin
inherited;
FShowProgress := True; {Default value was Show the Progress}
FAutoExit := False;
FAutoSize := True;
end;

procedure TDBGridToExcel.CreateProgressForm;
var
Panel : TPanel;
Button : TButton;
begin
if Assigned(ProgressForm) then exit; {Aready Create?}

ProgressForm := TForm.Create(Owner);
With ProgressForm do
begin
Font.Name := ‘宋体’;
Font.Size := 10;
BorderStyle := bsNone;
Width := 280;
Height := 120;
BorderWidth := 1;
Color := clBackground;
Position := poOwnerFormCenter;
end;

Panel := TPanel.Create(ProgressForm);
with Panel do { Create Panel }
begin
Parent := ProgressForm;
Align := alClient;
BevelInner := bvNone;
BevelOuter := bvRaised;
Caption := ”;
end;

Prompt := TLabel.Create(Panel);
with Prompt do { Create Label }
begin
Parent := Panel;
Left := 20;
Top := 25;
Caption := SConnectExcel;
end;

ProgressBar := TProgressBar.Create(panel);
with ProgressBar do { Create ProgressBar }
begin
Step := 1;
Parent := Panel;
Smooth := true;
Left := 20;
Top := 50;
Height := 18;
Width := 260;
end;

Button := TButton.Create(Panel);
with Button do { Create Cancel Button }
begin
Parent := Panel;
Left := 115;
Top := 80;
Caption := SCancel;
OnClick := ButtonClick;
end;

ProgressForm.Show;
ProgressForm.Update;
end;

destructor TDBGridToExcel.Destroy;
begin

inherited;
end;

procedure TDBGridToExcel.ExportDBGrid;
var
Data : TDataSet;
ADBGrid: TDBGrid;
i, j : integer;
CurrentPoint : Pointer;
OldBeforeScroll, OldAfterScroll: TDataSetNotifyEvent;
begin
Screen.Cursor := crHourGlass;
try
try
TForm(Owner).Enabled := False;
ExcelApp.DisplayAlerts := false;
ExcelApp.ScreenUpdating := false;
Quit := false;

if ShowProgress then Prompt.Caption := SPromptExport;
ADBGrid := DBGrid;
Data := ADBGrid.DataSource.DataSet;
with ADBGrid do { Insert Table Header }
for i := 1 to Columns.Count do
if Columns[i – 1].Visible then
ExcelApp.Cells[1,i].Value :=Columns[i – 1].Title.Caption;

CurrentPoint := Data.GetBookmark; {Save Current Position}
OldBeforeScroll := Data.BeforeScroll; { Save Old Before Scroll Event handle }
OldAfterScroll := Data.AfterScroll; { Save Old After Scroll Event Handle }
Data.DisableControls; { Disable Control }
Data.BeforeScroll := nil;
Data.AfterScroll := nil;

if ShowProgress then ProgressBar.Max := Data.RecordCount;
i := 2;
Data.First;
while not Data.Eof do { Process All record }
begin
with ADBGrid do { Process one record }
for j := 1 to Columns.Count do
if Columns[j – 1].Visible then
ExcelApp.Cells[i,j].Value := Columns[j – 1].Field.DisplayText;
Inc(i);
Data.Next;
if Assigned(FOnProgress) then FOnProgress(Self);
if ShowProgress then { Update Progress UI }
begin
ProgressBar.StepIt;
Application.ProcessMessages;
if Quit then exit;
end;
end;
except
MessageBox(GetActiveWindow,PChar(SConnectExcelError),Pchar(SError),MB_OK+MB_ICONERROR);
end;
ExcelApp.Visible := False;
TForm(Owner).Enabled := True;
Screen.Cursor := crDefault;
if ShowProgress then FreeAndNil(ProgressForm); { Free Progress Form }
ExcelApp.DisplayAlerts := True;
ExcelApp.ScreenUpdating := True;
finally
Data.BeforeScroll := OldBeforeScroll; { Restore Old Event Handle }
Data.AfterScroll := OldAfterScroll;
Data.GotoBookmark(CurrentPoint);
Data.FreeBookmark(CurrentPoint);
Data.EnableControls;
Screen.Cursor := crDefault;
end;
end;

procedure TDBGridToExcel.ExportToExcel;
begin
if DBGrid= nil then raise Exception.Create(SGridError); {No DataSource, then Error}
if ShowProgress then CreateProgressForm; {Whether or not Show the ProgressForm}
if not ConnectToExcel then { Exit when error occer }
begin
if ShowProgress then FreeAndNil(ProgressForm); {release form}
exit;
end;
ExportDBGrid; {begin Export Data}
end;

procedure TDBGridToExcel.SetShowProgress(const Value: Boolean);
begin
FShowProgress := Value;
end;
end.

////////////////////////////////////////////////////////////////////////////

转载一个:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1754027

{ 背景:今天要把DataSet导入Excel,查询以前的贴子,一部分用Ole,速度太慢,
一部分用ADO连接到Excel文件,也很慢,一气之下把DBGrigEh的导出部分改了出来,
欢迎大家指教、改进。
功能:将数据集的数据导入Excel;
用法:With TDS2Excel.Create(TDataSet(ADOQuery1)) do
Try
Save2File(SaveDialog1.FileName, True);
finally
Free;
end;
作者:Caidao (核心代码来自Ehlib)
时间:2003-04-09
地点:汕头
}

unit UntObject;

interface

Uses
DB, Classes;

var
CXlsBof: array[0..5] of Word = ($809, 8, 0, $10, 0, 0);
CXlsEof: array[0..1] of Word = ($0A, 00);
CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
CXlsNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
CXlsRk: array[0..4] of Word = ($27E, 10, 0, 0, 0);
CXlsBlank: array[0..4] of Word = ($201, 6, 0, 0, $17);

Type
TDS2Excel = Class(TObject)
Private
FCol: word;
FRow: word;
FDataSet: TDataSet;
Stream: TStream;
FWillWriteHead: boolean;
FBookMark: TBookmark;
procedure IncColRow;
procedure WriteBlankCell;
procedure WriteFloatCell(const AValue: Double);
procedure WriteIntegerCell(const AValue: Integer);
procedure WriteStringCell(const AValue: string);
procedure WritePrefix;
procedure WriteSuffix;
procedure WriteTitle;
procedure WriteDataCell;

procedure Save2Stream(aStream: TStream);
Public
procedure Save2File(FileName: string; WillWriteHead: Boolean);
Constructor Create(aDataSet: TDataSet);
end;

implementation

uses SysUtils;

Constructor TDS2Excel.Create(aDataSet: TDataSet);
begin
inherited Create;
FDataSet := aDataSet;
end;

procedure TDS2Excel.IncColRow;
begin
if FCol = FDataSet.FieldCount – 1 then
begin
Inc(FRow);
FCol :=0;
end
else
Inc(FCol);
end;

procedure TDS2Excel.WriteBlankCell;
begin
CXlsBlank[2] := FRow;
CXlsBlank[3] := FCol;
Stream.WriteBuffer(CXlsBlank, SizeOf(CXlsBlank));
IncColRow;
end;

procedure TDS2Excel.WriteFloatCell(const AValue: Double);
begin
CXlsNumber[2] := FRow;
CXlsNumber[3] := FCol;
Stream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
Stream.WriteBuffer(AValue, 8);
IncColRow;
end;

procedure TDS2Excel.WriteIntegerCell(const AValue: Integer);
var
V: Integer;
begin
CXlsRk[2] := FRow;
CXlsRk[3] := FCol;
Stream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
V := (AValue shl 2) or 2;
Stream.WriteBuffer(V, 4);
IncColRow;
end;

procedure TDS2Excel.WriteStringCell(const AValue: string);
var
L: Word;
begin
L := Length(AValue);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := FRow;
CXlsLabel[3] := FCol;
CXlsLabel[5] := L;
Stream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
Stream.WriteBuffer(Pointer(AValue)^, L);
IncColRow;
end;

procedure TDS2Excel.WritePrefix;
begin
Stream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;

procedure TDS2Excel.WriteSuffix;
begin
Stream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
end;

procedure TDS2Excel.WriteTitle;
var
n: word;
begin
for n := 0 to FDataSet.FieldCount – 1 do
WriteStringCell(FDataSet.Fields[n].FieldName);
end;

procedure TDS2Excel.WriteDataCell;
var
n: word;
begin
WritePrefix;
if FWillWriteHead then WriteTitle;
FDataSet.DisableControls;
FBookMark := FDataSet.GetBookmark;
FDataSet.First;
while not FDataSet.Eof do
begin
for n := 0 to FDataSet.FieldCount – 1 do
begin
if FDataSet.Fields[n].IsNull then
WriteBlankCell
else begin
case FDataSet.Fields[n].DataType of
ftSmallint, ftInteger, ftWord, ftAutoInc, ftBytes:
WriteIntegerCell(FDataSet.Fields[n].AsInteger);
ftFloat, ftCurrency, ftBCD:
WriteFloatCell(FDataSet.Fields[n].AsFloat);
else
WriteStringCell(FDataSet.Fields[n].AsString);
end;
end;
end;
FDataSet.Next;
end;
WriteSuffix;
if FDataSet.BookmarkValid(FBookMark) then FDataSet.GotoBookmark(FBookMark);
FDataSet.EnableControls;
end;

procedure TDS2Excel.Save2Stream(aStream: TStream);
begin
FCol := 0;
FRow := 0;
Stream := aStream;
WriteDataCell;
end;

procedure TDS2Excel.Save2File(FileName: string; WillWriteHead: Boolean);
var
aFileStream: TFileStream;
begin
FWillWriteHead := WillWriteHead;
if FileExists(FileName) then DeleteFile(FileName);
aFileStream := TFileStream.Create(FileName, fmCreate);
Try
Save2Stream(aFileStream);
Finally
aFileStream.Free;
end;
end;

end.
/////////////////////////////////////////////////////////

请问XLSReadWrite怎么实现对已有的excel xls表格进行指定单元的读取并赋值到string变量上
请问XLSReadWrite怎么实现对已有的excel xls表格进行指定单元的读取并赋值到string变量上
在线等,谢谢!

——解决方案——————————————————–
Delphi(Pascal) code

  //第一行到最后一行
  for iCount:=1 to xlsImport.Sheet[0].LastRow do
  begin
      ////第icount行第ilov列中的内容
      pItem.Caption:= xlsImport.sheets[0].AsString[ilov,iCount];
      CustomList.Add(pItem);
  end;