Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,
I'm new in vc++ mfc. I have to read records[column by column] from an excel file and perform some action. I can write txt file.
Can you provide me this for .txt and .xls format.
on that particular records.
Pl help me.
Thanks in advance.

What I have tried:

C++
void writeHistoryFile(CString openTime, CString action,CString actionTime,CString count)
{
	globalCount = atoi(count);
	CString LogPath = utils::getTempFolder();
	FILE* wFile = NULL;
	wFile = fopen(LogPath + "\\AnakageHistory.ll","a");
	if (wFile != NULL)
	{
		fprintf(wFile,openTime);
		fprintf(wFile,"\n");
		fprintf(wFile,"User did \""+action+"\" action on = "+actionTime);
		fprintf(wFile,"\n");
		fprintf(wFile,count);
		fprintf(wFile,"\n");
		CString success = isRegistedSuccess?"true":"false";
		fprintf(wFile,"Ragistration = "+success);
		fprintf(wFile,"\n");
		fprintf(wFile,"--------------------------------------------------");
		fprintf(wFile,"\n");
		fclose(wFile);
	}
}
Posted
Updated 30-Jan-17 23:29pm
v2
Comments
Richard MacCutchan 31-Jan-17 4:55am    
Where is the code to read the Excel file?

At first writing C++ code for interaction with Excel isnt a beginners task, so be prepared for a lot to learn and some weired stuff like COM.

The basic tasks are importing the Excel interface via a COM dll and using the correct namespace. After that you can access an Excel file in the specified way. Take a look at the article BasicExcel to learn it.

A tip beside: with C# the interaction with Excel is a lot easier. The reason is, that in C# you are in the .net-runtime which seamlessly use the COM-interface. The article Excel Reader shows how easy it works. It shows the power of the net-runtime and C#.
 
Share this answer
 
Excel files are binary and not text.

I know four methods to read and write Excel files:

  • Using Excel Automation (COM / OLE Dispatch) with the Excel type library. This requires an installed Excel on the machine running your application and the Excel type library on the development machine. See Office Automation Using Visual C++[^].
  • Using ODBC. Open Database Connectivity is an operating system independent standard for database management. With Excel ODBC drivers, it can be used to read and write Excel files. Because it is a database interface, it handles sheets from an Excel file like tables in a database file. The required Excel ODBC driver is installed by the Windows Setup.
  • Using ADO (OLE DB). ActiveX Data Objects is like ODBC a database interface, handling sheets from an Excel file like tables in a database file. It is more up-to-date than ODBC. This requires the ADO DLL wich is installed by Windows Setup. ADO may be also used to connect to Excel files using the ODBC drivers using a different connect string.
  • Using a library that provides reading and writing Excel files like the ExcelFormat Library[^].
     
    Share this answer
     

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900