怎么读写EXCEL文件??? ( 积分: 200 )

  • 主题发起人 主题发起人 马如龙
  • 开始时间 开始时间

马如龙

Unregistered / Unconfirmed
GUEST, unregistred user!
没有安装OFFICE,用什么读写EXCEL文件呀
各位老大救命呀
 
没有安装OFFICE,用什么读写EXCEL文件呀
各位老大救命呀
 
或者谁好心 给偶个XLSREADWRITE控件也可以的 最好有原代码 裸体跪求了
 
f1book 试试
 
lt66, 你有吗 给个下载地址呀
 
有XLSREADWRITE,没有源码
 
我个人觉得XLSREADWRITE是非常不错的控件,并且不用安装OFFICE。
你下载的XLSREADWRITE里没有DEMO的吗?我下载的里面有,下面给出我自己编的程序中最重要的一段。
procedure Tcheck_852_Form1.XLSRead1BlankCell(Sender: TObject; Col, Row,
FormatIndex: Integer);
begin
if judge_col(col)=true then
begin
if Row <> CurrRow then begin
writeline;
line:='';
CurrRow := Row;
end;
Line := Line + Separator;
end
else
exit;
end;

procedure Tcheck_852_Form1.XLSRead1BoolCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value, IsFormulaValue: Boolean);
begin
if judge_col(col)=true then
begin
if Row <> CurrRow then begin
writeline;
line:='';
CurrRow := Row;
end;
if Value then
Line := Line + XLSRead1.StrTRUE + Separator
else
Line := Line + XLSRead1.StrFalse + Separator;
end
else
exit;
end;

procedure Tcheck_852_Form1.XLSRead1NumberCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: Double; IsFormulaValue: Boolean);
begin
if judge_col(col)=true then
begin
if Row <> CurrRow then begin
writeline;
line:='';
CurrRow := Row;
end;
Line := Line + FloatToStr(Value) + Separator;
end
else
exit;
end;

procedure Tcheck_852_Form1.XLSRead1DateTimeCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: TDateTime; IsFormulaValue: Boolean);
begin
if judge_col(col)=true then
begin
if Row <> CurrRow then begin
writeline;
line:='';
CurrRow := Row;
end;
Line := Line + DateTimeToStr(Value) + Separator;
end
else
exit;
end;

procedure Tcheck_852_Form1.XLSRead1StringCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: String; IsFormulaValue: Boolean);
begin
if judge_col(col)=true then
begin
if Row <> CurrRow then begin
writeline;
line:='';
CurrRow := Row;
end;
Line := Line + Value + Separator;
end
else
exit;
end;
 
这是XLSREDWRITE所带DEMO的读部份:
unit ReadMain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
XLSRead, Grids, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
edFilename: TEdit;
Grid: TStringGrid;
Button3: TButton;
OpenDialog: TOpenDialog;
XLSRead: TXLSRead;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure XLSReadBoolCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value, IsFormulaValue: Boolean);
procedure XLSReadFormulaCell(Sender: TObject; Col, Row: Integer;
Formula: String);
procedure XLSReadNumberCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: Double; IsFormulaValue: Boolean);
procedure XLSReadStringCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: String; IsFormulaValue: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
Grid.ColWidths[0] := 20;
for i := 1 to Grid.ColCount - 1 do
Grid.Cells[i,0] := Char(Ord('A') + i - 1);
for i := 1 to Grid.RowCount - 1 do
Grid.Cells[0,i] := IntToStr(i);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
XLSRead.Filename := edFilename.Text;
XLSRead.Read;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
OpenDialog.Filename := edFilename.Text;
if OpenDialog.Execute then
edFilename.Text := OpenDialog.Filename;
end;

procedure TForm1.XLSReadBoolCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value, IsFormulaValue: Boolean);
begin
if not IsFormulaValue then begin
if Value then
Grid.Cells[Col + 1,Row + 1] := 'True'
else
Grid.Cells[Col + 1,Row + 1] := 'False';
end;
end;

procedure TForm1.XLSReadFormulaCell(Sender: TObject; Col, Row: Integer;
Formula: String);
begin
Grid.Cells[Col + 1,Row + 1] := Formula;
end;

procedure TForm1.XLSReadNumberCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: Double; IsFormulaValue: Boolean);
begin
if not IsFormulaValue then
Grid.Cells[Col + 1,Row + 1] := FloatToStr(Value);
end;

procedure TForm1.XLSReadStringCell(Sender: TObject; Col, Row,
FormatIndex: Integer; Value: String; IsFormulaValue: Boolean);
begin
if not IsFormulaValue then
Grid.Cells[Col + 1,Row + 1] := Value;
end;

end.
 
这是XLSREDWRITE所带DEMO的写部份:
unit WriteMain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls, XLSWrite;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
XLSWrite: TXLSWrite;
Label1: TLabel;
edFilename: TEdit;
Button3: TButton;
SaveDialog: TSaveDialog;
Grid: TStringGrid;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
Grid.ColWidths[0] := 20;
for i := 1 to Grid.ColCount - 1 do
Grid.Cells[i,0] := Char(Ord('A') + i - 1);
for i := 1 to Grid.RowCount - 1 do
Grid.Cells[0,i] := IntToStr(i);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
SaveDialog.Filename := edFilename.Text;
if SaveDialog.Execute then
edFilename.Text := SaveDialog.Filename;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Row,Col: integer;
V: double;
begin
XLSWrite.Filename := edFilename.Text;
XLSWrite.OpenFile;
// Scan trough the cells
for Row := 1 to Grid.RowCount - 1 do begin
for Col := 1 to Grid.ColCount - 1 do begin
// If not empty...
if Grid.Cells[Col,Row] <> '' then begin
// Cells that begins with '=' is assumed to be formulas.
if Grid.Cells[Col,Row][1] = '=' then
// Write the formula, but remove the leading '='.
XLSWrite.WriteFormula(Col - 1,Row - 1,-1,0,Copy(Grid.Cells[Col,Row],2,1024))
else begin
try
// Test if the cell is a number
V := StrToFloat(Grid.Cells[Col,Row]);
XLSWrite.WriteNumber(Col - 1,Row - 1,-1,V)
except
// If not, write a string value.
XLSWrite.WriteString(Col - 1,Row - 1,-1,Grid.Cells[Col,Row])
end;
end;
end;
end;
end;
XLSWrite.CloseFile;
end;

end.
 
weiliu, 能不能发你的XLSREDWRITE给我呀 我试试 keleicetea@163.com
谢谢
 
还有用XLSREDWRITE这个 能合并格子吗 能增加新的Sheet等等
 
咋回事,我看不见问题讨论的结果?
 
好的,晚上回家我发给你。
 
//转换成Excel文档
//muhx 20040423 night
procedure TMainFrm.ChangeToExcel(TmpCelInfo: TCellInfo; aFileName: string);
var
strSaveFileName: string;
newExcel: Variant;
i: Integer;
begin
strSaveFileName := aFileName;
try
NewExcel := CreateOleObject('Excel.Application');
newExcel.Visible := False;
except
MessageDlg('启动Excel失败!', mtError, [mbOk], 0);
NewExcel.Quit;
NewExcel := Unassigned;
Exit;
end;

try
try
newExcel.DisplayAlerts := False;
newExcel.Caption := 'LIR256B01内阻测试仪测试数据';
newExcel.WorkBooks.Add;
newExcel.ActiveSheet.Name := '内阻测试数据';

//设置列宽
newExcel.ActiveSheet.Columns[1].ColumnWidth := 7;
newExcel.ActiveSheet.Columns[2].ColumnWidth := 5;
newExcel.ActiveSheet.Columns[3].ColumnWidth := 5;
newExcel.ActiveSheet.Columns[4].ColumnWidth := 11;
newExcel.ActiveSheet.Columns[5].ColumnWidth := 9;
newExcel.ActiveSheet.Columns[6].ColumnWidth := 9;
newExcel.ActiveSheet.Columns[7].ColumnWidth := 9;
newExcel.ActiveSheet.Columns[8].ColumnWidth := 10;
newExcel.ActiveSheet.Columns[9].ColumnWidth := 10;
newExcel.ActiveSheet.Columns[10].ColumnWidth := 16;
//newExcel.ActiveSheet.PageSetup.CenterVertically := 2/0.035;

//添加表头 20040310 muhx
newExcel.Cells[1,5].Value := '内阻测试数据';
newExcel.ActiveSheet.Range['A1:J1'].Borders[3].Weight := 3;
newExcel.ActiveSheet.Range['A1:J1'].Borders[4].Weight := 3;
newExcel.ActiveSheet.Rows[1].Font.Bold := True;
newExcel.Cells[2,1].Value := '通道号';
newExcel.Cells[2,2].Value := '区号';
newExcel.Cells[2,3].Value := '板号';
newExcel.Cells[2,4].Value := '板上电池号';
newExcel.Cells[2,5].Value := '电池编号';
newExcel.Cells[2,6].Value := '通道选择';
newExcel.Cells[2,7].Value := '分选结果';
newExcel.Cells[2,8].Value := '电压(mV)';
newExcel.Cells[2,9].Value := '内阻(mΩ)';
newExcel.Cells[2,10].Value := '检测时间';
newExcel.ActiveSheet.Range['A2:J2'].Borders[4].Weight := 3;

//向Excel工作表中添加内容
for i := 0 to CellNo - 1 do
begin
ShowWait(CellNo - 1, 0, i, '当前进度:');
newExcel.Cells[i + 3, 1].Value := IntToStr(TmpCelInfo.iCellID);
newExcel.Cells[i + 3, 2].Value := IntToStr(TmpCelInfo.iZoneID);
newExcel.Cells[i + 3, 3].Value := IntToStr(TmpCelInfo.iBoardID);
newExcel.Cells[i + 3, 4].Value := IntToStr(TmpCelInfo.iCellNoInBoard);
newExcel.Cells[i + 3, 5].Value := TmpCelInfo.sCellName;
newExcel.Cells[i + 3, 6].Value := BoolToStr(TmpCelInfo.bCellSelected,True);
newExcel.Cells[i + 3, 7].Value := TmpCelInfo.sSortResult;
newExcel.Cells[i + 3, 8].Value := FormatFloat('0', TmpCelInfo.rVoltage);
newExcel.Cells[i + 3, 9].Value := FormatFloat('0.0', TmpCelInfo.rResistance);
newExcel.Cells[i + 3, 10].Value := DateTimeToStr(TmpCelInfo.tTestTime);
end;

//保存
if not newExcel.ActiveWorkBook.Saved then
newExcel.ActiveWorkBook.SaveAs(strSaveFileName);
FreeWait; //若进度条没有释放则释放进度条
sleep(20);
ShowStatusInfo('转换Excel文档完毕');
Application.MessageBox('转换数据完毕。', '提示',
MB_OK or MB_ICONINFORMATION);
except
Application.MessageBox('转换成Excel格式失败!', 'LIR256B01电池内阻测试仪',
MB_OK or MB_ICONWARNING);
end;
finally
newExcel.WorkBooks.Close;
newExcel.Quit;
newExcel := unassigned;
end;
end;

//--------------------在StatusBar中显示ProgressBar--------------------//

//取得StatusBar的Rect
procedure TMainFrm.StatusBarDrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
begin
fStatusDrawRect := Rect;
end;

//显示进度条
//muhx 20040418 am
procedure TMainFrm.ShowWait(aMax, aMin, aPos: integer; S: string);
begin
if not Assigned(fProgress) then //Assigned returns False if P is nil, True otherwise.
begin
try
fProgress := TProgressBar.Create(MainFrm);
fPanelWidth := StatusBar.Panels.Items[2].Width;
StatusBar.Panels.Items[2].Width := 250;
StatusBar.Repaint;
Application.ProcessMessages;
with fProgress do
begin
Parent := StatusBar;
Top := fStatusDrawRect.Top;
Left := fStatusDrawRect.Left;
Width := fStatusDrawRect.Right - fStatusDrawRect.Left;
Height := fStatusDrawRect.Bottom - fStatusDrawRect.Top;
min := aMin;
max := aMax;
Visible := True;
end;
except
fProgress.Free;
fProgress := nil;
end;
end;

try
with fProgress do
begin
if aPos < Max then
begin
Visible := True;
Position := aPos;
StatusBar.Panels.Items[1].Text := s;
StatusBar.Repaint;
Application.ProcessMessages;
end
else
begin
StatusBar.Panels.Items[2].Width := fPanelWidth;
Free;
fProgress := nil;
end;
end;
except
fProgress.Free;
fProgress := nil;
end;
end;

//释放进度条
//muhx 20040418 am
procedure TMainFrm.FreeWait;
begin
if Assigned(fProgress) then
fProgress := nil;
end;
 
已发出,请查收。
 
多人接受答案了。
 
后退
顶部