DBGrid输出excel表


void TfmSalesCategory::DBGrid2Excel(TDBGrid* & DBG, AnsiString strXlsFile)

{
//TODO: Add your source code here
    if(!DBG->DataSource->DataSet->Active) // 数据集没有打开就返回
        return;
    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
                "btExcelClick", MB_OK | MB_ICONERROR);
        return;
    }
    // 隐藏Excel界面
    vExcelApp.OlePropertySet("Visible", true);
    // 新建一个工作表
    vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
    // 操作这个工作薄
    vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OlePropertyGet("Sheets", 1);
    // 设置Excel文档的字体
    vSheet.OleProcedure("Select");
    vSheet.OlePropertyGet("Cells").OleProcedure("Select");
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Size", DBG->Font->Size);
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Name", DBG->Font->Name.c_str());
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("FontStyle", "常规");
    vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
    // 表格的行数
    int nRowCount(DBG->DataSource->DataSet->RecordCount + 1);
    nRowCount = nRowCount < 2?2: nRowCount;
    // 表格的列数
    int nColCount(DBG->Columns->Count);
    nColCount = nColCount < 1?1: nColCount;
    // 设置单元格的宽度
    for(int i=0; i<nColCount; i++)
    {
        int nColWidth = DBG->Columns->Items[i]->Width;
        vExcelApp.OlePropertyGet("Columns", i + 1)
                .OlePropertySet("ColumnWidth", nColWidth / 7);
    }
    // 先将列名写入Excel表格
    for(int j=0; j<DBG->Columns->Count; j++)
    {
        // 标题行的行高
        vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
        vSheet.OlePropertyGet("Cells", 1, j + 1).OlePropertySet("Value",
                DBG->Columns->Items[j]->Title->Caption.c_str());
        // 设置列名单元格的背景色
        Variant vInter = vSheet.OlePropertyGet(
                "Cells", 1, j + 1).OlePropertyGet("Interior");
        vInter.OlePropertySet("ColorIndex", 15); // 灰色
        vInter.OlePropertySet("Pattern", 1); // xlSolid
        vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
    }
    // 将DBGrid中的数据写入Excel表格
    DBG->DataSource->DataSet->First();
    for(int i=0; i<nRowCount; i++)
    {
        // 普通数据行的行高16
        vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
        for(int j=0; j<DBG->Columns->Count; j++)
        {
            vSheet.OlePropertyGet("Cells", i + 2, j + 1).OlePropertySet("Value",
                DBG->DataSource->DataSet->FieldByName(
                DBG->Columns->Items[j]->FieldName)->AsString.c_str());
        }
        DBG->DataSource->DataSet->Next();
    }
    // 保存Excel文档并退出
    vExcelApp.OlePropertyGet("ActiveWorkbook")
        .OleFunction("SaveAs", strXlsFile.c_str());
    vExcelApp.OleFunction("Quit");
    vSheet = Unassigned;
    vExcelApp = Unassigned;
    // 工作结束
    MessageBox(0, "成功导出!",
            "DBGrid2Excel", MB_OK | MB_ICONINFORMATION);
}
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、付费专栏及课程。

余额充值