求救....如何把数据表里符合条件的记录.备分到硬盘上(execl),还有还原数据表.谢谢!!
求救....如何把数据表里符合条件的记录.备分到硬盘上(execl)还有还原数据表.谢谢!! 问题点数:20、回复次数:8Top
1 楼sunliwen780502(孙立文)回复于 2004-09-03 10:26:21 得分 2
没作过符合条件的记录导到Excel,只作过导全部的记录.
帮你顶!!!Top
2 楼specialkid(异度行星)回复于 2004-09-03 11:33:59 得分 0
好的Top
3 楼sunliwen780502(孙立文)回复于 2004-09-05 18:59:50 得分 7
你自己试着修改一下吧.
/*******************************************************************************
* 函数名: ExportDataToExcel
* 输入参数: ado //pointer of TADOQuery
* dia //pointer of TSaveDialog
* grid //pointer of TDBGrid
* k //y coordiate of cells
* Str //title of Excel table
* 输出参数: None
* 返回值: void
* 描述:
*******************************************************************************/
void ExportDataToExcel(TADOQuery *ado, TSaveDialog *dia, TDBGrid *grid, int k, char* Str)
{
Variant ex, newxls, cellms;
AnsiString sFileName;
int row, i, j = 1;
if (dia->Execute())
{
try
{
ex = CreateOleObject("Excel.Application");//启动Excel
ex.OlePropertySet("Visible", (Variant)false);//使Excel不可见
newxls = (ex.OlePropertyGet("Workbooks")).OleFunction("Add");//添加一个工作薄
cellms = newxls.OlePropertyGet("ActiveSheet"); //创建工作区
}
catch (...)
{
ShowMessage("无法启动Excel");
return;
}
ado->Active = true;
ado->First();
row = 1;//在第一行显示标题
//ex.OlePropertyGet("Cells", 1, 6).OlePropertySet("Value", );
ex.OlePropertyGet("Cells", 1, k).OlePropertySet("Value", Str);
cellms = ex.Exec(PropertyGet("Range") << ("A" + IntToStr(row) + ":A" + IntToStr(row)));
for (i = 0; i < ado->FieldCount; i++)
{
//将字段名写到工作薄的第二行
cellms.OlePropertyGet("Cells", 2, i + 1).OlePropertySet("Value", (WideString)grid->Columns->Items[i]->Title->Caption.c_str());
//cellms.OlePropertyGet("Cells", 2, i + 1).OlePropertySet("Value", (WideString)ado->Fields->Fields[i]->FieldName);
}
while (!ado->Eof)//将数据库中的记录依次写到Excel中
{
j = j + 1;
for (i = 0; i < ado->FieldCount; i++)
{
cellms.OlePropertyGet("Cells", j + 1, i + 1).OlePropertySet("Value",(WideString)ado->Fields->Fields[i]->AsString);
}
ado->Next();
}
sFileName = dia->FileName;
newxls.OleProcedure("SaveAs", sFileName.c_str()); //保存Excel文件
ex.OleFunction("Quit");//退出Excel
ex = Unassigned;
newxls = Unassigned;
cellms = Unassigned;
}
}
Top
4 楼specialkid(异度行星)回复于 2004-09-07 09:15:06 得分 0
此函数CreateOleObject运行出错
[C++ Error] Unit1.cpp(53): E2268 Call to undefined function 'CreateOleObject'Top
5 楼befree(似有似无)回复于 2004-09-07 09:55:25 得分 2
备分和导出不一样的
CreateOleObject是在ComObj里面
#include<ComObj.hpp>Top
6 楼lynchzlm(gigi)回复于 2004-09-07 10:36:54 得分 1
缺少头文件,添上#include "ComObj.hpp"Top
7 楼lynchzlm(gigi)回复于 2004-09-07 10:43:57 得分 1
补充一下,既然能够导数据表中所有的记录,那么符合条件的也好办了.
很多方法:1/查询合适条件的记录先暂存在一个数据表中,然后同上面
2/或者在导出的时候边判断边导
最后,在上面函数所有跟ole有关的函数调用的最开始,加上CoInitialize(NULL),同时包括头文件#include"objbase.h",这样就ok了Top
8 楼lonefox(lonefox)回复于 2004-09-09 17:04:20 得分 7
我最近写的,供参考
Variant ExcelApp, Workbook, Sheet;
try {
try {
ExcelApp = Variant::CreateObject("Excel.Application");
}catch(...) {
ShowMessage("无法启动Excel,请确认安装了Office!");
return;
}
try {
ADOConnection1->ConnectionString = "FILE NAME=" + ExtractFilePath(Application->ExeName) + "sql.UDL";
ADOConnection1->LoginPrompt = false;
ADOConnection1->Open();
ADOConnection1->BeginTrans();
}catch(...) {
ShowMessage("无法连接数据库!");
ExcelApp.FN("Quit");
ExcelApp = Unassigned;
return;
}
try {
ExcelApp.PG("workbooks").OleFunction("open", edtFile->Text.c_str());
Workbook = ExcelApp.PG("ActiveWorkBook");
Sheet = Workbook.PG("ActiveSheet");
//int cols = Sheet.PG("UsedRange").PG("Columns").PG("Count");
int rows = Sheet.PG("UsedRange").PG("Rows").PG("Count");
ADOTable1->Connection = ADOConnection1;
ADOTable1->Active = false;
ADOTable1->CacheSize = 1000;
ADOTable1->TableName = "RS";
ADOTable1->Active = true;
int rc = ADOTable1->RecordCount;
ProgressBar1->Min = 0;
ProgressBar1->Max = rows-1;
for(int i = 2; i <= rows; i++) {
ADOTable1->Append();
ADOTable1->FieldValues["no"] = IntToStr(10000 + rc + i);
ADOTable1->FieldValues["name"] = Sheet.PG("Cells",i,1).PG("Value");
.
.
.
Label2->Caption = "第" + IntToStr(i-1) + "条记录";
ProgressBar1->Position = i-1;
Application->ProcessMessages();
}
ADOTable1->Post();
ADOConnection1->CommitTrans();
ShowMessage("成功导入!");
}catch(...) {
ADOConnection1->RollbackTrans();
ProgressBar1->Position = 0;
ShowMessage("导入失败!");
}
}__finally {
ADOTable1->Active = false;
ADOConnection1->Close();
ExcelApp.FN("Quit");
Sheet = Unassigned;
Workbook = Unassigned;
ExcelApp = Unassigned;
}Top




