Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Excel fields from Visual C++ 3

Status
Not open for further replies.

drexeliu

Programmer
May 29, 2001
2
US
The current project I am working on requires the ability to retrieve data from a Microsoft Excel file for use in the Visual C++ environment. Information is very sketchy about this sort of thing, but I do believe it is possible. It is definitely possible with Visual Basic, but this application requires C++. If anyone has any suggestions, either general or preferably specific, please let me know the steps necessary to accomplish this. Thanks!
 
My Guess is its going to so kind of infile statements
like what was in my code problem. take a look at the code
that is in my question.
 
Hi

I include here some code I use to read Excel file
Use the Visual C++ on-line Help for details

HTH

Thierry
EMail: Thierry.Marneffe@swing.be
WebSite:
Here it is:

COleVariant covOptional(( long) DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant covTrue(( short) TRUE);
COleVariant covFalse(( short) FALSE);

try
{
_Application app;
_Workbook book;
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range;

if ( !app.CreateDispatch( "Excel.Application"))
{
AfxMessageBox( Unable to Start Excel !!!");

return;
}

CWaitCursor wait;

// Get Workbooks Collection

books = app.GetWorkbooks();

// Open File

CString stre;

book = books.Open( strFile, covOptional, covTrue, covOptional, covOptional,
covOptional, covTrue, covOptional, covOptional, covFalse,
covOptional, covOptional, covFalse);

// Get Sheets

sheets = book.GetSheets();

// Get First Sheet

sheet = sheets.GetItem( COleVariant(( short) 1));

// Scan File to Get some Data

CMsg msg;
COleVariant covCell;
COleVariant covValue;
CTime t;
CString strUnit;
int nBatchNbr;
char szBuffer[100];

THIS is A SAMPLE ... DO as you NEED ...

for ( int nRow = 9; nRow <= 90; nRow += 2)
{
// Get Date

covCell = Cell( nRow, 3);

range = sheet.GetRange( covCell, covCell);

covValue = range.GetValue();

// Extract Date from COleVariant

if ( covValue.vt == VT_DATE)
{
t = GetOleVariant( covValue, t);
}
else
{
CString strMsg;

strMsg.Format( &quot;Error Getting Time Data (Row %d Col 3) !!!&quot;, nRow);

AfxMessageBox( strMsg);

// Avoid 'Save Changes' message

book.SetSaved( TRUE);

// Quit Excel

app.Quit();
}

// Scan Other Data

for ( int nCol = 4; nCol <= 27; nCol++)
{
// Get Unit Name

covCell = Cell( nRow, nCol);

range = sheet.GetRange( covCell, covCell);

covValue.Clear();

covValue = range.GetValue();

// Extract String ( with safety check)

if ( covValue.vt == VT_BSTR)
{

// Extract String
// Note: BSTRs are wide, double-byte (Unicode) strings on
// 32-bit Windows platforms

unsigned cmb = wcstombs( NULL, covValue.bstrVal,
SysStringLen( covValue.bstrVal)) + 1;

strnset( szBuffer, 100, ' ');

wcstombs( szBuffer, covValue.bstrVal, cmb);

strUnit.Format( &quot;%s&quot;, szBuffer);

}
}
}
// Avoid 'Save Changes' message

book.SetSaved( TRUE);

// Quit Excel

app.Quit();
}
catch( COleException *e)
{
char szBuffer[1024];

sprintf( szBuffer, &quot;COleException. SCODE: %08lx.&quot;, (long)e->m_sc);

m_EventLog.WriteLine( szBuffer);
}
catch( COleDispatchException *e)
{
char szBuffer[1024];

sprintf( szBuffer,
&quot;COleDispatchException. SCODE: %08lx, Description: \&quot;%s\&quot;.&quot;,
( long) e->m_wCode, ( LPSTR) e->m_strDescription.GetBuffer(1024));

m_EventLog.WriteLine( szBuffer);
}
catch(...)
{
m_EventLog.WriteLine( &quot;General Exception caught ...&quot;);
}
}
 
Here's a few links that might also be helpful.

Link #1: Microsoft Office Development with Visual Studio

Link #2: Automating Microsoft Office 97 and Microsoft Office 2000


Also, CodeGuru has a brief sample that shows how to accomplish one simple task: printing a Word document. The general principles shown should apply to Excel as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top