第一章 表格操作
第一节 Excel表格基础操作
一、创建应用对象(ExcelApp为excel函数)
ExcelApp=CreateOleObject (“Excel.Application”);
二、加载文件但不显示文件内容(用来处理异常报错信息,false表示不显示)
ExcelApp.OlePropertySet(“Visible”,(Variant)false);
三、屏幕不警告
ExcelApp.OlePropertySet(“DisplayAlerts” ,(Variant)false );
四、打开表格Workbook1
Workbook1=ExcelApp.OlePropertyGet(“workbooks”).OleFunction(“open”,OpenDialog1->FileName.c_str());
五、选中sheet1
Sheet1=Workbook1.OlePropertyGet(“ActiveSheet”);
六、选择其他sheet2
Workbook1.OlePropertyGet(“Sheets”,2)
七、复制其他表格区域
#源区域选择
String r= String(“A”) +startLNo +":I" + stopLNo; //A4:I100
Rangex=Sheet1.OlePropertyGet(“Range”,r.c_str()); //注意:一定要是.c_str()结尾
#复制到粘贴板
Rangex.OleFunction(“copy”);
#将源区域复制到目标区域
oRangex=newsheeti.OlePropertyGet(“Range”, “A4”); // 将表头复制到新的excel表格中
oRangex.OleProcedure(“pasteSpecial”);
八、保存表格并关闭
#获取文件路径
String new_excel= String(“c:\”) + name + “.xls”; // c:\XXPCS.xls
#另存为
newbooki.OleFunction(“SaveAs”,new_excel.c_str()); //文件路径要使用c_str()进行转换
#关闭newbook
newbooki.OleFunction(“close”);
九、退出excel
if(opened)
{
ExcelApp.OleFunction(“Quit”);
ExcelApp=Unassigned;
}
第二章 常用函数
#转换为str类型
.c_str()
#去掉空格
.trim()
#统计字段长度
.length()
#关闭程序
this->Close();
#清空内容
->clear()
#添加输出内容
.Add()
#转化为int类型
.ToInt()
#获取项目的索引数
ComboBox1->ItemIndex
#显示选中的内容
ComboBox1->Text
#设置窗口标题
OpenDialog1->Title = “请选择Excel文件”;
#扩展名大写
.UpperCase()
#获得文件大小
.size()
#在末尾增加一行
.add()
#在末尾增加多行的内容
.addstring()
#返回字段内容所在的位置(必须填写的是内容,区分大小写,全匹配)
.Indexof(“3.eeeeee”)
第三章 现成语句
#列数量统计
lieCount= Sheet1.OlePropertyGet(“UsedRange”).OlePropertyGet(“Columns”).OlePropertyGet(“Count”);
#行数量统计
hangCount= Sheet1.OlePropertyGet(“UsedRange”).OlePropertyGet(“rows”).OlePropertyGet(“Count”);
#捕获异常
Try
{}
catch( Exception & E )
{}
#检查输入的内容是否为数字
if(Edit1->Text>=‘a’&& Edit1->Text<=‘z’)
{
ShowMessage(“输入的是非法字符”);
return;
}
else if(Edit1->Text>=‘A’&& Edit1->Text<=‘Z’)
{
ShowMessage(“输入的是非法字符”);
return;
}
#设置列宽
ExcelApp.OlePropertyGet(“Columns”,1).OlePropertySet(“ColumnWidth”,22);
#设置行高
ExcelApp.OlePropertyGet(“Rows”,2).OlePropertySet(“RowHeight”,25);
#在工作表最前面插入一行
Sheet1.OlePropertyGet(“Rows”,1).OleProcedure(“Insert”);
#删除一行
ExcelApp.OlePropertyGet(“Rows”,2).OleProcedure(“Delete”); //将第2行删除
#设置单元格字体
Sheet1.OlePropertyGet(“Cells”,行号, 列号).OlePropertyGet(“Font”).OlePropertySet(“Name”,“隶书”); //字体
Sheet1.OlePropertyGet(“Cells”,行号, 列号).OlePropertyGet(“Font”).OlePropertySet(“size”,28); //大小
#获取sheet里某个单元格的值
Sheet1.OlePropertyGet(“Cells”,行号,列号).OlePropertyGet(“value”)
第四章 BCB技巧
1、修改memo中显示的内容
去掉里面显示的内容
2、修改默认打开文件的类型和文件名
点击
FileName表示指定默认的文件打开
Filter表示打开文件的类型
3、使用类
(1)将要导入的类(*.cpp)文件放到与工程统一个文件夹下
(2)在工程最开头写引用类的方法
#include “excel.cpp”
(3)在工程里调用该类中的方法,要先创建实例,
TExcelApp *ex; //Excel应用
TBook *book1,*book2,*book3; //工作薄
TSheet *sh1, *sh2; //工作表
注意:
红色字,表示类
标黄色的字,表示该类创建的实例,可以自定义
*表示指针,必须要写
(4)类的方法
例子:TSheet::getColCount(){}
表示Tsheet类的方法是getColCount()
4、控件常用属性说明
属性名称 功能 说明
Align 自动对齐方式 aClient整个Form
BevelWide 表格边宽
Caption 控件的标题
Visible 控件是否显示
Color 控件的背景颜色
Name 控件名字 只能非中文
5、控件输出的数据类型
控件名称 形式 数据类型 说明
Memo Lines Tstring
Edit Text string 框里必须要有值
ComboBox Text string
ListBox Item Tstring
Label Caption string
第五章 其他
void __fastcall表示不返回结果
_fastcall表示有返回结果
TBook* __fastcall表示返回指针指向TBook的类
const表示不可以修改数值
vector<TBook*>表示容器里的数据类型
第六章 报错信息
‘OlepropertySet’ is not a member of ‘Variant’
Identifier ‘Button2Click’ cannot have a type qualifier