How to work excel into c++

closed account (9yq5oG1T)
Hi all,
I'm trying to read in an excel file with data size 20x2. However, when i read in the file it reads every line in the workbook. Is there a way to only read entered data? Here is the code so far. Thank in advance for the help.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int dataCount;
int size = 0;
int size1 = 0;
int size2 = 0;
int dataArray1[] = { 0 };
int dataArray2[] = { 0 };

string dataFile = "C:\\Users\\Pickle\\Desktop\\Data.xlsx";
string str = "";

ofstream fout;
ifstream fin;

fin.open(dataFile);

while(fin >> str)
{
dataCount ++;
}

cout << "There are " << dataCount << " entries in the data set." << endl;

while(getline(fin,str))
{

}
}
1. Please use code tags around your code so it's more readable

and

2. Off the bat I can see that you didn't assign a value to int dataCount; and in your while loop:

1
2
3
4
while(fin >> str)
{
dataCount ++;
}


You are incrementing an unassigned value.
Last edited on
Maybe try saving the data from excel as either txt (tab delimited) or csv (comma delimited).
closed account (9yq5oG1T)
OK so I looked around and altered my code a little. Basically what I'm trying to do is read in data sets, save the data in an array, and then print out the array. I also want to use the numbers in the array so I need them as int or double. So I have tried saving it as .cvs and .txt and then trying to read it, but when I cout I get the entire line with the commas. Here is what I have so far. Thanks for the feedback so far guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    int i = 0;
    int data = 0;
    int dataCount = 0;
    int dataArray1[] = {0};

    string dataFile = "C:\\Users\\Pickle\\Desktop\\Data.csv";
    string dataFileR = "C:\\Users\\Pickle\\Desktop\\DataR.txt";
    string str = "";

    ofstream fout;
    ifstream fin;

    fin.open(dataFile);
    fout.open(dataFileR);

    while(fin>>str)
    {
     fout << str << endl;
     dataCount ++;
    }

    cout << endl << "There are " << dataCount << " entries in the data set." << endl << endl;

    fin.close();
    fin.open(dataFileR);

    while(fin>>str))
    {
     if(str!=",")
     {
      cout << str << endl;
     }

     else{}
    }

    return 0;
}
Also, for the while loop I like to use:

1
2
3
4
while(!fin.eof())
{
dataCount++;
}


But that's just me


You shouldnt use eof - The way he has it right now is much better.
http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong

Hopefully an expert on this forum can give a bit more of an explanation.
closed account (9yq5oG1T)
Well I think I figured it out, so I wanted to post it in case anyone else has the same problem. I don't think .xlsx works at all if you don't have advanced knowledge. I used .txt and .csv per recommendations by the guys above and it worked. I used stringstream to convert the strings to integers to save them in the array. So here it is and any comments on how to make it prettier are welcome!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    int i = 0;
    int data = 0;
    int dataCount = 0;
    int dataArray[1000] = {0};
    int dataArray1[1000] = {0};
    int dataArray2[1000] = {0};

    string dataFile = "C:\\Users\\Pickle\\Desktop\\Text.txt";
    string dataFileR = "C:\\Users\\Pickle\\Desktop\\TextR.txt";
    string str = "";

    ofstream fout;
    ifstream fin;

    fin.open(dataFile);
    fout.open(dataFileR);

    while(fin>>str)
    {
     fout << str << endl;
     dataCount ++;
    }

    cout << endl << "There are " << dataCount << " entries in the data set." << endl << endl;

    fin.close();
    fin.open(dataFileR);

    do
    {
     fin >> str;
     stringstream ss(str);
     ss >> dataArray[i];
     cout << i << " " << dataArray[i] << endl;
     i++;
    }

    while(i<dataCount);

    return 0;
}
xlsx file is not a text file, it is a binary file, so you cannot easily read strings from it.
You have to know the file format.
Actually, it is a zip file which contains a few xml files.
Hmm @TarikNeaj didn't know that, but thanks for the clarification on it, won't be using eof anymore lol
Topic archived. No new replies allowed.