473,421 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Reading Excel Data in VC++

Hello All,

I am trying to read an Excel spreadsheet (xls) programmatically. I have used
the ODBC database route and have opened the Excel spreadsheet and then using
a 'CRecordset', I read each row.

I correctly get that I have 13 columns, their names, etc... The problem
comes when I am reading the data. I use the function 'GetFieldValue'. It
correctly retrieves all data for each cell where numbers are stored as text.
If a cell has a number that has been formatted as a number with thousand
separators, etc, I always get null data returned. I have also tried forcing
the 'GetFieldValue' function to use a specific data format...have tried every
combination, but if I try read it as anything other than text, I get an
error. If I use the 'GetODBCFieldInfo' function, it returns that each cell in
the spreadsheet is text....

Where am I going wrong?
Regards.....Adrian
Sep 12 '08 #1
1 4259
Below is a function that reads an xls spreadsheet that is on the C drive
called 'FullStats05092008.xls'. I compiled it as a win32 consol aplication
with
#include "stdafx.h"
#include <odbcinst.h>
#include <afxdb.h>

It reads each line and prints to the consol......
If one of the cells being read has been formatted as a number, it reads it
as a null string.....

int ReadExcelFile( void )

{

CDatabase *Database;

CRecordset *Sheet;

int TotalColumns, TotalRows, column;

CString DriverString, RowString, CellValue, SqlCmd;

CString ExcelDriver, xlsFile;

Database = new CDatabase;

DriverString = "DRIVER={Microsoft Excel Driver
(*.xls)};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE; CREATE_DB=\"C:\\FullStats05092008.xls\";DBQ=C:\\Fu llStats05092008.xls";

Database->OpenEx(DriverString, CDatabase::noOdbcDialog);

// Open Sheet

Sheet = new CRecordset( Database );

SqlCmd = "SELECT * FROM [Sheet1$A1:IV65536]";

try

{

Sheet->Open(CRecordset::forwardOnly, SqlCmd, CRecordset::readOnly);

}

catch(...)

{

delete Sheet;

Sheet = NULL;

Database->Close();

return( false );

}

TotalColumns = Sheet->m_nResultCols;

TotalRows = 0;

while (!Sheet->IsEOF())

{

TotalRows++; // Keep count of total number of rows

try

{

// Get all the columns in a row

RowString.Empty();

for (column = 0; column < TotalColumns; column++)

{

Sheet->GetFieldValue(column, CellValue);

RowString += CellValue + " ";

}

RowString += "\r\n";

printf( RowString );

Sheet->MoveNext();

}

catch (...)

{

printf( "Error reading row\n" );

delete Sheet;

Sheet = NULL;

Database->Close();

return( false );

}

}

Sheet->Close();

delete Sheet;

Sheet = NULL;

Database->Close();
return( true );

}

Sep 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sobhan | last post by:
Hi all, I am writing a program in C++ in which I need to read a data file and export to the excel.The Data in the file in CSV format. Values in the .txt file are as follows: "a","b","c"...
4
by: Phoebe. | last post by:
Hi, Good Day! Reading 1 excel file into a dataset is fine. How can I read multiple excel with the same data structure into 1 dataset? How can I append those data? Can someone help? Thanks in...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
1
by: sweety | last post by:
Hi, Very Urgent... I Need to write a structure of data which is in "C" to excel file(*.xls). So, using C/VC++ how i can create a Excel file and how i can write a data into the respective...
2
by: k-w | last post by:
Hi all In vb I do: Dim Ex As New Excel.Application Ex.Workbooks.Open "c:\tmp\Book1.xls" Similar code in VC doesn't work:
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
3
by: Brad | last post by:
I'm having a problem reading data from an Excel file into a dataset. Can anybody give me an idea of what's happening? I've included the problematic source and the error message to the end of this...
2
by: Mariii | last post by:
Hello! I have a question. I work with Excel from VC++ application. I read csv-file, reorganize it and put data into Excel-file using CApplication app.CreateDispatch(_T("Excel.Application)); ......
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.