如何在程序里将Excel表增加一行

KD007 2003-10-10 08:57:52
如题
...全文
842 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
52vc 2003-10-10
  • 打赏
  • 举报
回复
以上是SQL 数据导入Excel 的程序部分代码
以下为定义方式
要在应用程序中控制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","查询系统");
52vc 2003-10-10
  • 打赏
  • 举报
回复
CGauge1->MaxValue=FindsetResult->FindQuery->RecordCount;
Variant ex,newxls;
CGauge1->Progress=1;
int i,j=1;

try

{
ex=CreateOleObject("Excel.Application"); //启动Excel
}

catch(...)
{
ShowMessage("Microsoft Excel 2000 没有安装或文件已经被破坏!");
}
//ex.OlePropertySet("Visible",(Variant)false); //使Excel启动后可见
newxls=ex.OlePropertyGet("workbooks").OleFunction("Add");
//FindsetResult->FindQuery->Open();
FindsetResult->FindQuery->First();
AnsiString temp;
for(i=0;i<FindsetResult->FindQuery->FieldCount;i++)
{
temp=AnsiString(FindsetResult->DBGrid1->Columns->Items[i]->Title->Caption);
ex.OlePropertyGet("Cells",(Variant)1,(Variant)(i+1)).OlePropertySet("Value",temp);
}

while(!FindsetResult->FindQuery->Eof)
//将数据库中的记录依次写到工作薄中
{
CGauge1->Progress++;
if(FindsetResult->FindQuery->RecNo%50==1)
{
Toex->Refresh();
FindsetResult->Refresh();
}
j=j+1;
for(i=0;i<FindsetResult->FindQuery->FieldCount;i++)
{
temp=AnsiString(FindsetResult->FindQuery->Fields->Fields[i]->AsString);
ex.OlePropertyGet("Cells",(Variant)j,(Variant)(i+1)).OlePropertySet("Value",temp);
}
FindsetResult->FindQuery->Next();
}
ex.OlePropertySet("Visible",(Variant)true);
KD007 2003-10-10
  • 打赏
  • 举报
回复
我一定好好学习,还有个问题,我想设置一个统计函数怎么办,它算属性还是对象呢?
【前言】 工作或学习中可能需要实现基于VC读\写Excel文件的功能,本人最近也遇到了该问题。中间虽经波折,但是最终还是找到了解决问题的办法。 在此跟大家分享,希望对跟我同样迷茫过的同学们有所帮助。 1、程序功能 1)打开一个excel文件; 2)显示到CListCtrl上; 3)新建一个Excel文件。 以上均在对话框中实现。 2、平台 VC++2010 3、实现方法 常用的Excel打开方式有两种 1)通过数据库打开; 2)OLE方式打开。 由于方式1)操作繁琐,经常出现莫名的错误,这选用方式2). 4、准备步骤 首先新建一个Dialog窗体程序,添加list control和两个按钮 1)将ExcelLib文件夹拷贝到程序目录下; 2)将Export2Excel.h,Export2Excel.cpp两个文件添加到项目; 3)包含头文件,#include "ExcelLib/Export2Excel.h" 通过以上步骤在程序中引入了可以读取Excle文件的CExport2Excel类; 5、打开excel文件 通过按钮点击打开 void CExcelTestDlg::OnBnClickedButtonOpenExcel() { //获取文件路径 CFileDialog* lpszOpenFile; CString szGetName; lpszOpenFile = new CFileDialog(TRUE,"","",OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"Excel File(*.xlsx;*.xls)|*.xls;*.xlsx",NULL); if (lpszOpenFile->DoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //打开文件 //文件中包含多个sheet时,默认打开第一个sheet CExport2Excel Excel_example; Excel_example.OpenExcel(szGetName); //获取sheet个数 int iSheetNum = Excel_example.GetSheetsNumber(); //获取已使用格行列数 int iRows = Excel_example.GetRowCount(); int iCols = Excel_example.GetColCount(); //获取单元格的内容 CString cs_temp = Excel_example.GetText(1,1); //AfxMessageBox(cs_temp); //List control上显示 //获取工作列名(第一行) CStringArray m_HeadName; m_HeadName.Add(_T("ID")); for (int i=1;iGetItemCount()>0) { m_list.DeleteColumn(0); } //初始化ClistCtrl,加入列名 InitList(m_list,m_HeadName); //填入内容 //第一行是标题,所以从第2行开始 CString num; int pos; for (int row = 2;row<=iRows; row++) { pos = m_list.GetItemCount(); num.Format(_T("%d"),pos +1); m_list.InsertItem(pos,num); for (int colum=1;columDoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //文件全名称 CString csFileName = szGetName; //需要添加的两个sheet的名称 CString csSheetName = "newSheet"; CString csSheetName2 = "newSheet2"; // 新建一个excel文件,自己写入文字 CExport2Excel Excel_example; //新建excel文件 Excel_example.CreateExcel(csFileName); //添加sheet,新加的sheet在前,也就是序号为1 Excel_example.CreateSheet(csSheetName); Excel_example.CreateSheet(csSheetName2); //操作最开始添加的sheet:(newSheet) Excel_example.SetSheet(2); //添加Excel_example.WriteHeader(1,"第一列"); Excel_example.WriteHeader(2,"第二列"); //添加核心数据 Excel_example.WriteData(1,1,"数据1"); Excel_example.WriteData(1,2,"数据2"); //保存文件 Excel_example.Save(); //关闭文件 Excel_example.Close(); } 7、注意事项 1)一般单个Excel文件包含多个sheet,程序默认打开第一个; 2)指定操作sheet,使用Excel_example.SetSheet(2)函数; 3)打开文件时最左侧的sheet序号为1,新建excel时最新添加的sheet序号为1. 【后记】 本程序主要基于网络CSDN中---“Excel封装库V2.0”---完成,下载地址是:http://download.csdn.net/detail/yeah2000/3576494,在此示感谢!同时, 1)在其基础上作了小改动,改正了几个小错误,添加了几个小接口; 2)添加了如何使用的例子,原程序是没有的; 3)详细的注释 发现不足之处,还请大家多多指教!

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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