<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>excel-viewer-activex-contro &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/excel-viewer-activex-contro/</link>
	<description>Feed of posts on WordPress.com tagged "excel-viewer-activex-contro"</description>
	<pubDate>Thu, 20 Jun 2013 01:00:01 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[How to read an Excel file]]></title>
<link>http://vcpptips.wordpress.com/2012/01/22/how-to-read-an-excel-file/</link>
<pubDate>Sun, 22 Jan 2012 05:30:09 +0000</pubDate>
<dc:creator>Sanoop S P</dc:creator>
<guid>http://vcpptips.wordpress.com/2012/01/22/how-to-read-an-excel-file/</guid>
<description><![CDATA[The previous post was about to read an xml file. Just like that, Excel(xls) files are another common]]></description>
<content:encoded><![CDATA[<p><a href="http://vcpptips.files.wordpress.com/2012/01/description.jpg"><img class="size-full wp-image-1059 alignnone" title="Description" src="http://vcpptips.files.wordpress.com/2012/01/description.jpg?w=233&#038;h=88" alt="" width="233" height="88" /></a></p>
<p style="text-align:left;">The previous post was about to read an xml file. Just like that,<span style="color:#0000ff;"> Excel(xls)</span> files are another commonly used <span style="color:#0000ff;">format to store data</span> in our applications. In this post we will see how to<span style="color:#0000ff;"> read an excel file</span>.</p>
<p><a href="http://vcpptips.files.wordpress.com/2012/01/howcanidoit_2011.jpg"><img class="size-full wp-image-1060 alignnone" title="HowCanIDoIt_2011" src="http://vcpptips.files.wordpress.com/2012/01/howcanidoit_2011.jpg?w=306&#038;h=88" alt="" width="306" height="88" /></a></p>
<p style="text-align:justify;">There are mainly<span style="color:#0000ff;"> two methods</span> to read an excel file.</p>
<p style="text-align:justify;"><span style="color:#0000ff;">1) </span><span style="color:#0000ff;"><span style="color:#0000ff;">Excel</span> Viewer ActiveX control</span></p>
<p style="text-align:justify;"><span style="color:#0000ff;">2)</span> <span style="color:#0000ff;">ODBC method</span></p>
<p style="text-align:justify;">Using <span style="color:#0000ff;">Microsoft office ActiveX control</span> is the direct and easiest method, but it has some system dependencies  to load the file. So its better to choose ODBC method.  ODBC reading using two <span style="color:#0000ff;">MFC</span> classes <span style="color:#0000ff;">CDatabase</span> and <span style="color:#0000ff;">CRecordset</span> to read the file.  Please see the following example to get a clear idea.</p>
<p style="text-align:justify;">See below sample code to read the content of <a href="http://vcpptips.files.wordpress.com/2012/01/countrycapitalinfo.xls">CountryCapitalInfo.xls</a> file.</p>
<p style="text-align:justify;"><a href="http://vcpptips.files.wordpress.com/2012/01/codesnippet_2011.jpg"><img class="size-full wp-image-1061 alignnone" title="CodeSnippet_2011" src="http://vcpptips.files.wordpress.com/2012/01/codesnippet_2011.jpg?w=249&#038;h=88" alt="" width="249" height="88" /></a></p>
<p><span style="color:#0000ff;">ReadExcel.h</span></p>
<pre>#pragma once

#include 

class CReadExcel
{
public: /** Public constructor/destructor */

    /** Constructor. */
    CReadExcel(void); 

    /** Destructor. */
    ~CReadExcel(void);

private: /** Prevent object cloning/copying */

    /** Copy constructor. */
    CReadExcel(const CReadExcel&#38;);  

    /** Assignment operator overloaded */
    CReadExcel&#38; operator=(const CReadExcel&#38;);

public:

    /** Read the given xls file and fill the file data to an arrary.
    Table name must be Product_Info*/
    BOOL ReadExcelFile(/*[in]*/ CString&#38; p_strFilePath,
                       /*[out]*/ std::vector&#38; p_arrProductDetails );

private:

    /** Get the name of the Excel-ODBC driver */
    CString GetExcelDriver();
};</pre>
<p><span style="color:#0000ff;">ReadExcel.cpp</span></p>
<pre>#include "StdAfx.h"
#include "ReadExcel.h"
#include "odbcinst.h"
#include "afxdb.h"

CReadExcel::CReadExcel(void)
{
}

CReadExcel::~CReadExcel(void)
{
}

BOOL CReadExcel::ReadExcelFile(/*[in]*/ CString&#38; p_strFilePath,
                              /*[out]*/ std::vector&#38; p_arrProductDetails)
{
    CDatabase   database;
    CString     strDriver;
    CString     strDsn;	

    // Retrieve the name of the Excel driver.
    strDriver = GetExcelDriver();
    if (strDriver.IsEmpty())
    {
        AfxMessageBox(_T("No excel ODBC driver."));
        return FALSE;
    }

    strDsn.Format(_T("ODBC;DRIVER={%s};DSN='';DBQ=%s"), strDriver, p_strFilePath);

    try
    {
        // Open the database
        database.Open(NULL,false,false,strDsn);

        CRecordset recset( &#38;database );

        CString strSql;

        // Build the SQL string
        strSql.Format(_T("SELECT * FROM %s"), _T("MyTable"));

        recset.Open(CRecordset::forwardOnly, strSql, CRecordset::readOnly);

        unsigned long count = recset.GetRecordCount() ; 

        CString strProductInfo;
        strProductInfo.Format(_T("%s%s%s"), _T("Country"), _T("-"), _T("Capital"));
        p_arrProductDetails.push_back(strProductInfo);

        CString str1, str2;
        CString strItem1, strItem2;
        while( !recset.IsEOF() )
        {
            str1.Empty();
            str2.Empty();

            // Read the result line
            recset.GetFieldValue(_T("Country"), strItem1);
            str1 += strItem1;    

            recset.GetFieldValue(_T("Capital"), strItem2);
            str2 += strItem2;

            strProductInfo.Format(_T("%s%s%s"), str1, _T("-"), str2);
            p_arrProductDetails.push_back(strProductInfo);

            // Skip to the next resultline
            recset.MoveNext();
        }
        // Close the database
        database.Close();

    }
    catch(CDBException e)
    {
        AfxMessageBox(_T("Database error: ") + e.m_strError);
        return FALSE;
    }
    catch(...)
    {
        AfxMessageBox(_T("Could not open  the excel file."));
        return FALSE;
    }

    return TRUE;
}

CString CReadExcel::GetExcelDriver()
{
    char szBuf[2001];
    WORD cbBufMax = 2000;
    WORD cbBufOut;
    char *pszBuf = szBuf;
    CString strDriver;

    // Get the names of the installed drivers
    if (!SQLGetInstalledDrivers(szBuf, cbBufMax,&#38;cbBufOut))
        return "";

    // Search for the driver...
    do
    {
        if( strstr( pszBuf, _T("Excel") ) != 0 )
        {
            // Found !
            strDriver = CString( pszBuf );
            break;
        }
        pszBuf = strchr( pszBuf, _T('') ) + 1;
    }
    while (pszBuf[1] != _T(''));

    return strDriver;
}</pre>
<p><span style="color:#0000ff;">TestCode</span></p>
<pre>BOOL ReadExcelFile(CString p_strFilePath)
{
 CWaitCursor wait;
 CReadExcel readExcel;
 std::vector&#60;CString&#62; arrExcelInfo;
 if (!readExcel.ReadExcelFile(p_strFilePath, arrExcelInfo))
 {
  return FALSE;
 }
 return TRUE;
}</pre>
<div>
<p><span style="font-size:x-small;"><span style="font-size:x-small;"> <a href="http://vcpptips.files.wordpress.com/2012/01/note_2011.jpg"><img class="alignnone size-full wp-image-1067" title="Note_2011" src="http://vcpptips.files.wordpress.com/2012/01/note_2011.jpg?w=140&#038;h=88" alt="" width="140" height="88" /></a></span></span></p>
<p>Don&#8217;t forget to include the header files <span style="color:#0000ff;">odbcinst.h</span> and<span style="color:#0000ff;"> afxdb.h<span style="color:#000000;"> to your project</span></span>.<br />
One of the drawbacks of this method is that you have to set a name for the <span style="color:#0000ff;">data section</span> <span style="color:#0000ff;">(Insert-&#62;Names)</span> in the excel sheet.</p>
</div>
]]></content:encoded>
</item>

</channel>
</rss>
