delphi使用ado导出excel(2)

简介:
var
  FileName, s: String;
  i,n:integer;
  excelapp:Variant;
  Temp_Worksheet: _WorkSheet;
begin
  excelapp := CreateOleObject('excel.application');
  excelapp.Visible := true;
  excelapp.Caption := '临床路径每天执行情况表';
  ExcelApp.WorkBooks.Add;
  ExcelApp.WorkSheets[2].Activate;

  //导出
  if Self.qry_pat.RecordCount = 0 then
  begin
    s := '没有导出数据...';
    Application.MessageBox(PChar(s), PChar(Application.Title),
      MB_OK or MB_ICONERROR);
    Exit;
  end;
  Application.ProcessMessages;

  excelapp.Range['a1', 'a'+inttostr(qry_pat.RecordCount)].ColumnWidth := 20;
  excelapp.cells.Item[1,1] := qry_pat.Fields[1].FieldName;
  excelapp.Cells.Item[1,1].Font.name := '宋体';
  excelapp.Cells.Item[1,1].Font.bold := true;
  excelapp.Cells.Item[1,1].orientation := xlhorizontal;
  excelapp.Cells.Item[1,1].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,1].font.size := 11;
  excelapp.cells.Item[1,2] := qry_pat.Fields[2].FieldName;
  excelapp.Cells.Item[1,2].Font.name := '宋体';
  excelapp.Cells.Item[1,2].Font.bold := true;
  excelapp.Cells.Item[1,2].orientation := xlhorizontal;
  excelapp.Cells.Item[1,2].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,2].font.size := 11;
    excelapp.cells.Item[1,3] := qry_pat.Fields[3].FieldName;
  excelapp.Cells.Item[1,3].Font.name := '宋体';
  excelapp.Cells.Item[1,3].Font.bold := true;
  excelapp.Cells.Item[1,3].orientation := xlhorizontal;
  excelapp.Cells.Item[1,3].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,3].font.size := 11;
    excelapp.cells.Item[1,4] := qry_pat.Fields[4].FieldName;
  excelapp.Cells.Item[1,4].Font.name := '宋体';
  excelapp.Cells.Item[1,4].Font.bold := true;
  excelapp.Cells.Item[1,4].orientation := xlhorizontal;
  excelapp.Cells.Item[1,4].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,4].font.size := 11;
    excelapp.cells.Item[1,5] := qry_pat.Fields[5].FieldName;
  excelapp.Cells.Item[1,5].Font.name := '宋体';
  excelapp.Cells.Item[1,5].Font.bold := true;
  excelapp.Cells.Item[1,5].orientation := xlhorizontal;
  excelapp.Cells.Item[1,5].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,5].font.size := 11;
    excelapp.cells.Item[1,6] := qry_pat.Fields[6].FieldName;
  excelapp.Cells.Item[1,6].Font.name := '宋体';
  excelapp.Cells.Item[1,6].Font.bold := true;
  excelapp.Cells.Item[1,6].orientation := xlhorizontal;
  excelapp.Cells.Item[1,6].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,6].font.size := 11;
    excelapp.cells.Item[1,7] := qry_pat.Fields[7].FieldName;
  excelapp.Cells.Item[1,7].Font.name := '宋体';
  excelapp.Cells.Item[1,7].Font.bold := true;
  excelapp.Cells.Item[1,7].orientation := xlhorizontal;
  excelapp.Cells.Item[1,7].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,7].font.size := 11;
    excelapp.cells.Item[1,8] := qry_pat.Fields[8].FieldName;
  excelapp.Cells.Item[1,8].Font.name := '宋体';
  excelapp.Cells.Item[1,8].Font.bold := true;
  excelapp.Cells.Item[1,8].orientation := xlhorizontal;
  excelapp.Cells.Item[1,8].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,8].font.size := 11;
    excelapp.cells.Item[1,9] := qry_pat.Fields[9].FieldName;
  excelapp.Cells.Item[1,9].Font.name := '宋体';
  excelapp.Cells.Item[1,9].Font.bold := true;
  excelapp.Cells.Item[1,9].orientation := xlhorizontal;
  excelapp.Cells.Item[1,9].verticalAlignment := xlTop;
  excelapp.Cells.Item[1,9].font.size := 11;


  qry_pat.First;
  n := 2;
  while not qry_pat.eof do
  begin
    excelapp.cells.Item[n,1] := qry_pat.Fields[1].AsString;
    excelapp.Cells.Item[n,1].Font.name := '宋体';
    excelapp.Cells.Item[n,1].Font.bold := true;
    excelapp.Cells.Item[n,1].orientation := xlhorizontal;
    excelapp.Cells.Item[n,1].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,1].font.size := 11;
    excelapp.Cells.Item[n,1].wraptext := true;
    excelapp.cells.Item[n,2] := qry_pat.Fields[2].AsString;
    excelapp.Cells.Item[n,2].Font.name := '宋体';
    excelapp.Cells.Item[n,2].Font.bold := true;
    excelapp.Cells.Item[n,2].orientation := xlhorizontal;
    excelapp.Cells.Item[n,2].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,2].font.size := 11;
    excelapp.Cells.Item[n,2].wraptext := true;
        excelapp.cells.Item[n,3] := qry_pat.Fields[3].AsString;
    excelapp.Cells.Item[n,3].Font.name := '宋体';
    excelapp.Cells.Item[n,3].Font.bold := true;
    excelapp.Cells.Item[n,3].orientation := xlhorizontal;
    excelapp.Cells.Item[n,3].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,3].font.size := 11;
    excelapp.Cells.Item[n,3].wraptext := true;
        excelapp.cells.Item[n,4] := qry_pat.Fields[4].AsString;
    excelapp.Cells.Item[n,4].Font.name := '宋体';
    excelapp.Cells.Item[n,4].Font.bold := true;
    excelapp.Cells.Item[n,4].orientation := xlhorizontal;
    excelapp.Cells.Item[n,4].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,4].font.size := 11;
    excelapp.Cells.Item[n,4].wraptext := true;
        excelapp.cells.Item[n,5] := qry_pat.Fields[5].AsString;
    excelapp.Cells.Item[n,5].Font.name := '宋体';
    excelapp.Cells.Item[n,5].Font.bold := true;
    excelapp.Cells.Item[n,5].orientation := xlhorizontal;
    excelapp.Cells.Item[n,5].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,5].font.size := 11;
    excelapp.Cells.Item[n,5].wraptext := true;
        excelapp.cells.Item[n,6] := qry_pat.Fields[6].AsString;
    excelapp.Cells.Item[n,6].Font.name := '宋体';
    excelapp.Cells.Item[n,6].Font.bold := true;
    excelapp.Cells.Item[n,6].orientation := xlhorizontal;
    excelapp.Cells.Item[n,6].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,6].font.size := 11;
    excelapp.Cells.Item[n,6].wraptext := true;
        excelapp.cells.Item[n,7] := qry_pat.Fields[7].AsString;
    excelapp.Cells.Item[n,7].Font.name := '宋体';
    excelapp.Cells.Item[n,7].Font.bold := true;
    excelapp.Cells.Item[n,7].orientation := xlhorizontal;
    excelapp.Cells.Item[n,7].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,7].font.size := 11;
    excelapp.Cells.Item[n,7].wraptext := true;
        excelapp.cells.Item[n,8] := qry_pat.Fields[8].AsString;
    excelapp.Cells.Item[n,8].Font.name := '宋体';
    excelapp.Cells.Item[n,8].Font.bold := true;
    excelapp.Cells.Item[n,8].orientation := xlhorizontal;
    excelapp.Cells.Item[n,8].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,8].font.size := 11;
    excelapp.Cells.Item[n,8].wraptext := true;
        excelapp.cells.Item[n,9] := qry_pat.Fields[9].AsString;
    excelapp.Cells.Item[n,9].Font.name := '宋体';
    excelapp.Cells.Item[n,9].Font.bold := true;
    excelapp.Cells.Item[n,9].orientation := xlhorizontal;
    excelapp.Cells.Item[n,9].verticalAlignment := xlTop;
    excelapp.Cells.Item[n,9].font.size := 11;
    excelapp.Cells.Item[n,9].wraptext := true;

    inc(n);
    qry_pat.Next;
  end;
 // xlWorksheet.cells.Item[n+qry_medicalorder.recordcount,1] := '';

  SaveDialogExport.Filter := 'Excel文件 (*.xls)';
  SaveDialogExport.Title := '导出为';
  if excelapp.activeworkbook.saved then
  begin
    ExcelApp.ActiveSheet.PrintPreview;
    ExcelApp.WorkBooks.Close;
    ExcelApp.Quit;
    Screen.Cursor := crDefault;
    // Application.ProcessMessages;
    s := '导出完成...';
    Application.MessageBox(PChar(s), PChar(Application.Title),
      MB_OK or MB_IconInformation);

  end;














本文转自鹅倌51CTO博客,原文链接:http://blog.51cto.com/kaixinbuliao/1119516 ,如需转载请自行联系原作者



相关文章
|
1月前
|
NoSQL 关系型数据库 MySQL
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
业务诉求:考虑到数据库数据日渐增多,导出会有全量数据的导出,多人同时导出可以会对服务性能造成影响,导出涉及到mysql查询的io操作,还涉及文件输入、输出流的io操作,所以对服务器的性能会影响的比较大;结合以上原因,对导出操作进行排队; 刚开始拿到这个需求,第一时间想到就是需要维护一个FIFO先进先出的队列,给定队列一个固定size,在队列里面的人进行排队进行数据导出,导出完成后立马出队列,下一个排队的人进行操作;还考虑到异步,可能还需要建个文件导出表,主要记录文件的导出情况,文件的存放地址,用户根据文件列表情况下载导出文件。
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
|
7天前
|
easyexcel 数据库
公司大佬对excel导入、导出的封装,那叫一个秒啊
封装公司统一使用的组件的主要目标是为了简化开发人员的调用流程,避免各个项目组重复集成和编写不规范的代码。文中提到对阿里EasyExcel进行了二次封装,提供了导入和导出功能,并支持模板的导入和导出。此外,还处理了读取数据与实际保存数据不一致的情况,通过提供自定义转换器来解决。
27 0
|
7天前
|
数据库
开发指南009-从list导出excel文件
从数据库返回一般是对象的列表,平台底层提供了从list转为excel文件的方法
|
7天前
|
前端开发
开发指南007-导出Excel
平台上开发导出Excel比过去的单体架构要复杂些,因为前端和后台不在一个进程空间里。
|
1月前
|
SQL Java easyexcel
【Java】百万数据excel导出功能如何实现
【Java】百万数据excel导出功能如何实现
139 0
|
25天前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
52 0
|
1月前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
21 0
|
1月前
|
Java API Apache
使用AOP+反射实现Excel数据的读取
使用AOP+反射实现Excel数据的读取
excel根据数据得出公式
excel根据数据得出公式
|
1月前
|
SQL 数据可视化 数据处理
使用SQL和Python处理Excel文件数据
使用SQL和Python处理Excel文件数据
54 0