| « | January 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:21 评论数量:94 留言数量:1 访问次数:145888 建立时间:2006年1月9日 |

| |
|
[转帖]如何用 ADO 方式把数据写到 Excel 表中,以及如何从 Excel 表中读出数据? 文章收藏
joinclear 发表于 2006/1/13 9:04:33 |
| 转载自: http://dev.hackbase.com/hackbase11/how303381.htm
如何用 ADO 方式把数据写到 Excel 表中,以及如何从 Excel 表中读出数据?
另外,在写 Excel 表格时,如何控制单元格的格式,如绘制边框线,水平居中,字体等?
回复人: lanren_me(阿波) ( ) 信誉:115
2003-05-29 12:34:43Z
得分:25
?
要在应用程序中控制Excel2000的运行,首先必须在编制自动化客户程序时使其头文件要包含Comobj.hpp和Utilcls.h。
即:#include<Comobj.hpp>
#include<Utilcls.h>
C++ Builder开发者把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。
设置对象属性:Variant OlePropertySet(属性名,参数……);
获得对象属性:void OlePropertyGet(属性名,参数……);
调用对象方法:1) Variant OleFunction(函数名,参数……);
2) void OleProcedure(过程名,参数……);
C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。
1、Excel中常用的对象是:Application,Workbooks,Worksheets等。
(1) 创建应用对象:如:
Variant ex;
ex=Variant::CreateObject ("Excel.Application");
或者 ex=CreateOleObject ("Excel.Application");
(2) 创建工作簿对象:
Variant wb;
wb=ex.OlePropertyGet("ActiveWorkBook");
(3) 创建工作表对象:
Variant sheet;
sheet=wb.OlePropertyGet("ActiveSheet");
(4) 创建区域对象:
Variant range;
range=sheet.OlePropertyGet("Range","A1:A10");
2、常用的属性操作:
(1)新建EXCEL文件:
(a):新建系统模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add") //默认工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add",1) //单工作表
ex.OlePropertyGet("workbooks").OleFunction("Add",2) //图表
ex.OlePropertyGet("workbooks").OleFunction("Add",3) //宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",4) //国际通用宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",5) //与默认的相同
ex.OlePropertyGet("workbooks").OleFunction("Add",6) //工作簿且只有一个表
(b):新建自己创建的模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add","C:\\WINDOWS\\Profiles\\test2\\Application Data\\Microsoft\\Templates\\result.xlt"); // 后面写上模板的完全路径,注意“\\”
(2)打开工作簿:
ex.OlePropertyGet("workbooks").OleFunction("open","路径名.xls")
(3)保存工作簿:
wb.OleFunction("Save"); //表格保存
wb..OleFunction("SaveAs","文件名"); //表格保存为,文件路径注意用“\\”
(4)退出EXCEL:
ex.OleFunction ("Quit");
(5)设置字体:
(a):设置单元格字体
sheet.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Name","隶书");
sheet.OlePropertyGet("Cells",2,3).OlePropertyGet("Font").OlePropertySet("size",28);
(b):设置所选区域字体
range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("size",28); range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Color",
RGB(0,0,255));
其中参数的设置:
Font---Name : “隶书” //字体名称
----Size : 12 //字体大小
----Color : RGB(*,*,*) //颜色
-----Underline : true/false //下划线
-----Italic: true/false //斜体
(6)单元格的合并:
(a) range1=sheet.OlePropertyGet("Range", "A1:A2"); //A1和A2单元格合并
(b) AnsiString Str="A"+IntToStr(j)+":"+"C"+IntToStr(j);
range1=sheet.OlePropertyGet("Range",Str); //可以用变量控制单元格合并
range1.OleFunction("Merge" , false);
(7)赋值语句:
(a):指定单元格赋值
sheet.OlePropertyGet("Cells",3,6).OlePropertySet("Value",str);
sheet.OlePropertyGet("Cells",j,1).OlePropertySet("Value","共查到记录:"+IntToStr(j-6));
(b):所选区域单元格赋值
range.OlePropertyGet("Cells").OlePropertySet("Value",10);
(c):所选区域行赋值
range.OlePropertyGet("Rows",1).OlePropertySet("Value",1234);
(d):工作表列赋值
sheet.OlePropertyGet("Columns",1).OlePropertySet("Value",1234);
(8)取值语句:
AnsiString abc=sheet.OlePropertyGet("Cells",120,1).OlePropertyGet("Value");
(9)区域选择:
range.OlePropertyGet("Cells").OleFunction("Select");
(10)窗口属性:
(a)显示属性
ex.OlePropertySet("Windowstate",3); //最大化显示
参数 1---------xlNormal //正常显示
2---------xlMinimized //最小化显示
3---------xlMaximized //最大化显示
(b)状态栏属性
ex.OlePropertySet ("StatusBar","您好,请您稍等。正在查询!");
ex.OlePropertySet ("StatusBar", false); //还原成默认值
(c)标题属性:
ex.OlePropertySet("Caption","查询系统");
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant my_excel,all_workbooks,my_workbook,my_worksheet;
Variant my_range;
AnsiString columname,residue,cellname,rangename;
int i,j=1;
int count;
try
{
my_excel=CreateOleObject("excel.Application");//启动Excel
}
catch(...)
{
MessageBox(GetFocus(),"无法启动Excel","警告",MB_OK|MB_ICONSTOP);
Abort();
}
all_workbooks=my_excel.OlePropertyGet("WorkBooks");
Procedure Open("Open");
my_excel.OlePropertySet("Visible",(Variant)true); //使Excel启动后可见
my_workbook=all_workbooks.OleFunction("Add");//新建一个工作薄
my_workbook=my_excel.OlePropertyGet("ActiveWorkbook");
my_worksheet=my_workbook.OlePropertyGet("ActiveSheet");
my_worksheet.OlePropertyGet("Rows",2).OleProcedure("Insert");//插入一行
//根据DBGrid的表头输出Excel表头
for(i=0;i<DBGridEh1->FieldCount;++i)
{
my_excel.OlePropertyGet("Cells",1,(i+1)).OlePropertySet("Value",DBGridEh1->Columns->Items[i]->Title->Caption.c_str());
}
//输出表体
ADOQuery1->DisableControls();
ADOQuery1->First();
for(j=2;!ADOQuery1->Eof;++j)
{
for(i=0;i<DBGridEh1->FieldCount;++i)
{
//向表格添加数据
my_excel.OlePropertyGet("Cells",j,(i+1)).OlePropertySet("Value",DBGridEh1->Columns->Items[i]->Field->AsString.c_str());
}
ADOQuery1->Next();
}
ADOQuery1->EnableControls();
my_worksheet.OlePropertyGet("Range","A1:D3").OlePropertyGet("Interior").OlePropertySet("ColorIndex",15); //改表格颜色
// my_worksheet.OlePropertyGet("Range","A1:A3").OleProcedure("Merge",0); //合并单元格
my_worksheet.OlePropertyGet("Range","A1:D1").OlePropertyGet("Columns").OleProcedure("autofit"); //设置自适应列宽
// my_worksheet.OlePropertyGet("Range","A1:D3").OleProcedure("Merge",1);
my_worksheet.OlePropertyGet("Range","A1:D3").OlePropertySet("HorizontalAlignment",3); //居中
MessageBox(this->Handle,"请按确定彻底关闭Excel","通知",MB_OK|MB_ICONEXCLAMATION|MB_APPLMODAL);
my_excel.OleFunction("Quit");
}
Top
回复人: jukyy(做一大水雷把鬼子炸沉) ( ) 信誉:90
2003-05-29 13:03:16Z
得分:25
?
高人呀,学习。我这有一个向Excel里写数据的函数,事先把数据导入到一个ADOTable中,然后调用
DataSetToExcel(ADOTable1,true);
//以下是往Excel里写数据
void DataSetToExcel(TDataSet* DataSet,bool H)
{
Variant Ex,Wbs,Wb,Sh1;
int CurrRow,CurrCol;
CurrCol=0;
CurrRow=0;
Screen->Cursor=crHourGlass;
if(Ex.IsEmpty())
try
{
Ex=Variant::CreateObject("Excel.Application");
}
catch(...)
{
Screen->Cursor=crDefault;
Application->MessageBoxA("打开Excel出错,请确认你已经正确安装了Microsoft Office2000!","违章车辆照片管理系统",64);
return;
}
try
{
if(Ex.PG("ActiveWorkBook").IsEmpty())
Ex.PG("WorkBooks").PR("ADD");
if(Wb.IsEmpty())
Wb=Ex.PG("ActiveWorkBook");
Sh1=Wb.PG("Sheets").FN("Add");
Ex.OlePropertySet("Visible",true);
}
catch(...)
{
Ex=Ex.Empty();
Wb=Wb.Empty();
DataSetToExcel(DataSet,H);
}
if(H) //PR=OleProcedure,PG=OlePropertyGet,PS=OlePropertySet
{
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrCol++;
Sh1.PG("Cells",1,CurrCol).PS("Value",DataSet->Fields->Fields[j]->FieldName);
}
}
DataSet->First();
for (int i=0;i<DataSet->RecordCount;i++)
{
CurrCol=0;
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrCol++;
Sh1.PG("Cells",i+2,CurrCol).PS("Value",DataSet->Fields->Fields[j]->AsString);
}
}
DataSet->Next();
}
}
else
{
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrRow++ ;
Sh1.PG("Cells",CurrRow,1).PS("Value",DataSet->Fields->Fields[j]->FieldName);
DataSet->First();
for (int i=0;i<DataSet->RecordCount;i++)
{
Sh1.PG("Cells",CurrRow,i+2).PS("Value",DataSet->Fields->Fields[j]->AsString);
DataSet->Next();
}
}
}
}
Screen->Cursor=crDefault;
}
|
|
|