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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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;j<PhoneADOQuery->FieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->FieldName;      Sh.OlePropertyGet("Cells", 1, j+1).OlePropertySet("Value", Datatem.c_str());   }   PhoneADOQuery->First();   for (i=0; i<PhoneADOQuery->RecordCount; i++)   {   for (j=0;j<PhoneADOQuery->FieldCount;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; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值