怎么用C++获取Excel内有效数据的行数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用C++获取Excel内有效数据的行数相关的知识,希望对你有一定的参考价值。
编译器用的vs2008 Excel用的2010
我要把Excel的数据批量导入到我做的MFC程序当中,但是数据量比较大需要用循环语句
在循环过程中不知道具体Excel里有几行数据 因此不知道需要循环多少次不知道准备多少个容器来容纳数据
求告知获取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(过程名,参数……);
QT如何获取QTableWidget表格中的高亮位置的行数?
如图,我想实现的功能是选择QTableWidget表格中的一行,然后点击删除连接按钮,就能把选中的那行给删除掉,现在是不知道如何获取高亮的位置,删除可以用tableWidget->removeRow(row);吧?
参考技术A int m=ui->tiblewidge>currentRow();ui->tiblewidge->removeRow(m);本回答被提问者采纳
以上是关于怎么用C++获取Excel内有效数据的行数的主要内容,如果未能解决你的问题,请参考以下文章