打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi Stringgrid 导出Excel

Delphi Stringgrid 导出Excel

时间:2011-5-26来源:yang 作者: peng点击: 29次

procedure Tfm1.Stringgridtoexcel(atitle:string;Astringgrid: Tstringgrid; Afontsize: integer);
var xlapp, xlsheet: variant;
  row, CCC, nnn, jjj: integer;  
begin  
  try  
    xlapp := createoleobject(‘excel.application‘);  
  except  
    showmessage(‘not found excel in your system, so can not create file!‘);  
    exit;  
  end;  
  try  
    ccc := Astringgrid.ColCount;  
    xlapp.workbooks.add; //添加新工作簿  
    xlapp.visible := false;  
    xlsheet := xlapp.activesheet;  
    xlapp.activewindow.windowstate := 2;  
    xlapp.range[xlsheet.cells[1, 1], xlsheet.cells[1, ccc]].MERGE;  
    xlsheet.cells[1, 1].value := Atitle; //页头第一行;  
    xlsheet.cells[1, 1].HorizontalAlignment := -4108;  
 
    row := 2;  
    for jjj := 0 to Astringgrid.RowCount - 1 do  
    begin  
      for nnn := 1 to ccc do  
        xlsheet.cells[row, nnn] := trim(Astringgrid.Cells[nnn - 1, row - 2]);  
      xlsheet.rows[row].RowHeight := 18;  
      inc(row);  
    end;  
    xlapp.visible := true;  
        //格式调整  
    xlapp.range[xlsheet.cells[row, 1], xlsheet.cells[row, 13]].WrapText := True;  
    xlapp.range[xlsheet.cells[row, 1], xlsheet.cells[row, 13]].HorizontalAlignment := -4108;  
    xlsheet.pagesetup.headerMargin := 1 / 0.035; //页眉到顶端边距1cm  
    xlsheet.pagesetup.footerMargin := 0.6 / 0.035; //页脚到底端边距1cm  
    xlsheet.pagesetup.topMargin := 1 / 0.035; //顶边距1cm  
    XLSHEET.pagesetup.bottomMargin := 1.3 / 0.035; //底边距1cm  
    xlsheet.pagesetup.leftMargin := 0.5 / 0.035; //左边距1cm  
    xlsheet.pagesetup.rightMargin := 0.5 / 0.035; //右边距1cm  
    xlsheet.pagesetup.leftfooter := ‘制表: ‘ + ‘制表人‘;  
    xlsheet.pagesetup.centerfooter := ‘‘; //页脚  
    xlsheet.pagesetup.rightfooter := ‘第&P页/共&N页‘;  
    xlsheet.pagesetup.leftHeader := ‘‘;  
    xlsheet.pagesetup.orientation := 1; //横向  
    xlsheet.pagesetup.printtitlerows := ‘$1:$1‘;  
    xlsheet.rows[1].font.name := ‘宋体‘; //设置第一行字体属性  
    xlsheet.rows[1].font.bold := true;  
    xlsheet.rows[1].font.size := 20;  
    xlsheet.rows[1].RowHeight := 28;  
    for nnn := 1 to ccc do  
    begin  
      xlsheet.columns[nnn].columnwidth := Astringgrid.ColWidths[nnn - 1] * 0.1188;  
    end;  
 
    for nnn := 1 to 4 do begin  
      xlapp.range[xlsheet.cells[2, 1], xlsheet.cells[row - 1, CCC]].borders[nnn].linestyle := 1;  
      xlapp.range[xlsheet.cells[2, 1], xlsheet.cells[row - 1, CCC]].borders[nnn].weight := 1;  
    end;  
 
    xlapp.range[xlsheet.cells[2, 1], xlsheet.cells[row - 1, CCC]].font.size := Afontsize;  
    xlapp.range[xlsheet.cells[2, 1], xlsheet.cells[row - 1, ccc]].WrapText := True;  
 
    varclear(xlsheet);  
    varclear(xlapp);  
  except  
    on E: Exception do  
    begin  
      xlapp.quit;
      showmessage(‘出现错误‘+E.message);  
    end;  
  end;  
end;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
关于 VFP 输出 EXCEL 的格式
视图操作(代理)--将选中文档的条目导入EXCEL表格中
DELPHI如何将数据导出到指定格式的EXCEL模版
vb生成操作保存提示excel文件
VB打开EXCEL的方法
VB 读取Excel表的内容 |VB 网|VB 视频教程|VB编程入门网
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服