操作Excel的问题 未设置对象变量或With block变量

钞票搬运工 2019-01-09 03:39:13
procedure output_excel_file(file_path:string;report_type:Toutput_report_type;report_CDS:TADOQUERY);
var
Ecloum,Erow,row1,row2,i:integer;
Excel_application:variant;
strFormat:OleVariant;
begin


try
Excel_application:=createoleobject('Excel.application');
Excel_application.workbooks.add;

except
application.MessageBox('excel没有安装请检查','worng',mb_iconerror+mb_ok);
Excel_application.quit;
Excel_application:=unassigned;
exit;
end;
............................................
................................................

Excel_application.workbooks[1].SaveAs(file_path);

end;

工具:Delphi2010
环境:windows7 office2010 plus

运行到:
Excel_application.workbooks[1].SaveAs(file_path);
时;
报错
运行时错误"91"
未设置对象变量或With block变量
请大家帮看看该如何决绝。谢谢!


...全文
807 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
minizhuxianchun 2019-01-09
  • 打赏
  • 举报
回复
procedure TSearchForm.Ygnsr_DcbtnClick(Sender: TObject);
var
Excel: Variant;
WorkBook: OleVariant;
SavePath: OleVariant;
i,c: integer;
StatePanel: TMiniTitlePanel;
begin
if Ygnsr_Grid.RowCount=0 then
begin
MiniToolTip1.ShowBalloonTip(Ygnsr_Grid.Handle,2,'提示','无可导出的数据!');
Exit;
end;
DocSaveDialog.Title:='保存在';
DocSaveDialog.DefaultExt:='xlsx';
DocSaveDialog.Filter:='EXCEL文档|*xlsx';
if DocSaveDialog.Execute then
SavePath:=DocSaveDialog.FileName
else
Exit;

StatePanel:=TMiniTitlePanel.Create(nil);
StatePanel.Parent:=Ygnsr_Grid;
StatePanel.Width:=185;
StatePanel.Height:=32;
StatePanel.Left:=(StatePanel.Parent.Width-StatePanel.Width) div 2;
StatePanel.Top:=(StatePanel.Parent.Height-StatePanel.Height) div 2;
StatePanel.ShowTitle:=False;
StatePanel.Caption:='进度:0/'+IntToStr(Ygnsr_Grid.RowCount);
StatePanel.Font.Color:=clBlue;
StatePanel.Color:=clSkyBlue;
StatePanel.Show;
StatePanel.Repaint;
Self.Repaint;
try
Excel:=CreateOleObject('Excel.Application');
try
Excel.Visible:=False;
WorkBook:=Excel.WorkBooks.Add;
try
Excel.WorkSheets[1].Activate;

Excel.ActiveSheet.Columns[1].HorizontalAlignment:=2; //左对齐
Excel.ActiveSheet.Columns[1].ColumnWidth:=12;
Excel.Cells[1,1].Value:='员工姓名';
Excel.ActiveSheet.Columns[2].HorizontalAlignment:=4; //右对齐 3为居中
Excel.ActiveSheet.Columns[2].ColumnWidth:=12;
Excel.Cells[1,2].Value:='1月工资';
Excel.ActiveSheet.Columns[3].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[3].ColumnWidth:=12;
Excel.Cells[1,3].Value:='2月工资';
Excel.ActiveSheet.Columns[4].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[4].ColumnWidth:=12;
Excel.Cells[1,4].Value:='3月工资';
Excel.ActiveSheet.Columns[5].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[5].ColumnWidth:=12;
Excel.Cells[1,5].Value:='4月工资';
Excel.ActiveSheet.Columns[6].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[6].ColumnWidth:=12;
Excel.Cells[1,6].Value:='5月工资';
Excel.ActiveSheet.Columns[7].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[7].ColumnWidth:=12;
Excel.Cells[1,7].Value:='6月工资';
Excel.ActiveSheet.Columns[8].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[8].ColumnWidth:=12;
Excel.Cells[1,8].Value:='7月工资';
Excel.ActiveSheet.Columns[9].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[9].ColumnWidth:=12;
Excel.Cells[1,9].Value:='8月工资';
Excel.ActiveSheet.Columns[10].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[10].ColumnWidth:=12;
Excel.Cells[1,10].Value:='9月工资';
Excel.ActiveSheet.Columns[11].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[11].ColumnWidth:=12;
Excel.Cells[1,11].Value:='10月工资';
Excel.ActiveSheet.Columns[12].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[12].ColumnWidth:=12;
Excel.Cells[1,12].Value:='11月工资';
Excel.ActiveSheet.Columns[13].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[13].ColumnWidth:=12;
Excel.Cells[1,13].Value:='12月工资';
Excel.ActiveSheet.Columns[14].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[14].ColumnWidth:=12;
Excel.Cells[1,14].Value:='年终奖';
Excel.ActiveSheet.Columns[15].HorizontalAlignment:=4;
Excel.ActiveSheet.Columns[15].ColumnWidth:=12;
Excel.Cells[1,15].Value:='总计';
StatePanel.Caption:='任务处理中...0/'+IntToStr(Ygnsr_Grid.RowCount);
StatePanel.Update;
for i:=1 to Ygnsr_Grid.RowCount do
for c:=1 to Ygnsr_Grid.Titles.Count do
begin
Excel.Cells[i+1,c].Value:=Ygnsr_Grid.Cells[c-1,i-1];

StatePanel.Caption:='任务处理中...'+IntToStr(i)+'/'+IntToStr(Ygnsr_Grid.RowCount);
StatePanel.Update;
end;

StatePanel.Caption:='正在保存...';
StatePanel.Repaint;
WorkBook.SaveAs(SavePath);
finally
WorkBook.Close;
Excel.Quit;
Excel:=Unassigned; //退出时不保留进程。
end;
except
Abort;
MessageBox(Handle,'系统未安装EXCEL!','提示',MB_OK+MB_ICONWARNING);
end;
finally
StatePanel.Free;
end;
end;
lyhoo163 2019-01-09
  • 打赏
  • 举报
回复
关注。。。。。。。

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧