Why won't it read an .xlsx file?

Ok, so I am trying to make this code read a name and grades from an .xlsx file. In this code all it does when I cout << fileName, it doesn't output anything. I don't think that my code is even reading the file. help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 Out.open ("studentGrade.xlsx");

	getline(In, fileName);
    In >> quizOne >> quizTwo >> quizThree >> quizFour >> quizFive >> quizSix >> quizSeven
	>> homeworkOne >> homeworkTwo >> homeworkThree >> homeworkFour >> homeworkFive >> homeworkSix >> homeworkSeven >> homeworkEight >> homeworkNine >> homeworkTen
	>> labOne >> labTwo >> labThree >> labFour >> labFive >> labSix
	>> project
	>> testOne >> testTwo >> testThree
	>> finalExam;

	In.open ("InputFile_Kuykendall.txt");

	cout << fileName << endl;
	cout << endl;
Try opening the In stream ... BEFORE you attempt to read from it.
Your code shows you are not aware of the xlsx format:

https://en.wikipedia.org/wiki/Office_Open_XML
xlsx is extremely complex: fonts, colors, macros, formulae, data, data element types, and a billion other things are jacked into a complex binary format.

export the file as csv is the easy way out for most spreadsheet work. If that won't do, you need either a ton of work or to use a library to read this format.
lastchance, oh yeah that my help :P. and jonnin I realized this and have already converted it into a csv file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

ofstream out;
ifstream in;

	out.open ("Student.csv");
	in.open ("Student.csv");

	getline(in, line);


	cout << line << endl;
	cout << endl;

	cout << setw(10) << "NAME" << setw(20) << "TOTAL" << setw(15) << "PERCENTAGE" << setw(15) << "LETTER GRADE" << endl;
	cout << "Student   " << studentName << setw(20) << total << setw(8) << percentage << setw(8) << letterGrade << endl;

	}
else if (main == '3')




so this is my current version but it still won't pull the name from the file I am opening
Hello Lightning Mage,

I see that you have realized that you can not read an ".xlsx" file directly. Now you have to understand how to read a ".csv" file, much easier.

Given an input file something like:

John Doe,90,90,A\n  // '\n' to indicate a new line or end of line


understand that each comma separates a field and you need to read each field separately.

So instead of getline(in, line); you would need to do something like:

1
2
3
4
std::getline(in, studentName, ',');
std::getline(in, total,',');
std::getline(in, percentage, ',');
std::getline(in, letterGrade);  // <--- Being last the third parameter is not needed 


After this any numerical value would have to be changed from a string to whatever you need it to be.

When this is working the way you want, put these lines in a while loop with the condition being the first line of code without the ";".

I have a couple of programs that read an Excel ".csv" file and process the data.

Hope that helps,

Andy
Thanks Andy, but what I was doing was just testing to see if it would read the first line of info. It won't, I am wondering if I am saving the csv file wrong, or what. The problem is that when I try to read and display even the first string of information it wont show anything. That is the real problem that I am facing. Also when I save my file as a .csv, then reopen the file it has the same layout as the .xlsx, so I was wondering if I might be saving it wrong.
Hello Lightning Mage,

In my version of Excel, 2010 version, there are three versions for saving a ".csv" file I believe I use the first version that says "CSV (comma delimited) (*.csv)". If that makes any difference for you.

I see what you are trying and there should be no reason that your test should not work. What I can think of right now is for you to post a portion, 3 or 4 lines, of the "csv" file so I can see what you are working with. The "csv" file is just a plain text file, so it should not be a problem.

Post a sample of the "csv" file and I will see what I can come up with.

Andy
Always check that the file opens correctly before you try to read from it.
1
2
3
4
5
6
7
8
#include <cstdio>
ifstream src("filename");

if (!src)
{
  perror("File error");
  return errno; // or exit(errno)
}
Topic archived. No new replies allowed.