• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi表格数据导出到excel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

procedure TfrmMain.xGridDataToExcel(mGrid: TStringGrid; pTitle, SePTitle,
pTail, pStrCols: string);
var
i,j:integer;
strlist:Tstringlist;
str,Filename:string;
h,k:integer;
Excelid: OleVariant;
s: string;
v,sheet,range:variant;
icol,irow:integer;
nCols:integer;
nCurrCol:integer;
nCurrRow:integer;
begin
excelSaveto.Title:='请选择需要导出到的目标文件';
if excelSaveto.Execute = false then exit;
Filename:=trim(excelSaveto.FileName);
nCols := 0;
for j:=0 to mGrid.ColCount - 1 do begin
      if mGrid.ColWidths[j]>0 then nCols := nCols + 1;
end;
if nCols = 0 then begin
      showmessage('没有数据,无法导出!');
      exit;
end;
//导出到excel表格
try
    Excelid := CreateOLEObject('Excel.Application');
except
    Application.MessageBox('Excel没有安装!', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
    Exit;
end;
Excelid.Visible := false;
//Excelid.Visible := true;
Excelid.WorkBooks.Add;
//Excelid.WorkBooks[1].WorkSheets[1].Name := pTitle;
Sheet := Excelid.Workbooks[1].WorkSheets[1];
//标题
sheet.cells[1, 1] := pTitle;
sheet.range[sheet.cells[1, 1],sheet.cells[1,nCols]].Select; //选择该列
Excelid.selection.HorizontalAlignment := $FFFFEFF4;                               //居中
Excelid.selection.MergeCells := True;
//小标题
nCurrRow := 2;
if SePTitle <> '' then begin
    Sheet.Cells[2,1] := SePTitle;
    sheet.range[sheet.cells[2, 1],sheet.cells[2,nCols]].Select; //选择该列
    //Excelid.selection.HorizontalAlignment := $FFFFEFF4;                               //居中
    Excelid.selection.MergeCells := True;
    //表体(包括表头)
    nCurrRow := 3;
end;

for i:=0 to mGrid.RowCount-1 do begin
    nCurrCol := 1;
    for j:=0 to mGrid.ColCount-1 do begin
      if mGrid.ColWidths[j]>0 then begin
        if pos(','+inttostr(j)+',', ','+pStrCols+',')<>0 then begin //导出为字符串格式
          Sheet.Cells[nCurrRow,nCurrCol].NumberFormatLocal := '@';
          Sheet.Cells[nCurrRow,nCurrCol] := mGrid.Cells[j,i];
        end else begin
          Sheet.Cells[nCurrRow,nCurrCol] := mGrid.Cells[j,i];
        end;
        nCurrCol := nCurrCol + 1;
      end;
    end;
    nCurrRow := nCurrRow + 1;
end;
//表尾文字
Sheet.Cells[nCurrRow,1] := pTail;
sheet.range[sheet.cells[nCurrRow, 1],sheet.cells[nCurrRow,nCols]].Select; //选择该列
Excelid.selection.HorizontalAlignment := $FFFFEFF4;                               //居中
Excelid.selection.MergeCells := True;
try
    sheet.cells[1,1].Select;
    Excelid.Workbooks[1].SaveAs(FileName);
    Excelid.Workbooks[1].close;
    Excelid.Quit;
except
    Excelid.Quit;        //有时写完后立即退出,但写进程还占用着该文件,不允许退出,所以这里再退出一次
end;                   //实际上就是设一点点延迟,
Excelid := Unassigned;
end;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DelphiPost登陆Delphi盒子论坛源码发布时间:2022-07-18
下一篇:
delphi.指针.PChar发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap