how to read datas from an excel file

J

jw

i have datas in an excel file like this:
names surnames
jack white
smith black
marry brown

how can i read these datas for ex i know that names in the cell A1 and
surnames is next to it?
 
M

Moonlit

Hi,


Unless you know excell's format it probably is easier to save the excell
sheet as csv file and then do something like this

#include <ifstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
int RetVal = 0;

try
{
ifstream Input( "excell.csv" );
// Testing if open etc.
string Line Name, Surname;
while( getline( Input, Line ) )
{
// the next appraoch only works if there is no whitespace in the fields
replace_if( Line.begin(), Line.end(), bind2nd( equal_to<char>(), ';' ),
' ' );
stringstream Tokens( Line );
Tokens >> Name >> Surname;

}
}
catch( ...)
{
RetVal = -1;
}
return RetVal;
}


--


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
P

Phlip

jw said:
i have datas in an excel file like this:
names surnames
jack white
smith black
marry brown

how can i read these datas for ex i know that names in the cell A1 and
surnames is next to it?

Use ADO, and open the file as a database.

Then ask yourself why you are using C++ instead of a scripting language,
such as the VBA built into Excel.
 
J

jw

i must use c++ and the user of the program will write the datas to an
excel file,my prog. must read them from this file.
 
P

Peter_Julian

"Moonlit" <news moonlit xs4all nl> wrote in message
| Hi,
|
|
| Unless you know excell's format it probably is easier to save the
excell
| sheet as csv file and then do something like this
|

#include <iostream>
#include <ostream>

| #include <ifstream>
| #include <string>
| #include <sstream>

#include <stdexcept>

|
| using namespace std;
|
| int main()
| {
| int RetVal = 0;
|
| try
| {
| ifstream Input( "excell.csv" );

if(!Input)
{
throw std::exception("while opening file\n");
}

| // Testing if open etc.
| string Line Name, Surname;
| while( getline( Input, Line ) )
| {
| replace_if( Line.begin(), Line.end(), bind2nd(
equal_to<char>(), | ';' ),
| ' ' );
| stringstream Tokens( Line );
| Tokens >> Name >> Surname;
| }

if ( !Input.eof() )
{
throw std::exception("while parsing data.\n");
}

| }

catch(const std::exception& e)
{
std::cout << "Error: \n";
std::cout << e.what() << std::endl;
}

| catch( ...)
| {
| RetVal = -1;
| }
| return RetVal;

catch(...)
{
std::cout << "Error: unexpected exception caught!\n";
}
return 0;

| }
|
|
| --
|
|
| Regards, Ron AF Greve
|
| http://moonlit.xs4all.nl
|
| | >i have datas in an excel file like this:
| > names surnames
| > jack white
| > smith black
| > marry brown
| >
| > how can i read these datas for ex i know that names in the cell A1
and
| > surnames is next to it?
| >
|
|
 
B

ben

jw said:
i must use c++ and the user of the program will write the datas to an
excel file,my prog. must read them from this file.

If you don't mind running your C++ on CLR then you can expose yourself
to the .Net thingy which would make your data access task much easier.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top