C++ question
-
This is just a C++ question. Does anybody know how to get data from an excel file? I figured out a way to put data to an excel file. I just do this: ofstream outfile("ftest.out"); int a=1, b=2, c=3, d=4; outfile << a << "," << b << "," << c << "," << d; then when I open ftest.out with excel, it recognizes that the comma means to move to the next block. But when I try to get data from an excel file the same way: ifstream infile("ftest.in"); int a, b, c, d; infile >> a >> "," >> b >> "," >> c >> "," >> d; This doesn't work. Does anyone know some other method to get data from an excel file?
-
This is just a C++ question. Does anybody know how to get data from an excel file? I figured out a way to put data to an excel file. I just do this: ofstream outfile("ftest.out"); int a=1, b=2, c=3, d=4; outfile << a << "," << b << "," << c << "," << d; then when I open ftest.out with excel, it recognizes that the comma means to move to the next block. But when I try to get data from an excel file the same way: ifstream infile("ftest.in"); int a, b, c, d; infile >> a >> "," >> b >> "," >> c >> "," >> d; This doesn't work. Does anyone know some other method to get data from an excel file?
I don't know how to get the data from a normal excel file but I do know why your outfile code worked and your infile code doesn't... Your ftest.out file would be considered a csv file (comma seperated values) and excel supports csv. A normal excel file is not stored in csv format. Sorry I don't really have an answer but at least you know it isn't some little bug in your code. There are a few articles here on codeproject dealing with excel files, maybe you could find an answer there... try here for instance -Jack To an optimist the glass is half full. To a pessimist the glass is half empty. To a programmer the glass is twice as big as it needs to be.
-
This is just a C++ question. Does anybody know how to get data from an excel file? I figured out a way to put data to an excel file. I just do this: ofstream outfile("ftest.out"); int a=1, b=2, c=3, d=4; outfile << a << "," << b << "," << c << "," << d; then when I open ftest.out with excel, it recognizes that the comma means to move to the next block. But when I try to get data from an excel file the same way: ifstream infile("ftest.in"); int a, b, c, d; infile >> a >> "," >> b >> "," >> c >> "," >> d; This doesn't work. Does anyone know some other method to get data from an excel file?
I haven't tried it, but generally, if you export the Excel file in ASCII or .csv format (whichever it offers you as an option) the result will be a comma-separated text file. You can then read it as char data and parse the fields using the commas as end-of-field indicators. I believe there's a tokenizer function that does this, but I'm far from my MSDN library right now. Check there online for "token" or some such.
-
This is just a C++ question. Does anybody know how to get data from an excel file? I figured out a way to put data to an excel file. I just do this: ofstream outfile("ftest.out"); int a=1, b=2, c=3, d=4; outfile << a << "," << b << "," << c << "," << d; then when I open ftest.out with excel, it recognizes that the comma means to move to the next block. But when I try to get data from an excel file the same way: ifstream infile("ftest.in"); int a, b, c, d; infile >> a >> "," >> b >> "," >> c >> "," >> d; This doesn't work. Does anyone know some other method to get data from an excel file?
There is (was) an "Excel Developers Kit" (book) that at least went through the 97 version. Good ideas are not adopted automatically. They must be driven into practice with courageous patients. -Admiral Rickover. ...
-
This is just a C++ question. Does anybody know how to get data from an excel file? I figured out a way to put data to an excel file. I just do this: ofstream outfile("ftest.out"); int a=1, b=2, c=3, d=4; outfile << a << "," << b << "," << c << "," << d; then when I open ftest.out with excel, it recognizes that the comma means to move to the next block. But when I try to get data from an excel file the same way: ifstream infile("ftest.in"); int a, b, c, d; infile >> a >> "," >> b >> "," >> c >> "," >> d; This doesn't work. Does anyone know some other method to get data from an excel file?
You may consider using the getline method:
ifstream infile("ftest.in");
char[81] myText;
for(int i = 0;!infile.eof;i++)
{
myText = infile.getline(myText);}
Hope something like this helps. Nick Parker
-
I haven't tried it, but generally, if you export the Excel file in ASCII or .csv format (whichever it offers you as an option) the result will be a comma-separated text file. You can then read it as char data and parse the fields using the commas as end-of-field indicators. I believe there's a tokenizer function that does this, but I'm far from my MSDN library right now. Check there online for "token" or some such.
How do I export the Excel file in ASCII or .csv format? Do you mean that this is an option in the excel program itself, or do you mean that visual C++ has the ability to do this to the file? If its an option in excel itself, could you tell me how to go about exporting the file?
-
How do I export the Excel file in ASCII or .csv format? Do you mean that this is an option in the excel program itself, or do you mean that visual C++ has the ability to do this to the file? If its an option in excel itself, could you tell me how to go about exporting the file?
I don't use Excel, though several at work do, so I'm not certain how to do it. But I believe I found it once in the File menu. Check there for an Export option; it should offer you a set of format selections, including text (aka ASCII, or CSV). If it's not there, check the online Help for "Exporting." Every other spreadsheet program since Calc has had this option, so I doubrt that it's been left out. A potential problem might come up when using multiple sheet spreadsheets - it may export only one sheet - you'll need to manually review the resulting file to see what you've got.