var
ExcelApp:Variant;
expath,str:String;
ExCountRow,ExCountCol,i,j:integer;
begin
if dlgOpen1.Execute then
begin
expath:=dlgOpen1.FileName;
application.ProcessMessages;
try
ExcelApp:=CreateOleObject('excel.application');
ExcelApp.visible:=false;
ExcelApp.workbooks.open(expath);
ExCountRow:=0;
while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do
begin
Inc(ExCountRow);
end;
AdvStringGrid1.RowCount:=ExCountRow-1;
for i:=1 to AdvStringGrid1.RowCount do
begin
for j:=1 to AdvStringGrid1.ColCount do
begin
AdvStringGrid1.Cells[j-1,i-1]:=ExcelApp.cells[i+1,j].value;
end;
end;
ExcelApp.workbooks.close;
Excelapp.quit;
finally
ExcelApp.workbooks.close;
Excelapp.quit;
end;
end;
end;
procedure TForm1.N2Click(Sender: TObject);
var
ExportFile: String;
i: Integer;
FExcel : Variant;
begin
if Application.MessageBox('是否确定导出?','提示',MB_OK or MB_OKCANCEL) <> id_ok then exit;
Try
FExcel := CreateOleObject('Excel.Application');
FExcel.Visible := True;
ExportFile := extractfilepath(application.ExeName) + '1.xls' ;
FExcel.Workbooks.add(ExportFile);
for i := 1 to sg3.RowCount do
begin
for j := 0 to sg3.ColCount - 1 do
FExcel.Workbooks[1].sheets[1].cells(i,j+1) := sg3.Cells[j,i-1] ;
end;
OR
for i := 1 to AdvStringGrid1.RowCount do
begin
FExcel.Workbooks[1].sheets[1].cells(i,1) := AdvStringGrid1.Cells[0,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,2) := AdvStringGrid1.Cells[1,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,3) := AdvStringGrid1.Cells[2,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,4) := AdvStringGrid1.Cells[3,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,5) := AdvStringGrid1.Cells[4,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,6) := AdvStringGrid1.Cells[5,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,7) := AdvStringGrid1.Cells[6,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,8) := AdvStringGrid1.Cells[7,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,9) := AdvStringGrid1.Cells[8,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,10) := AdvStringGrid1.Cells[9,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,11) := AdvStringGrid1.Cells[10,i-1] ;
FExcel.Workbooks[1].sheets[1].cells(i,12) := AdvStringGrid1.Cells[11,i-1] ;
end;
ShowMessage('输出完毕!');
Except
ShowMessage('发生未知的错误,可能是您的电脑尚未安装电子表格软件!');
End;
end;
procedure SgToExcel_txt(AdvSg:TAdvStringGrid);
var
ExportFile: String;
i,j: Integer;
FExcel: Variant; // 表格中的内容可以输出到电子表格中去
str : String ;
begin
Try
FExcel := CreateOleObject('Excel.Application');
FExcel.Visible := True;
ExportFile := ExtractFilePath(ExeOrDllPath) + 'output.xls' ;
FExcel.Workbooks.add(ExportFile);
FExcel.Workbooks[1].sheets[1].Cells.NumberFormat := '@ ';
Str := '';
for i := 0 to AdvSg.RowCount - 1 do
begin
for j := AdvSg.FixedCols to AdvSg.ColCount - 2 do
begin
Str := Str + AdvSg.Cells[j,i] + #9 ;
end;
Str := Str + AdvSg.Cells[AdvSg.ColCount -1,i] + #13 ;
end;
ClipBoard.AsText := Str ;
if ClipBoard.HasFormat(CF_TEXT) then
FExcel.Workbooks[1].sheets[1].Paste;
FExcel.Workbooks[1].sheets[1].Columns.AutoFit;
FExcel.Workbooks[1].sheets[1].Cells[1,1].select;
//ClipBoard.Clear;
Except
ShowError('发生未知的错误,可能是您的电脑尚未安装电子表格软件!');
End;
end;