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

Ok with *.txt file, but problem with *.xls file, why please?

Page 1 of 1

4 Replies - 4126 Views - Last Post: 07 January 2008 - 11:47 AM Rate Topic: -----

#1 m.babanfati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 10-December 07

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

Post icon  Posted 07 January 2008 - 06:25 AM

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 !

#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 ==========

Is This A Good Question/Topic? 0
  • +

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

#2 GWatt  Icon User is offline

  • member icon

Reputation: 276
  • View blog
  • Posts: 3,075
  • Joined: 01-December 05

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

Posted 07 January 2008 - 07:00 AM

I googled it, and I found this page:
http://www.codeguru....icle.php/c4307/

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

This post has been edited by GWatt: 07 January 2008 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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

Posted 07 January 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).
Was This Post Helpful? 0
  • +
  • -

#4 m.babanfati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 10-December 07

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

Posted 07 January 2008 - 10:13 AM

View PostAmadeus, on 7 Jan, 2008 - 07:06 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

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

Posted 07 January 2008 - 11:47 AM

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

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:
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: 07 January 2008 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1