乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何用c或者c++读取excel的某一单元格-c读取excel,c语言编程读取excel数据文件

如何用c或者c++读取excel的某一单元格-c读取excel,c语言编程读取excel数据文件

作者:乔山办公网日期:

返回目录:excel表格制作


你把CSV文件用记事本打开看一下就都知道了:逗号分隔的,比方说要读第6行第4个,那么就先跳掉前5行,然后跳掉前3个逗号,然后开始读,读到下一个逗号之前。

使用Visual C++的扩展指令#import:

#import "C:\\Program Files\\Common Files\\microsoft shared\\OFFICE14\\MSO.DLL" \

rename("RGB","MsoRGB") \
rename("SearchPath","MsoSearchPath")

#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"

#import "C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" ) \
exclude( "IFont", "IPicture" ) no_dual_interfaces

#import指令会从指定的可执行文件,动态链接库等COM组件中导出类型e5a48de588b6e799bee5baa6332库(type lib),在Debug和Release临时目录中生成对应的类型库头文件(type lib header file),以供C++程序使用。
如以上三条指令在编译后会生成excel.tlh, mso.lh和vbetext.olb三个头文件,可以在Debug和Release目录中找到。
这个,你估计看不懂吧


曾用C++ Builder 写过EXCEL操作。
一般有两种方法:
1,用ADO连续EXCEL数据库,这种方式,操作可用SQL来操作。
2,用OLE方式,这种方式可以达到EXCEL中大部分的功能和操作。

以下供参考:

/************* 1--- ADO *************/
//.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ADODB.hpp>
#include <DB.hpp>
#include <DBGrids.hpp>
#include <Dialogs.hpp>
#include <Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *btnOpen;
TButton *btnExit;
TOpenDialog *OpenDialog1;
TButton *btnEx;
TDataSource *DataSource1;
TADOConnection *ADOConnection1;
TADOQuery *ADOQuery1;
TDBGrid *DBGrid1;
void __fastcall btnExClick(TObject *Sender);
void __fastcall btnOpenClick(TObject *Sender);
void __fastcall btnExitClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DBGrid1->DataSource = DataSource1;
DataSource1->DataSet = ADOQuery1;
ADOQuery1->Connection = ADOConnection1;
ADOConnection1->LoginPrompt = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnExClick(TObject *Sender)
{
if(ADOQuery1->Active && ADOQuery1->RecordCount>0 )
{
int col;
AnsiString s;
FILE *fp;
fp = fopen("c:\\out.txt","w+t");
if(fp==NULL)
{
Application->MessageBox("导出文件建立失败","提示");
return ;
}
col = ADOQuery1->Fields->Count;
ADOQuery1->First();
while(!ADOQuery1->Eof)
{
s = "";
for(int i=0;i<col;i++)
s += ADOQuery1->Fields->Fields[i]->AsString +"\t";

fprintf(fp,"%s\n",s.c_str());
ADOQuery1->Next();
}
fclose(fp);
}
else
{
Application->MessageBox("没有需要导出的记录,请重新打开XLS文件","提示");
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
AnsiString sFile="";
AnsiString sCon;
if(OpenDialog1->Execute())
{
sFile = OpenDialog1->FileName;
/*
sCon = AnsiString("Driver={Microsoft Excel Driver (*.xls)};DBQ=")
+ sFile +"; ";
*/
sCon = AnsiString("Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='DSN=Excel Files;DBQ=")
+ sFile +"'; ";

}

try
{
if(sFile!="")
{
ADOConnection1->Connected = False;
ADOConnection1->ConnectionString = sCon;
ADOConnection1->Connected = True;
}
}
catch(...)
{
Application->MessageBox("连接XLS失败","提示");
return ;
}

if(ADOConnection1->Connected)
{
AnsiString sSQL = "Select * from [Sheet1$]";
ADOQuery1->SQL->Text = sSQL;
ADOQuery1->Active = True;
}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

/************* 2-- OLE *************/
C++ Builder把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。
设置对象属性:void OlePropertySet(属性名,参数……);
获得对象属性:Variant OlePropertyGet(属性名,参数……);
调用对象方法:1) Variant OleFunction(函数名,参数……);
2) void OleProcedure(过程名,参数……);

在程序中可以用宏定义来节省时间:

#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure

举例:
ExcelApp.OlePropertyGet("workbooks").OleFunction("Add");
可写为
ExcelApp.PG("workbooks").FN("Add");

C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。
首先定义以下几个变量:
Variant ExcelApp,Workbook1,Sheet1,Range1;

1、Excel中常用的对象是:Application,Workbooks,Worksheets等。

★创建应用对象★
Variant ExcelApp;
ExcelApp = Variant::CreateObject ("Excel.Application");
或者
ExcelApp = CreateOleObject ("Excel.Application");

★创建工作簿对象★
Variant WorkBook1;
WorkBook1 = ExcelApp.PG("ActiveWorkBook");

★创建工作表对象★
Variant Sheet1;
Sheet1 = WorkBook1.PG("ActiveSheet");

★创建区域对象★
Variant Range;
Range = Sheet1.PG("Range","A1:A10");
或者使用
Excel.Exec(PropertyGet("Range")<<"A1:C1").Exec(Procedure("Select"));

2、常用的属性操作:

★使Excel程序不可见★
ExcelApp.PS("Visible", (Variant)false);

★新建EXCEL文件★

◎ 新建系统模板的工作簿
ExcelApp.PG("workbooks").FN("Add") //默认工作簿
ExcelApp.PG("workbooks").FN("Add", 1) //单工作表
ExcelApp.PG("workbooks").FN("Add", 2) //图表
ExcelApp.PG("workbooks").FN("Add", 3) //宏表
ExcelApp.PG("workbooks").FN("Add", 4) //国际通用宏表
ExcelApp.PG("workbooks").FN("Add", 5) //与默认的相同
ExcelApp.PG("workbooks").FN("Add", 6) //工作簿且只有一个表
或者使用ExcelApp的Exec方法
Excel.Exec(PropertyGet("Workbooks")).Exec(Procedure("Add"));
◎ 新建自己创建的模板的工作簿
ExcelApp.PG("workbooks").FN("Add", "C:\\Temp\\result.xlt");

★打开工作簿★
ExcelApp.PG("workbooks").FN("open", "路径名e799bee5baa6e997aee7ad94e78988e69d83339.xls")

★保存工作簿★
WorkBook1.FN("Save"); //保存工作簿
WorkBook1.FN("SaveAs", "文件名");//工作簿保存为,路径注意用"\\"

★退出EXCEL★
ExcelApp.FN ("Quit");
ExcelApp = Unassigned;
或者
ExcelApp.Exec(Procedure("Quit"));

★操作工作表★

◎ 选择选择工作表中第一个工作表
Workbook1.PG("Sheets", 1).PR("Select");
Sheet1 = Workbook1.PG("ActiveSheet");

◎ 重命名工作表
Sheet1.PS("Name", "Sheet的新名字");

◎ 当前工作簿中的工作表总数
int nSheetCount=Workbook1.PG("Sheets").PG("Count");

★操作行和列★

◎ 获取当前工作表中有多少行和多少列:
Sheet1.PG("UsedRange").PG("Columns").PG("Count"); //列数
Sheet1.PG("UsedRange").PG("Rows").PG("Count"); //行数

◎ 设置列宽
ExcelApp.PG("Columns", 1).PS("ColumnWidth", 22);
或者
Range = ExcelApp.PG("Cells", 1, 3);
Range.PS("ColumnWidth", 22);

◎ 设置行高
ExcelApp.PG("Rows", 2).PS("RowHeight", 25);
或者
Range = ExcelApp.PG("Cells", 2, 1);
Range.PS("RowHeight", 25);

◎ 在工作表最前面插入一行
Sheet1.PG("Rows", 1).PR("Insert");

◎ 删除一行
ExcelApp.PG("Rows", 2).PR("Delete"); //将第2行删除

// 本文作者:ccrun ,如转载请保证本文档的完整性,并注明出处。
// 欢迎光临 C++ Builder 研究
// 摘自:http:///doc/go.asp?id=529

★操作单元格★

◎ 设置单元格字体
Sheet1.PG("Cells", 1, 1).PG("Font").PS("Name", "隶书"); //字体
Sheet1.PG("Cells", 2, 3).PG("Font").PS("size", 28); //大小

◎ 设置所选区域字体
Range.PG("Cells").PG("Font").PS("Size", 28);
// 本文转自 C++Builder研究 - http:///article.asp?i=529&d=0iezy5
Range.PG("Cells").PG("Font").PS("Color", RGB(0, 0, 255));
其中参数的设置:
Font Name : "隶书" //字体名称
Size : 12 //字体大小
Color : RGB(*,*,*) //颜色
Underline : true/false //下划线
Italic: true/false //斜体

◎ 设置单元格格式为小数百分比
Sheet1.PG("Cells", 1, 1).PS("NumberFormatLocal", "0.00%");

◎ 设定单元格的垂直对齐方式
Range = ExcelApp.PG("Cells", 3, 4);
// 1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐
Range.PS("VerticalAlignment", 2);

◎ 设定单元格的文本为自动换行
Range = ExcelApp.PG("Cells", 3, 4);
Range.PS("WrapText", true);

★单元格的合并★

◎ Range = Sheet1.PG("Range", "A1:A2"); //A1和A2单元格合并
String strRange = "A" + IntToStr(j) + ":" + "C" + IntToStr(j); //比如:A1:C5
Range1=Sheet1.PG("Range", strRange.c_str()); //可以用变量控制单元格合并
Range1.FN("Merge", false);

★读写单元格★

◎ 指定单元格赋值
String strValue = "abcdefg";
Sheet1.PG("Cells", 3, 6).PS("Value", strValue.c_str());
Sheet1.PG("Cells", j, 1).PS("Value", "总记录:" + String(j-6));
或者使用
Excel.Exec(PropertyGet("Cells")<<1<<3).Exec(PropertySet("Value")<<15);

◎ 所选区域单元格赋值
Range.PG("Cells").PS("Value", 10);

◎ 所选区域行赋值
Range.PG("Rows",1).PS("Value", 1234);

◎ 工作表列赋值
Sheet1.PG("Columns",1).PS("Value", 1234);

◎ 读取取值语句:
String strValue = Sheet1.PG("Cells", 3, 5).PG("Value");

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网