5,927
社区成员




procedure TForm1.N2Click(Sender: TObject);
var eclApp,WorkBook:Variant; {声明为OLE Automation对象}
xlsFileName:string;
begin
self.Caption:='新建工作表';
xlsFileName:=slj+'ex.xls';
try
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
Except
Application.MessageBox('你的机器没有安装Microsoft Excel','使用Microsoft Excel',MB_OK+MB_ICONWarning);
Exit;
End;
Try
WorkBook:=eclApp.workbooks.Add;
///第一行 第一列
EclApp.Cells(2,1):='序号';
EclApp.Cells(2,2):='家长姓名';
EclApp.Cells(2,3):='家长年龄';
EclApp.Cells(2,4):='手机' ;
EclApp.Cells(2,5):='邮箱';
EclApp.Cells(2,6):='孩子姓名';
EclApp.Cells(2,7):='孩子性别';
EclApp.Cells(2,8):='孩子学龄';
EclApp.Cells(2,9):='区域';
EclApp.Cells(2,10):='区域';
WorkBook.SaveAS(xlsFileName);
WorkBook.close;
///EclApp.Quit; //退出Excel Application
except
ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。');
WorkBook.close;
EclApp.Quit;
eclApp:=Unassigned;
end;
end;
ComObj
新建excel表格 xe5function ExportStrGridToExcel(Args: array of const): Boolean;
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
I: Integer;
begin
Result := False;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;
try
XLApp := CreateOleObject('Excel.Application');
except
Exit;
end;
XLApp.WorkBooks.Add;
XLApp.SheetsInNewWorkbook := High(Args) + 1;
for I := Low(Args) to High(Args) do
begin
with TStringGrid(Args[I].VObject) do
begin
XLApp.WorkBooks[1].WorkSheets[I+1].Name := Name;
Sheet := XLApp.Workbooks[1].WorkSheets[Name];
for jCount := 0 to RowCount - 1 do
begin
for iCount := 0 to ColCount - 1 do
begin
Sheet.Cells[jCount + 1, iCount + 1] := Cells[iCount, jCount];
end;
end;
end;
end;
XlApp.Visible := True;
end;
StringGrid1导出到excel
编辑版本 delphi xe5