如何自动将表中的数据传输到Excel或WORD的文件中

libamboo 2002-04-14 06:32:15
在BCB中用Query查询出某表中符合条件的记录后,如何自动将表中的数据传输到Excel或WORD的文件中,Excel和word文件在数据传过来的时候自动建立
...全文
338 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangdie 2002-05-14
  • 打赏
  • 举报
回复

收藏
houzy123 2002-04-22
  • 打赏
  • 举报
回复
CB6我没用过,我用的是cb5,我现在没cb6,也不能测试代码,sorry
chenjianboqq 2002-04-21
  • 打赏
  • 举报
回复
以下的程序可以在cb6运行





//execel输出

//定义对象
Variant excel_app; //excel applicatian对象
Variant excel_book; //book
Variant excel_sheet; //工作簿
Variant excel_range; //范围
Variant my_worksheet; //当前页
Variant my_range; //当前范围
///***************输出****************
excel_app=Variant::CreateObject("excel.application"); //创建app对象
excel_book=excel_app.OlePropertyGet("workbooks"); //创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=excel_book.OleFunction("add"); //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
excel_app.OlePropertySet("Visible",(Variant)true); //可见
//int j(1);
PropertyGet Range("Range");
PropertySet SetValue("Value");
PropertySet SetFormula("Formula");
PropertyGet GetValue("Value");
PropertyGet GetFormula("Formula");
AnsiString mytmp;
for(int j=1;j<=Table1->FieldCount;++j)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=String(char(64+j))+IntToStr(1);
//Range.ClearArgs();
// SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << mytmp );
my_range.Exec(SetValue << Table1->Fields->Fields[j-1]->FieldName);
Range.ClearArgs();
}
Table1->Bof;
for (int i=2;i<=Table1->RecordCount+1;++i)
{

for(int j=1;j<=Table1->FieldCount;++j)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=String(char(64+j))+IntToStr(i);
//Range.ClearArgs();
//SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << mytmp );
my_range.Exec(SetValue << Table1->Fields->Fields[j-1]->AsString);
Range.ClearArgs();
}
Table1->Next();
}
}
libamboo 2002-04-20
  • 打赏
  • 举报
回复
:)老兄,我真不是向你想象的那么聪明,简直太笨,我不会用f1book,也很少用adoquery控件,多数用query控件,能告诉f1book的用法吗
caijitao 2002-04-20
  • 打赏
  • 举报
回复
:)老兄,会用f1book吧。将adoquery的值利用循环可以非常方便的写入,而后再savetofile,:)呵呵,百试不爽。而且性能可比excelserver强啦。
chenjianboqq 2002-04-20
  • 打赏
  • 举报
回复
楼上的朋友为什么你的程序在CB5可以而在CB6却出现类字
符串无效的提示呢急盼回答!!
houzy123 2002-04-20
  • 打赏
  • 举报
回复
我有将table转到excel的原代码,word参考bcb例子即可
#include "ComObj.hpp"
#include <utilcls.h>
....
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
AnsiString filenamex=SaveDialog1->FileName+".xls";
Variant Wb,Sh1;
Wb=CreateOleObject("Excel.Sheet");
int i=0;
int iFieldCount=Table1->FieldCount;
AnsiString cc;
Sh1=Wb.OlePropertyGet("ActiveSheet");
for(int j=0;j<iFieldCount;j++)
{
cc=Table1->Fields->Fields[j]->FieldName;
Sh1.OlePropertyGet("Cells",1,j+1).OlePropertySet("Value",cc);
}
i=1;
Table1->First();

while (!Table1->Eof)
{
i++;
for(int j=0;j<iFieldCount;j++)
{
cc=Table1->Fields->Fields[j]->AsString;
Sh1.OlePropertyGet("Cells",i,j+1).OlePropertySet("Value",cc);
}
Table1->Next();
}
Wb.OleProcedure("SaveAs",filenamex);
Wb.OleProcedure("Close");
}
}
以上代码在bcb5测试通过
libamboo 2002-04-19
  • 打赏
  • 举报
回复
zken:你好?

我试了您这次给我的代码是我想象的那样,但是我最终想要也是要与数据库相连接,我用Query查询从Table中得到的数据输出到WORD 或 EXECL中,按着你第一次给的代码行吗?请您指教,越快越好,非常感谢!
zken 2002-04-19
  • 打赏
  • 举报
回复
不好意思,我的程序用了数据库,你没必要下边是一个例子,你new一个项目,然后再加上两个按钮就可以用了,记得包含文件#include <utilcls.h> 上次我忘了说了。

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <utilcls.h>
#include "wordtable.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//******************word输出×*********
//******************定义变量***********
Variant word_app; //application
Variant word_docs; //文档对象
Variant word_range; //range对象
Variant word_tables; //tables对象
Variant word_table; //table对象
Variant active_doc; //当前文档
Variant the_cell; //单元格对象
Variant cell_range; //单元格的范围
AnsiString showme; //要显示的东西
int cell_width ; //表格宽度
long cell_long ; //表格长度
//**************开始操作××××××××
word_app=Variant::CreateObject("Word.application"); //创建word。APPLICATION对象
word_docs=word_app.OlePropertyGet("documents"); //获得文档对象
word_docs.OleProcedure("add"); //添加新文档
word_app.OlePropertySet("Visible",true); //可见
active_doc=word_app.OlePropertyGet("ActiveDocument"); //当前文档
word_range=active_doc.OleFunction("Range"); //range对象
word_tables=word_range.OlePropertyGet("Tables"); //得到table对象
//word_table=word_tables.OleFunction("Add",word_range,100,10);
cell_width=6; //得到宽度
cell_long=8;
//*****画表格
word_table=word_tables.OleFunction("Add",word_range,cell_long,cell_width);
int j=1;
Procedure InsertAfter("InsertAfter");
for (int i=1;i<6 ;++i)
{
for(int j=1;j<4;++j)
{
the_cell=word_table.OleFunction("cell",j,i);
cell_range=the_cell.OlePropertyGet("range");
InsertAfter.ClearArgs();
cell_range.Exec(InsertAfter<<"easy");
}

}


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
//execel输出
//定义对象
Variant excel_app; //excel applicatian对象
Variant excel_book; //book
Variant excel_sheet; //工作步
Variant excel_range;
Variant my_worksheet; //当前页
Variant my_range; //当前范围


///***************输处****************
excel_app=Variant::CreateObject("excel.application"); //创建app对象
excel_book=excel_app.OlePropertyGet("workbooks"); //创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=excel_book.OleFunction("add"); //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
excel_app.OlePropertySet("Visible",(Variant)true); //可见
int j(1);
PropertyGet Range("Range");
PropertySet SetValue("Value");
PropertySet SetFormula("Formula");
PropertyGet GetValue("Value");
PropertyGet GetFormula("Formula");
AnsiString mytmp;
for (int i=1;i<5;++i)
{

for(int j=1;j<8;++j)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=String(char(64+i))+IntToStr(j);
Range.ClearArgs();
SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << mytmp );
my_range.Exec(SetValue << "excel");
Range.ClearArgs();
}


}

}
//---------------------------------------------------------------------------
WeiXuBin 2002-04-18
  • 打赏
  • 举报
回复
你们可以把5张图片放在WOrd中吗?高难度哦!
libamboo 2002-04-17
  • 打赏
  • 举报
回复
若将某个固定表中的数据用Query查询直接输出到word或excel中与由某个固定表中的数据直接输出到word或excel中有何不同,请指教,我非常急需
libamboo 2002-04-17
  • 打赏
  • 举报
回复
zken(乱码) :您好 ,我这两天看了您的解答代码,我在BCB下运行了一下,编译时出现了几个错误,我只运行了第二部分excel,我将代码和错误写出,请您在给我修改一下,我急想得到正确的答案。

在您给的代码基础上该了一点:
Variant excel_app; //excel applicatian对象
Variant excel_book; //book
Variant excel_sheet; //工作步
Variant excel_range;
Variant my_worksheet; //当前页
Variant my_range; //当前范围


///***************输入****************
excel_app=Variant::CreateObject("excel.application"); //创建app对象
excel_book=excel_app.OlePropertyGet("workbooks"); //创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=excel_book.OleFunction("add"); //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
excel_app.OlePropertySet("Visible",(Variant)true); //可见
int j(1);
// ADOQuery1->Bof;
Table1->Bof;
PropertyGet Range("Range");
PropertySet SetValue("Value");
PropertySet SetFormula("Formula");
PropertyGet GetValue("Value");
PropertyGet GetFormula("Formula");
AnsiString mytmp;
while (!Table1->Eof)
{
int i;
// for(int i(1);i<=check_count;++i)
for(i=0;i<Table1->FieldCount;i++)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=IntToStr(i)+IntToStr(j);
//excel_range=excel_sheet_now.OlePropertyGet(Range << "A1");
//excel_range.Exec(SetValue << "fdFDFD");
mytmp=Intstrexcel(j,i);
Range.ClearArgs();
SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << Intstrexcel(j,i) );
my_range.Exec(SetValue << Table1->Fields->Fields[i-1]->AsString.Trim());
Range.ClearArgs();
}
Table1->Next();
++j;
}


错误信息:
[C++ Error] Unit1.cpp(120): E2268 Call to undefined function 'Intstrexcel'
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
[C++ Error] sysvari.h(2561): E2449 Size of 'args' is unknown or zero
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
[C++ Error] sysvari.h(2562): E2094 'operator+' not implemented in type 'TAutoArgs<1>' for arguments of type 'int'
[C++ Error] sysvari.h(2563): E2450 Undefined structure 'TAutoArgs<1>'
[C++ Error] sysvari.h(2563): E2285 Could not find a match for 'Variant::OlePropertySet(const AnsiString,undefined)'
表的数据源也设置好了

TR@SOE 2002-04-16
  • 打赏
  • 举报
回复
www.trackon.net/bcblist/resource.asp里有两篇文章可以参考。
libamboo 2002-04-16
  • 打赏
  • 举报
回复
zken(乱码) 你好?我还想请教一下,你用的ADOQuery和通过ODBC数据源连接的Query的用法一样吗?我是一个初学,里面还有很多不明白的,你定义的变量不用在头文件中写吗,
zken 2002-04-15
  • 打赏
  • 举报
回复
下边是我的程序中的一部分,你应该能用的上,别忘了给分!
//开始输出
//1)word输出
if (out_type==1 )
{
//******************word输出×*********
//******************定义变量***********
Variant word_app; //application
Variant word_docs; //文档对象
Variant word_range; //range对象
Variant word_tables; //tables对象
Variant word_table; //table对象
Variant active_doc; //当前文档
Variant the_cell; //单元格对象
Variant cell_range; //单元格的范围
AnsiString showme; //要显示的东西
int cell_width ; //表格宽度
long cell_long ; //表格长度
//**************开始操作××××××××
word_app=Variant::CreateObject("Word.application"); //创建word。APPLICATION对象
word_docs=word_app.OlePropertyGet("documents"); //获得文档对象
word_docs.OleProcedure("add"); //添加新文档
word_app.OlePropertySet("Visible",true); //可见
active_doc=word_app.OlePropertyGet("ActiveDocument"); //当前文档
word_range=active_doc.OleFunction("Range"); //range对象
word_tables=word_range.OlePropertyGet("Tables"); //得到table对象
//word_table=word_tables.OleFunction("Add",word_range,100,10);
cell_width=check_count; //得到宽度
cell_long=ADOQuery1->RecordCount;
//*****画表格
word_table=word_tables.OleFunction("Add",word_range,cell_long,cell_width);
int j=1;
Procedure InsertAfter("InsertAfter");
while (!ADOQuery1->Eof)
{

for(int i(1);i<=cell_width;++i)
{
the_cell=word_table.OleFunction("cell",j,i);
cell_range=the_cell.OlePropertyGet("range");
cell_range.OleProcedure("insertbefore",ADOQuery1->Fields->Fields[i-1]->AsString.Trim());
//cell_range.OleProcedure("insertafter","a");
}
ADOQuery1->Next();
++j;
}
}

if (out_type==2)
{ //execel输出
//定义对象
Variant excel_app; //excel applicatian对象
Variant excel_book; //book
Variant excel_sheet; //工作步
Variant excel_range;
Variant my_worksheet; //当前页
Variant my_range; //当前范围


///***************输入****************
excel_app=Variant::CreateObject("excel.application"); //创建app对象
excel_book=excel_app.OlePropertyGet("workbooks"); //创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=excel_book.OleFunction("add"); //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
excel_app.OlePropertySet("Visible",(Variant)true); //可见
int j(1);
ADOQuery1->Bof;
PropertyGet Range("Range");
PropertySet SetValue("Value");
PropertySet SetFormula("Formula");
PropertyGet GetValue("Value");
PropertyGet GetFormula("Formula");
AnsiString mytmp;
while (!ADOQuery1->Eof)
{

for(int i(1);i<=check_count;++i)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=IntToStr(i)+IntToStr(j);
//excel_range=excel_sheet_now.OlePropertyGet(Range << "A1");
//excel_range.Exec(SetValue << "fdFDFD");
mytmp=Intstrexcel(j,i);
Range.ClearArgs();
SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << Intstrexcel(j,i) );
my_range.Exec(SetValue << ADOQuery1->Fields->Fields[i-1]->AsString.Trim());
Range.ClearArgs();
}
ADOQuery1->Next();
++j;
}
}
leonpard 2002-04-14
  • 打赏
  • 举报
回复
关注
libamboo 2002-04-14
  • 打赏
  • 举报
回复
在那里能找到
binbin 2002-04-14
  • 打赏
  • 举报
回复
有专门的控件。
mxExports
libamboo 2002-04-14
  • 打赏
  • 举报
回复
我用的是SQLServer 和DBF表,请大家帮忙多谢了

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧