Click here to Skip to main content
15,914,419 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to Convert Excel sheet file to .cvs format using VC++ Pin
Hamid_RT2-Aug-07 21:02
Hamid_RT2-Aug-07 21:02 
AnswerRe: How to Convert Excel sheet file to .cvs format using VC++ Pin
Anurag Gandhi2-Aug-07 21:08
professionalAnurag Gandhi2-Aug-07 21:08 
AnswerRe: How to Convert Excel sheet file to .cvs format using VC++ Pin
David Crow3-Aug-07 3:48
David Crow3-Aug-07 3:48 
QuestionHow to extract taskbar button icon Pin
FlyingBear2-Aug-07 19:23
FlyingBear2-Aug-07 19:23 
AnswerRe: How to extract taskbar button icon Pin
Naveen2-Aug-07 20:09
Naveen2-Aug-07 20:09 
GeneralRe: How to extract taskbar button icon Pin
FlyingBear2-Aug-07 23:21
FlyingBear2-Aug-07 23:21 
Questionhelp me Pin
philiptabraham2-Aug-07 19:04
philiptabraham2-Aug-07 19:04 
AnswerRe: help me Pin
Anurag Gandhi2-Aug-07 19:23
professionalAnurag Gandhi2-Aug-07 19:23 
No need to create .csv file.
You can directly Create, edit or Read the excel file from your VC++ application using ODBC.

Here is the sample code which I have created but it is specific to my project. You need to do the changes accordingly.

<br />
#include "StdAfx.h"<br />
#include "Excel.h"<br />
<br />
CExcel::CExcel(void)<br />
{<br />
	m_rc = new CRecordset(&m_database);<br />
	m_driver = "MICROSOFT EXCEL DRIVER (*.XLS)" ; // exactly the same name as in the ODBC-Manager<br />
}<br />
<br />
CExcel::~CExcel(void)<br />
{<br />
}<br />
<br />
// Function to create File and Table.//<br />
int CExcel::CreateSheet(CString strFileName, CString strTableName, CString strColumns)<br />
{<br />
	//strColumns has the format like this: col1, col2, col3 <br />
	CString sql;<br />
	CString strTableCol;<br />
	CString strBlank;<br />
<br />
	strFileName= CString("Data\\") + strFileName;<br />
	strTableCol= strColumns;<br />
	strTableCol.Trim();<br />
	strTableCol.Insert(0,CString("["));<br />
	strTableCol.Replace(_T(", "),_T("] TEXT, ["));<br />
	strTableCol.Replace(_T("."),_T("] TEXT"));<br />
	strColumns.Replace('.',' ');<br />
<br />
	//strTableCol= strTableCol;<br />
	//AfxMessageBox(strTableCol);<br />
<br />
	int iLen=strColumns.GetLength();<br />
	for(int i=0;i<iLen;i++)<br />
	{<br />
		if(strColumns[i]==',')<br />
			strBlank+="'',";<br />
	}<br />
	strBlank+="''";<br />
	<br />
	sql.Format(_T("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s"), <br />
			      m_driver, strFileName, strFileName);<br />
<br />
	try<br />
	{<br />
		m_database.OpenEx ( sql, CDatabase::noOdbcDialog ) ;<br />
		sql.Format(_T("CREATE TABLE %s (%s)"), strTableName, strTableCol);<br />
		m_database.ExecuteSQL(sql);<br />
		sql.Format(_T("INSERT INTO %s VALUES (%s)"), strTableName, strBlank);<br />
		//sql.Format(_T("INSERT INTO %s (%s) VALUES (%s)"), strTableName, strColumns, strBlank);  //Till 24 March.//<br />
		//AfxMessageBox(sql);<br />
		//	sql.Format("INSERT INTO %s (Name,Age,Salary) VALUES ('%s','%d', '%d')", m_tablestr, name, age, sal ) ;<br />
		for(int iCount=0;iCount<500;iCount++)	//Inserting 500 empty rows.//<br />
			m_database.ExecuteSQL(sql);<br />
		m_database.Close();<br />
		//AfxMessageBox(_T("File/Table created successfully."));<br />
	}<br />
	catch(CDBException e)<br />
	{<br />
		CString str;<br />
		str.Format(_T("Driver not installed: %s"), m_driver ) ; <br />
		AfxMessageBox(str);<br />
	}<br />
	<br />
	return 0;<br />
}<br />
<br />
int CExcel::OpenSheet(CString strFileName, CString strTableName, CString strColumns)<br />
{<br />
	CString sql, str;<br />
	strFileName= CString("Data\\") + strFileName;<br />
<br />
	sql.Format(_T("DRIVER={%s};DSN='';READONLY=TRUE;DBQ=%s"), m_driver, strFileName ) ;<br />
<br />
	try<br />
	{<br />
		if ( m_database.OpenEx( sql, CDatabase::noOdbcDialog ) )<br />
		{<br />
			sql.Format (_T("SELECT * FROM %s"), strTableName);<br />
			m_database.ExecuteSQL ( sql );<br />
<br />
			m_rc -> Open(CRecordset::snapshot, sql, CRecordset::none);<br />
		} <br />
	}<br />
	catch ( CDBException e )<br />
	{<br />
		CString str;<br />
		str.Format(_T("Driver not installed: %s"), m_driver);<br />
		AfxMessageBox(str);<br />
	}<br />
	return 0;<br />
}<br />
<br />
int CExcel::CloseSheet(void)<br />
{<br />
	m_rc->Close();<br />
	m_database.Close();<br />
	return 0;<br />
}<br />


You need to understand the code properly before applying it to your project as you are new to VC++.

Anurag Gandhi.
http://www.softgandhi.co.nr

GeneralRe: help me Pin
philiptabraham2-Aug-07 19:36
philiptabraham2-Aug-07 19:36 
GeneralRe: help me Pin
philiptabraham2-Aug-07 19:51
philiptabraham2-Aug-07 19:51 
GeneralRe: help me Pin
Anurag Gandhi2-Aug-07 21:02
professionalAnurag Gandhi2-Aug-07 21:02 
AnswerRe: help me Pin
Hamid_RT2-Aug-07 20:56
Hamid_RT2-Aug-07 20:56 
QuestionVector inside a structure Pin
vinodkoul2-Aug-07 18:46
vinodkoul2-Aug-07 18:46 
AnswerRe: Vector inside a structure Pin
Stephen Hewitt2-Aug-07 19:14
Stephen Hewitt2-Aug-07 19:14 
AnswerRe: Vector inside a structure Pin
cp98762-Aug-07 19:15
cp98762-Aug-07 19:15 
GeneralRe: Vector inside a structure Pin
vinodkoul2-Aug-07 20:12
vinodkoul2-Aug-07 20:12 
QuestionRe: Vector inside a structure Pin
vinodkoul2-Aug-07 20:43
vinodkoul2-Aug-07 20:43 
AnswerRe: Vector inside a structure Pin
cp98762-Aug-07 20:54
cp98762-Aug-07 20:54 
QuestionPrint value of key pressed...? Pin
deostroll2-Aug-07 18:22
deostroll2-Aug-07 18:22 
AnswerRe: Print value of key pressed...? Pin
bob169722-Aug-07 18:43
bob169722-Aug-07 18:43 
QuestionRe: Print value of key pressed...? Pin
deostroll2-Aug-07 18:59
deostroll2-Aug-07 18:59 
AnswerRe: Print value of key pressed...? Pin
David Crow3-Aug-07 3:52
David Crow3-Aug-07 3:52 
AnswerRe: Print value of key pressed...? Pin
bob169723-Aug-07 17:39
bob169723-Aug-07 17:39 
Questionaccess violation in int array Pin
George_George2-Aug-07 16:53
George_George2-Aug-07 16:53 
AnswerRe: access violation in int array Pin
Sameerkumar Namdeo2-Aug-07 17:17
Sameerkumar Namdeo2-Aug-07 17:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.