DBGrid数据导出到Excel表格中

//DBGrid数据导出到Excel表格
void __fastcall TMDIChild::GridToExcel(TDataSet *DataSet,AnsiString titleB,int n)  //int n指示最后n列不用处理
{ //要导出数据的DBGrid/导出到Excel中的标题

  //导出到Excel开始
  Variant excelApp,workBook,aft,sheetA,range,bef;
  //检查机器是否安装了Office
  try{
    //打开excel应用程序
    excelApp = CreateOleObject("Excel.Application");
  }
  catch(...)
  {
    ShowMessage("运行Excel出错,请检查本机是否安装了Office!");
    return;
  }
  excelApp.OPS("Visible",true); //使应用程序可见
  //建立一个新的excel文件
  workBook = excelApp.OPG("WorkBooks").OFN("Add");
  workBook = excelApp.OPG("ActiveWorkBook");//打开工作簿
  //显示存在的sheet的数目
  int count = workBook.OPG("sheets").OPG("count");
  aft = workBook.OPG("sheets",count);
  //当前sheet赋值给sheetA
  sheetA = workBook.OPG("ActiveSheet"); 
//表一的标题***************
  //合并单元格A1到M1,并将A1:M1区域赋值给range
  range = sheetA.OPG("Range","A1:M1");
  range.OFN("Merge",false);//合并用false,拆分用true
  //向单元格中填写内容
  sheetA.OPG("Cells",1,1).OPS("Value",titleB.c_str());
  //设置区域字体
  range.OPG("Cells").OPG("Font").OPS("Name","宋体");
  //设置区域字号大小
  range.OPG("Cells").OPG("Font").OPS("Size",18);
  //设置垂直对齐方式-居中
  range.OPG("Cells").OPS("VerticalAlignment",2);
  //设置水平对齐方式-居中
  range.OPG("Cells").OPS("HorizontalAlignment",3);
//表一的标题结束*************
//表一的表头****************
  for(int i=0;i<DataSet->Fields->Count-n;i++)
  {
    String str = DataSet->Fields->Fields[i]->DisplayLabel;
    //向单元格填写内容
    sheetA.OPG("Cells",2,i+1).OPS("Value",str.c_str());
    //设置边框     ,速度会较慢
    //sheetA.OPG("Cells",2,i+1).OPG("Borders",1).OPS("LineStyle",1);//左边框
    //sheetA.OPG("Cells",2,i+1).OPG("Borders",2).OPS("LineStyle",1);//右边框
    //sheetA.OPG("Cells",2,i+1).OPG("Borders",3).OPS("LineStyle",1);//上边框
    //sheetA.OPG("Cells",2,i+1).OPG("Borders",4).OPS("LineStyle",1);//下边框
    sheetA.OPG("Cells",2,i+1).OPS("WrapText",true);//设置自动换行
    sheetA.OPG("Cells",2,i+1).OPS("HorizontalAlignment",3);//设置水平居中
  }
//表一的表头结束************
//表一的数据开始************
  DataSet->First();
  for(int j=1;j<DataSet->RecordCount+1;j++)
  {
    for(int i=0;i<DataSet->Fields->Count-n;i++)
    {
      String strA = DataSet->Fields->Fields[i]->Text;
      sheetA.OPG("Cells",j+2,i+1).OPS("Value",strA.c_str());
      //加上下面的较慢
      //sheetA.OPG("Cells",j+2,i+1).OPG("Borders",1).OPS("LineStyle",1);//左边框
      //sheetA.OPG("Cells",j+2,i+1).OPG("Borders",2).OPS("LineStyle",1);//右边框
      //sheetA.OPG("Cells",j+2,i+1).OPG("Borders",3).OPS("LineStyle",1);//上边框
      //sheetA.OPG("Cells",j+2,i+1).OPG("Borders",4).OPS("LineStyle",1);//下边框
    }
    if (!DataSet->Eof)DataSet->Next();
  }     
//表一的数据结束***********
  //结束,如果没有如下代码,Excel线程直到应用程序退出才结束
  excelApp=Unassigned;
  workBook=Unassigned;
  aft=Unassigned;
  sheetA = Unassigned;
  range = Unassigned;
  ShowMessage(titleB+"导出成功!");
}

发布原因:在网上找了很久也没找到个好用的,不是慢,就是要依赖Excel(必须按装),又找呀找,才找到一个http://blog.youkuaiyun.com/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;
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; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值