Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,929 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,774 people online right now. Registration is fast and FREE... Join Now!




Reading from and Displaying to an excel file

 
Reply to this topicStart new topic

Reading from and Displaying to an excel file, Input and Output Problem

m.babanfati
2 Jan, 2008 - 07:52 PM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
Hello all;

Can someone please help me out of this mess?
I have a 3X3 array in an excel file (named: Book2.xls), I wrote a program to read the file and display the same array in another excel file named: Book3.xls, but I keep on getting the same error as you can see at the bottom of the code.

Please Help !


CODE


#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;

// nRows and nCols stands for the number of rows and columns for the array

const int nRows2 = 3;
const int nCols2 = 3;

// method to read a 3X3 array from the only sheet (Sheet1) in an excel file named "Books2.xls"

void readArray(int myArray[nRows2][nCols2]) {
    // to open an excel file "Book2.xls" for reading
    ifstream myExcelFile CSpreadsheet("Book2.xls", "Sheet1");
    if (myExcelFile.is_open()) {
        for (int row = 0; row <nRows2; row++) {
            for (int col = 0; col <nCols2; col++) {
                // To accept/read-in values for the array
                myExcelFile >> myArray[row][col];
            }
        }
     }
    else {
        cout << "Can't open your Excel file";
    }
    myExcelFile.close();
}

// Method to display/write contents of the earlier read array in another excel file named "Book3.xls"
void displayMyArray(int myArray[nRows2][nCols2])
{   // to open a file "examples_output.txt" for output
    ofstream myfile CSpreadsheet("Book3.xls", "Sheet1");
    myfile.open ("Book3.xls");
     if (myfile.is_open()){
          for (int row = 0; row < nRows2; row++){
             for (int col = 0; col < nCols2; col++){
// to print out elements of an array
      myfile << myArray[row][col]  << '\t';}
             myfile << endl <<endl;}
    }
    else {
         cout << "Can't open file for writing";
    }
    myfile.close();
}// method displayMyArray ends here
int main () {
    
    int myArray[nRows2][nCols2];
    readArray(myArray);
    displayMyArray(myArray);
return 0;
}


The Error message is as follows:

1>------ Build started: Project: project2, Configuration: Debug Win32 ------
1>Compiling...
1>project2.cpp
1>.\project2.cpp(112) : error C2146: syntax error : missing ';' before identifier 'CSpreadsheet'
1>.\project2.cpp(112) : error C3861: 'CSpreadsheet': identifier not found
1>.\project2.cpp(130) : error C2146: syntax error : missing ';' before identifier 'CSpreadsheet'
1>.\project2.cpp(130) : error C3861: 'CSpreadsheet': identifier not found
1>Build log was saved at "file://c:\Users\m.babanfati\Documents\Visual Studio 2008\Projects\project2\Debug\BuildLog.htm"
1>project2 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
User is offlineProfile CardPM
+Quote Post

Trake
RE: Reading From And Displaying To An Excel File
3 Jan, 2008 - 04:25 AM
Post #2

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 61


My Contributions
I think you shouldn't be using:

CODE

ifstream myExcelFile CSpreadsheet("Book2.xls", "Sheet1");


I think it should just be:

CODE

ifstream CSpreadsheet("Book2.xls", "Sheet1");

User is offlineProfile CardPM
+Quote Post

lockdown
RE: Reading From And Displaying To An Excel File
3 Jan, 2008 - 07:59 AM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(Trake @ 3 Jan, 2008 - 05:25 AM) *

I think you shouldn't be using:

CODE

ifstream myExcelFile CSpreadsheet("Book2.xls", "Sheet1");


I think it should just be:

CODE

ifstream CSpreadsheet("Book2.xls", "Sheet1");



That is some what correct. The if stream is just creating the stream you will be using to write information to. You then need to open the file you want the information written to and tell what you are doing (commonly open or close).

CODE

ifstream CSpreadsheet;

CSpreadsheet.open("Book2.xls",ios:::in); // Can be ios:::in or ios:::out depending on what you want to do

// Your information

CSpreadsheet.close


From my code above I have removed Sheet 1 since that will be the default sheet that will open when the application access the excel file. Now I am not a expert in the stream commands so I am not sure how it would open two files. But from what I can assume and some what understand having the Sheet1 in their makes the program think to open another file. But I am not sure if that is correct. Either way in this instance you would be fine by remove Sheet 1 because it defaults to that. But if you wanted a different sheet I am not sure.

I could be wrong though because I have only worked with spread sheets once in this fashion and that was awhile back.

Here is a site that explains the ifstream, its commands, and parameters. http://www.cplusplus.com/reference/iostrea...tream/open.html

This post has been edited by lockdown: 3 Jan, 2008 - 08:03 AM
User is offlineProfile CardPM
+Quote Post

m.babanfati
RE: Reading From And Displaying To An Excel File
4 Jan, 2008 - 05:13 PM
Post #4

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
Hi all !

Thanks for the suggestions by Trake and lockdown & at the same time apologizing for failing to update you about the progress so far.

In fact the number of errors reduced from 4 to 2 (50% off). But i still get some 2 errors that I worked around to eliminate, but couldn't.

The complete code and the newly generated errors are as follows:
CODE

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
using namespace std;

// method to read a 3X3 array from the only sheet (Sheet1) in an excel file named "Books2.xls"
const int nRows2 =3;
const int nCols2 =3;
void readArray(int myArray[nRows2][nCols2]) {
    // to open an excel file "Book2.xls" for reading
    ifstream CSpreadsheet;
    CSpreadsheet.open("Book2.xls",ios::in); // Can be ios:::in or ios:::out depending on what you want to do
    ifstream CSpreadsheet("Book2.xls");
    if (CSpreadsheet.is_open()) {
        for (int row = 0; row <nRows2; row++) {
            for (int col = 0; col <nCols2; col++) {
                // To accept/read-in values for the array
                CSpreadsheet >> myArray[row][col];
            }
        }
     }
    else {
        cout << "Can't open your Excel file";
    }
    CSpreadsheet.close();
}

// Method to display/write contents of the earlier read array in another excel file named "Book3.xls"
void displayMyArray(int myArray[nRows2][nCols2])
{   // to open a file "examples_output.txt" for output
    ofstream CSpreadsheet;
    CSpreadsheet.open ("Book3.xls", ios::out);
     if (CSpreadsheet.is_open()){
          for (int row = 0; row < nRows2; row++){
             for (int col = 0; col < nCols2; col++){
// to print out elements of an array
               CSpreadsheet << myArray[row][col]  << '\t';}
             CSpreadsheet << endl <<endl;}
    }
    else {
         cout << "Can't open file for writing";
    }
    CSpreadsheet.close();
}// method displayMyArray ends here
int main () {
    
    int myArray[nRows2][nCols2];
    readArray(myArray);
    displayMyArray(myArray);
return 0;
}


While the error message is:

1>------ Build started: Project: project2, Configuration: Debug Win32 ------
1>Compiling...
1>project2.cpp
1>.\project2.cpp(177) : error C2370: 'CSpreadsheet' : redefinition; different storage class
1> .\project2.cpp(175) : see declaration of 'CSpreadsheet'
1>.\project2.cpp(182) : error C2088: '>>' : illegal for class
1>Build log was saved at "file://c:\Users\m.babanfati\Documents\Visual Studio 2008\Projects\project2\Debug\BuildLog.htm"
1>project2 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any further suggestion is highly appreciated.



QUOTE(lockdown @ 3 Jan, 2008 - 08:59 AM) *

QUOTE(Trake @ 3 Jan, 2008 - 05:25 AM) *

I think you shouldn't be using:

CODE

ifstream myExcelFile CSpreadsheet("Book2.xls", "Sheet1");


I think it should just be:

CODE

ifstream CSpreadsheet("Book2.xls", "Sheet1");



That is some what correct. The if stream is just creating the stream you will be using to write information to. You then need to open the file you want the information written to and tell what you are doing (commonly open or close).

CODE

ifstream CSpreadsheet;

CSpreadsheet.open("Book2.xls",ios:::in); // Can be ios:::in or ios:::out depending on what you want to do

// Your information

CSpreadsheet.close


From my code above I have removed Sheet 1 since that will be the default sheet that will open when the application access the excel file. Now I am not a expert in the stream commands so I am not sure how it would open two files. But from what I can assume and some what understand having the Sheet1 in their makes the program think to open another file. But I am not sure if that is correct. Either way in this instance you would be fine by remove Sheet 1 because it defaults to that. But if you wanted a different sheet I am not sure.

I could be wrong though because I have only worked with spread sheets once in this fashion and that was awhile back.

Here is a site that explains the ifstream, its commands, and parameters. http://www.cplusplus.com/reference/iostrea...tream/open.html


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month