|
|
The easiest way i thinnk is:
Open the excel sheet programmetically, save it as .cvs or .csv format or whatever format you want.
To do that you need to improt excel class from type library.
For details go to MSDN.
Please let me know if you find the better way at soft.gandhi@gmail.com.
Anurag Gandhi.
http://www.softgandhi.co.nr
|
|
|
|
|
Using Excel Automation, do something like:
_Application app;
Workbooks books;
_Workbook book;
if (app.CreateDispatch("Excel.Application") == TRUE)
{
books = app.GetWorkbooks();
book = books.Open("c:\\book1.xls", ...);
book.SaveAs(COleVariant("c:\\book1.cvs"), ...);
app.Quit();
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
If I only has a HWND for an unknown window, and I want its icon, currently I use CWnd::GetIcon, but this requires the application to SetIcon previously, if not, the icon is incorrect, however, the icons on the corresonding buttons of taskbar are always correct, so how to get them?
|
|
|
|
|
try calling the GetClassLong() function with the value GCL_HICONSM.
|
|
|
|
|
|
i am new to vc++.Can anybody tell me how to read the data from an excel file using vc++.I heard it has to be changed to .cvs format.but i dont know that.....can anybody help me out
Thank in advance
|
|
|
|
|
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)" ;
}<br />
<br />
CExcel::~CExcel(void)<br />
{<br />
}<br />
<br />
int CExcel::CreateSheet(CString strFileName, CString strTableName, CString strColumns)<br />
{<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 />
<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 />
for(int iCount=0;iCount<500;iCount++)
m_database.ExecuteSQL(sql);<br />
m_database.Close();<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
|
|
|
|
|
thanks.... ill try with it
|
|
|
|
|
i was trying this code....
But for the select statement you need a table name. The excel sheet has multiple tables but does not have a name.So is there any programatic way of giving table name to the tables in the excel sheet...
or is there any other way of getting the data. i just want to fetch the data and manipulate on it.
void CReadExcelDlg::OnButton1()
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "ReadExcel.xls"; // the file name. Could also be something
// like C:\\Sheets\\WhatDoIKnow.xls
// Clear the contents of the listbox
m_ctrlList.ResetContent();
// Retrieve the name of the Excel driver. This is
// necessary because Microsoft tends to use language
// specific names like "Microsoft Excel Driver (*.xls)" versus
// "Microsoft Excel Treiber (*.xls)"
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// Blast! We didn´t find that driver!
AfxMessageBox("No Excel ODBC driver found");
return;
}
// Create a pseudo DSN including the name of the Driver and the Excel file
// so we don´t have to have an explicit DSN installed in our ODBC admin
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
TRY
{
// Open the database using the former created pseudo DSN
database.Open(NULL, false, false, sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL string
// Remember to name a section of data in the Excel sheet using
// "Insert->Names" to be able to work with the data like you would
// with a table in a "real" database. There may be more than one table
// contained in a worksheet.
sSql = "SELECT field_1, field_2 "
"FROM demo_table "
"ORDER BY field_1";
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
// Browse the result
while (!recset.IsEOF())
{
// Read the result line
recset.GetFieldValue("field_1", sItem1);
recset.GetFieldValue("field_2", sItem2);
// Insert result into the list
m_ctrlList.AddString(sItem1 + " --> "+sItem2);
// Skip to the next resultline
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: " + e->m_strError);
}
END_CATCH;
}
// Get the name of the Excel-ODBC driver
// Contibuted by Christopher W. Backen - Thanx Christoper
CString CReadExcelDlg::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// Get the names of the installed drivers
// ("odbcinst.h" has to be included )
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// Search for the driver...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
// Found !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
|
|
|
|
|
If you are creating the excel sheet with the Application provided by me, it will give the name to your sheet as you desire.
If you are using manually created excel sheet, You need to provide the name to the table of the sheet.
To do that, Select the columns to name or Select all your text including header.
In Insert Menu -> Name -> Define, You can give the name to your sheet.
I am using MS excel 2003. I don't know whether it is supported in lower versions. Please check it.
Anurag Gandhi.
http://www.softgandhi.co.nr
|
|
|
|
|
|
Hi,
I am facing a peculiar problem with vectors. I define a vector in my program with a size given by another variable. Works fine.
But the same thing, I try inside a structure gives me a copiler error of C2061: sytax error : identifier size
Code:
#include <vector>
void main()
{
int size =4;
vector<int> vect(size);
vect[0] = 3;
vect[1] = 2;
vect[2] = 1;
display(vect);
}
This works fine But below code doesnt
int gsize =3;
struct VectorStruct
{
vector<int> vect2(gsize); //error C2061: syntax error
// : identifier 'gsize'
};
and even if i try to give a vlaue instead of variable it fails
struct VectorStruct
{
vector<int> vect2(2); //error C2059: syntax error : 'constant'
};
Can someone help me out. I cant figure out what going wrong
Vinod
|
|
|
|
|
Please revise your post: it's not readable. We can't see any of the "<" or ">" symbols or what's between them.
Steve
|
|
|
|
|
You pass parameters to constructors of member variables in the class constructor, like:
struct VectorStruct
{
vector vect2;
VectorStruct() : vect2(2) {}
};
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Revised Post
Hi,
I am facing a peculiar problem with vectors. I define a vector in my program with a size given by another variable. Works fine.
But the same thing, I try inside a structure gives me a copiler error of C2061: sytax error : identifier size
Code:
#include
void main()
{
int size =4;
vector <int > vect(size);
vect[0] = 3;
vect[1] = 2;
vect[2] = 1;
display(vect);
}
This works fine But below code doesnt
int gsize =3;
struct VectorStruct
{
vector <int > vect2(gsize); //error C2061: syntax error
// : identifier 'gsize'
};
and even if i try to give a vlaue instead of variable it fails
struct VectorStruct
{
vector <int > vect2(2); //error C2059: syntax error : 'constant'
};
Can someone help me out. I cant figure out what going wrong
Thanks Peter for the suggestion, it works. Thanks a ton
Vinod
|
|
|
|
|
If I want the same thing to be in constructor of that structure what is the way
I tried..
int gsize =3;
struct VectorStruct
{
int size;
vector <int> vect2;
//VectorStruct():vect2(i){}; //this works
VectorStruct()
{ //constructor
size=gsize;
vect2(size);
}
}
This gives an compiler error,(error C2064: term does not evaluate to a function taking 1 arguments) howto give size in constructor? Any pointers..
|
|
|
|
|
To pass a parameter to the constructor of a member, you need to pass that parameter to the constructor of the class, e.g.
struct VectorStruct
{
vector <int> vect2;
VectorStruct(int size):vect2(size){}
}
use class:
int gsize =3;
VectorStruct vs(gsize);
VectorStruct vs1(6);
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
I am having trouble printing the value I just keyed in. However I am able to get the ascii value corresponding to it. Now how do I change that to text?
The code below does not work...
case WM_CHAR: {<br />
LPSTR s; s = (LPSTR) wParam; MessageBox(hwnd,s,"hi",MB_OK);<br />
}<br />
break;
|
|
|
|
|
// Try something like this...
case WM_CHAR: {
char s[2]={0};
s[0]=wParam;
::MessageBox(hwnd,s,"hi",MB_OK);
}
break;
|
|
|
|
|
Now how can I keep compounding all those keystrokes into a string variable...? I've tried strcat() - does not work!
|
|
|
|
|
Since s[0] = wParam will always add to the first position in the array, replace the 0 with an actual variable.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Are you using MFC? If so, using a CString to concatenate is the simplest approach. Not the most efficient but the easiest to implement.
It's += operator handles TCHAR and LPCTSTR as well as other CString references.
CString is really the only reason I ever started using MFC in the first place.
|
|
|
|
|
Hello everyone,
There is error message when executing my program,
Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.
It is very simple, does anyone know what is wrong with the program?
I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
foo.c
<br />
int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};<br />
goo.c
<br />
extern int* p_int;<br />
<br />
int main (int argc, char** argv)<br />
{<br />
int i;<br />
int sum = 0;<br />
for (i = 0; i < 16; i++)<br />
{<br />
sum += p_int [i];
}<br />
<br />
return 0;<br />
}<br />
thanks in advance,
George
|
|
|
|
|
What is the address of p_int (in main()) for both the cases.
|
|
|
|
|