//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include "Excel_2K_SRVR.h"
//#include "ComObj.hpp"
//#include<Utilcls.h>
//#pragma link "Excel_2K_SRVR"
#include <Sysutils.hpp> //打开EXCEL的头文件
#include <excel_2k.h> // 打开EXCEL的头文件
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str1="asvasd";//要入库的数据
Variant ex,newxls,sh;
try{
ex=CreateOleObject("Excel.Application");//启动Excel
ex.OlePropertySet("Visible",(Variant)true);//使Excel启动后可见
//ex.OlePropertySet("Visible",(Variant)false);//使Excel启动后不可见
//新建一新工作薄(加上这一句,会有两个Excel窗口,同时关闭)
//ex.OlePropertyGet("WorkBooks").OleProcedure("ADD");
//使用ExcelApp的Exec方法新建一有3个工作表的默认工作薄
// newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add");
//Excel启动后窗体状态:1(xlNormal)正常显示(Excel上次关闭时是什么状态,
//启动后就是什么状态),2(xlMinimized)最小化(不是缩小到任务栏),3(xlMaximized)最大化
ex.OlePropertySet("Windowstate",3);
//创建有单个工作表的工作簿
//newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",1);
//打开已存在的文件,使用时可将上面关于新建的那行屏蔽掉
newxls=ex.OlePropertyGet("workbooks").OleFunction("open", "D:\\我的文档\\C++学习例子\\操作EXCEL\\1.xls");
//选择工作表:
//选择第二工作表
//newxls.OlePropertyGet("Sheets", 2).OleProcedure("Select");
sh=newxls.OlePropertyGet("ActiveSheet");
//重命名工作表:
//sh.OlePropertySet("Name", "表名");//重命名当前工作表
//取得工作表总数:
//int nSheetCount=newxls.OlePropertyGet("Sheets").OlePropertyGet("Count");
//Edit1->Text=nSheetCount; //添加 Edit1组件显示数目
//新建工作表并重命名:
/* Variant bef1,aft1;
int count=ex.OlePropertyGet("sheets").OlePropertyGet("count");
aft1=ex.OlePropertyGet("sheets",count);
ex.OlePropertyGet("sheets").OleProcedure("Add",bef1.NoParam() , aft1);
sh = ex.OlePropertyGet("ActiveSheet");
sh.OlePropertySet("Name","增加的sheet的名字");//名字不能重复
*/
//给单元格赋值:
//sh.OlePropertySet("Cells","第 &P 頁,共 &N 頁");//给所有单元格赋值
sh.OlePropertyGet("Cells",1,1).OlePropertySet( "Value",str1.c_str());//给指定单元格赋值
//sh.OlePropertyGet("Rows",1).OlePropertySet("Value",1234);//给整个行的单元格赋值
//sh.OlePropertyGet("Columns",1).OlePropertySet("Value",1234);//给指定的整个列赋值
//从单元格中取值:
//AnsiString abc=sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Value");//取指定单元格的值
//Edit2->Text=abc; //添加 Edit2组件显示取值
//选择单元格:
//sh.OlePropertyGet("Cells").OleFunction("Select");//选择所有单元格
//sh.OlePropertyGet("Cells",3,4).OleFunction("Select");//选择指定单元格(指定的单元格获得焦点)
//区域操作:
//sh.OlePropertyGet("Range","A1:A10");//创建区域对象
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Cells").OlePropertySet("Value",10);//给指定区域的单元格赋值
//sh.OlePropertyGet("Range","A1:A10").OlePropertyGet("Cells").OleFunction("Select");//选中指定区域的单元格
//单元格中的数据计算:
//sh.OlePropertyGet("Cells",5,6).OlePropertySet("FormulaR1C1","=SUM(R[-3]C:R[-1]C)");//竖排加法运算
//sh.OlePropertyGet("Cells",2,9).OlePropertySet("FormulaR1C1","=SUM(RC[-3]:RC[-1])");//横向加法运算
//sh.OlePropertyGet("Cells",5,8).OlePropertySet("FormulaR1C1","=SUM(R[-3]C[-2]:R[-1]C[-1])");//横竖向加法运算
}
catch(...){
ShowMessage("启动Excel出错,可能沒有安裝Excel");
return;
}
//Excel的警告提示:
//关闭Excel的警告提示,如提示保存等
//sh.OlePropertyGet("Application").OlePropertySet("DisplayAlerts",false);
//打开Excel的警告提示,如提示保存等
sh.OlePropertyGet("Application").OlePropertySet("DisplayAlerts",true);
}
//---------------------------------------------------------------------------
没有合适的资源?快使用搜索试试~ 我知道了~
bcb-EXCEL.zip_C Builder_bcb_bcb EXCEL_c++ builder excel_excel
共14个文件
obj:2个
cpp:2个
~dfm:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 160 浏览量
2022-07-13
20:13:48
上传
评论
收藏 774KB ZIP 举报
温馨提示
这是一个基于c++builder 操作excel 表格的实例
资源推荐
资源详情
资源评论
收起资源包目录
bcb-EXCEL.zip (14个子文件)
操作EXCEL
Project1.cpp 710B
Unit1.obj 88KB
Project1.exe 41KB
Project1.res 876B
Unit1.~cpp 4KB
Project1.tds 2.75MB
Unit1.~dfm 763B
Unit1.dfm 763B
Unit1.h 878B
Unit1.cpp 4KB
1.xls 14KB
Unit1.~h 855B
Project1.obj 15KB
Project1.bpr 4KB
共 14 条
- 1
资源评论
- qq_245776712023-12-05资源和描述一致,质量不错,解决了我的问题,感谢资源主。
alvarocfc
- 粉丝: 104
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功