DBGrid 写EXCEL 方法一

 

procedure Tmainform.SaveToExcelClick(Sender: TObject);
    var
     
      i,j,k:integer;
      xls:OleVariant;  
  begin

      //创建Excel对象, 需要在单元加上ComObj;
      try
          xls:=CreateOleObject('Excel.Application');
      except
          //showMessage(sMsgNotInstallExcel,sTitleError,1);
          exit;
      end;

      xls.WorkBooks.add;       //增加一个表
      xls.Visible:=true;       //可见  
      xls.Caption:='另存为EXCEL文件';

      //以下为设置EXCEL各列的宽度  
      //xls.Columns[1].ColumnWidth:=5;
    

      //表格标题为文件名称
      xls.Cells(1,1):='jjjjjjjjjjjjjjj';

      //画加框线范围A1:H1  1-左  2-右  3-顶 4-底 5-斜(/)6-斜(/)
      //for   k:=1   to   4   do
      //xls.Range['A1:H1'].Borders[k].Weight:=2   ;

      //xls.Cells(1,3):='0000';
      xls.Cells(2,1):='数据导出日期:'+DateToStr(now);
      //设置背景颜色
      xls.Range['A1:G1'].Interior.ColorIndex:= 37;  //37   为天蓝色

      //DBGrideh1标题栏写入excel标题
      for   i:=1   to   DBGrideh1.FieldCount   do
          begin
              xls.Cells[3,i].Value:=DBGrideh1.Columns[i-1].Title.Caption;
              xls.Cells[3,i].Interior.ColorIndex:=15;//15为灰色
              for k:=1 to 4 do
              xls.Cells[3,i].Borders[k].Weight:=2; //每个单元格加框线
          end;


    

       //从第4行开始循环写入DBGrideh1的数据--------------------------
      i:=4 ;
        with   DataModule1.ADOQuery_member   do  
          begin  
              first;     //第一条  
              while   not   eof   do  //i: 控制行
              begin
                for   j:=1   to   DBGrideh1.FieldCount   do  //j:控制列
                 begin
                  xls.cells(i,j):=FieldByName(DBGrideh1.Columns[j-1].FieldName).AsString   ;
                  for   k:=1   to   4   do    //单元格加框线
                      xls.Cells[i,j].Borders[k].Weight:=2   ;
                 end;
              inc(i);       //行号i递增+1
              next;         //while do 下一条数据
              end;  
          end;

      //保存EXCEL表格  
      //if   not   xls.ActiveWorkBook.Saved   then
      //     xls.ActiveWorkBook.SaveAs(fn);  
      //SetForegroundWindow(Application.Handle);   //本语句是防止软件窗体被其它窗体覆盖
      //MsgBox(sMsgExportSucceed,sTitleHint,0);
      //xls.quit;       //退出EXCEL程序
   
  end;

void __fastcall TPhoneForm::SelectButtonClick(TObject *Sender) {   AnsiString StrDate, ExName;//存放日期用于sheet   AnsiString Datatem,phone1="拨号";//临时存放数据库的字段值   int i,j;   //查询所需的数据   PhoneADOQuery->Close();   PhoneADOQuery->Parameters->ParamByName("date1")->Value=PhoneMaskEdit1->Text;   PhoneADOQuery->Parameters->ParamByName("date2")->Value=PhoneMaskEdit2->Text;   PhoneADOQuery->Active=true;   //新建一个EXCEL   Ex = Variant::CreateObject("Excel.Application");   Ex.OlePropertyGet("workbooks").OleFunction("Add", 6);   Wb = Ex.OlePropertyGet("ActiveWorkBook");   Sh = Wb.OlePropertyGet("ActiveSheet");   Ex.OlePropertySet("Visible", true);   //给sheet以日期重命名,   StrDate=DateToStr(Date());   Sh.OlePropertySet("Name", StrDate.c_str());   //给EXCEL输入数据   for (j=0;jFieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->FieldName;      Sh.OlePropertyGet("Cells", 1, j+1).OlePropertySet("Value", Datatem.c_str());   }   PhoneADOQuery->First();   for (i=0; iRecordCount; i++)   {   for (j=0;jFieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->AsString;      Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("Value", Datatem.c_str());      if (phone1==PhoneADOQuery->Fields->Fields[j]->FieldName)     {Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("NumberFormatLocal", "0_ ");//设置单元格格式为数值格式 }   }   PhoneADOQuery->Next();   }   //保存EXCEL并退出   ExName=GetCurrentDir()+"\\"+DateToStr(Date())+".xls";   Wb.OleFunction("SaveAs", ExName.c_str());   Wb.OleFunction("Close");   Ex.OleFunction ("Quit");   Ex = Unassigned; }
发布原因:在网上找了很久也没找到个好用的,不是慢,就是要依赖Excel(必须按装),又找呀找,才找到一个http://blog.csdn.net/xiangding/archive/2003/10/27/16918.aspx,但试了一下,不能用,不知是我用法不对还是本身有问题(由于没有给出使用示例),后来找到http://developer.51cto.com/art/200510/7494.htm这个不错,但他又让我装ehlid,不爽,所以对其做了修改,其它也就是去了个加页脚的地方,很爽,有进度条,导完后如果按安装了Excel则直接打开,如果没有的话就不管了,自已想办法打开吧!特点:一、不需安装Excel即可导出。二、有进度条。三、最多可以导出多少条,我没试,我试了5万条没有问题,时间也就是只需1-5分钟(我电脑较慢)。四、不需安装组件。使用方法(注:别忘了uses DBGridToExcel;):procedure TForm1.btn1Click(Sender: TObject);var DBGridToExcel: TDBGridToExcel; ExcelFileName: string;begin DBGridToExcel := TDBGridToExcel.Create(nil); try DBGridToExcel.TitleName := '入网车辆统计报表'; DBGridToExcel.BeginDate := '开始日期:2005-07-01'; DBGridToExcel.EndDate := '结束日期:2005-07-18'; DBGridToExcel.UserName := '系统管理员'; DBGridToExcel.DBGrid := dbgrd1; DBGridToExcel.ShowProgress := True; if ExcelFileName = '' then begin ExcelFileName := 'c:\1.xls'; with TSaveDialog.Create(nil) do begin Filter := 'Microsoft Excel xls文件|*.xls'; DefaultExt := 'xls'; if not Execute then Exit else ExcelFileName := FileName; end; end; DBGridToExcel.FileName := ExcelFileName; DBGridToExcel.Open := False; DBGridToExcel.ExportToExcel; finally DBGridToExcel.Free; end;end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值