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

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




Is Read-in & Writing to *.xls different from *.txt file?

 
Reply to this topicStart new topic

Is Read-in & Writing to *.xls different from *.txt file?, Ok with *.txt file, but problem with *.xls file, why please?

m.babanfati
7 Jan, 2008 - 05:25 AM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
Hello all;

I need your help in this
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>
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;
}


the error message, I normally get 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 ==========
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Is Read-in & Writing To *.xls Different From *.txt File?
7 Jan, 2008 - 06:00 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
I googled it, and I found this page:
http://www.codeguru.com/cpp/data/mfc_datab...icle.php/c4307/

I think you should look at the ReadCell(char*,long,long) method.

This post has been edited by GWatt: 7 Jan, 2008 - 06:01 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Is Read-in & Writing To *.xls Different From *.txt File?
7 Jan, 2008 - 06:06 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Keep in mind that Excel is a proprietary format owned by MS, not like a .txt file. the easiest way to access it's data would be through the COM objects and their APIs supplied by MS (search MSDN for the right ones), but there are third party classes, such as the one pointed out by GWatt, that will allow you to do the same thing (with some limitations).
User is online!Profile CardPM
+Quote Post

m.babanfati
RE: Is Read-in & Writing To *.xls Different From *.txt File?
7 Jan, 2008 - 09:13 AM
Post #4

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
QUOTE(Amadeus @ 7 Jan, 2008 - 07:06 AM) *

Keep in mind that Excel is a proprietary format owned by MS, not like a .txt file. the easiest way to access it's data would be through the COM objects and their APIs supplied by MS (search MSDN for the right ones), but there are third party classes, such as the one pointed out by GWatt, that will allow you to do the same thing (with some limitations).


Thanks alot Gwatt and Amadeus !

But the concept of this "------COM objects and their APIs------", as a beginner is one of the strange things I begin to hear about in this programming community. Can you please shed more light, and precisely what & where I need to begin? the web address supplied by GWatt is returning "some funny error message", while displaying seemingly reasonable stuff beneath the error message.

Thanks for your concerns
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Is Read-in & Writing To *.xls Different From *.txt File?
7 Jan, 2008 - 10:47 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Everyone talks about different languages having strengths and weaknesses? This is a really good example of where NOT to use C++. tongue.gif

The issue is, .XLS format is not a text file. The C++ code you're showing wants a text file. Think of it this way, try opening an excel file with notepad. See all the junk? That's what C++ sees.

The solution is asking the software already present on the computer to do the work. Rather than doing it directly, you have to talk to some Microsoft interface that can do the job for you. These interfaces include various database style bridges, like ODBC and OleDB, and the application object itself.

( For some reason, my Excel.Application code won't post? No worries,this is the main example. )

In vb script:
CODE

Sub TestOle(xlsFileName, rows, cols)
    Set oConn = CreateObject("ADODB.Connection")
    oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & xlsFileName _
        & ";Extended Properties=""Excel 8.0;HDR=NO;"""

    Set oRS =  CreateObject("ADODB.Recordset")
    oRS.Open "Select * From [Sheet1$]", oConn
    'oRS.Open "Select * From [Sheet1$A1:D5]", oConn

    col=1
    For row = 1 to rows
        line = ""
        For col = 0 to cols - 1
            line = line & oRS.Fields(col).Value
            line = line & vbTab
        Next
        Wscript.Echo line
        oRs.MoveNext
    Next
    oConn.Close
End Sub


This uses ADO as an interface. It has the big advantage of not launching a copy of Excel to do the work and should probably be preferred for data retrieval.

Now, why VBScript and not C++? Well, I'd never use C++ to do this kind of thing. Well, not unless I'm writing my own COM+ wrapper and had to. Even then, I'd use managed C++. Ok, to be honest, I'd use C#.

The library that was pointed to before uses this method. You could look at the code an worry out what it's doing. Basically, reproduce the last method, figure out how to hook your program into ADO and then just use the methods offered.

Hope this helps.

This post has been edited by baavgai: 7 Jan, 2008 - 10:53 AM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:20PM

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