How to create a Excel File(.XLSX) And(.XSL) through code in MFC
hiii can anybody help me here..
i am trying to create an excel file through code.but i am not able to..
i tried using CStdioFile class but its not working..
here is some part of my code..the one which fails,
try
{
Cstring Strname = path + s_excel; // FINDS .XSL FILE
lpdisp= books.open(strname,covOptional,covOptional,covOptional,covOptional,covOptional
covOptional,covOptional,covOptional,covOptional,covOptional);
assert(lpdisp);
}
catch(COleDispathException *e)
{
Cstring Strname = path + X_excel; // FIND .XLSX FILE
lpdisp= books.open(strname,covOptional,covOptional,covOptional,covOptional,covOptional
covOptional,covOptional,covOptional,covOptional,covOptional);
assert(lpdisp);
}
please help if anyone can..
Re: How to create a Excel File(.XLSX) And(.XSL) through code in MFC
It is called "OLE Automation" and there are plenty of examples available on the web. C++ examples are a little more scarce than VB and C#.
https://support.microsoft.com/en-us/kb/179706
http://forums.codeguru.com/showthrea...-save-workbook
Re: How to create a Excel File(.XLSX) And(.XSL) through code in MFC
Another possibility is creating it as a database file using corresponding connection string:
https://www.connectionstrings.com/excel/
Re: How to create a Excel File(.XLSX) And(.XSL) through code in MFC
Here is simple code for you.
This will generate simple .xls file .
You cant have colored records and all other facilities with this
Code:
void zzzDoc::OnOpenexcelfile()
{
// TODO: Add your control notification handler code here
filename = File Name you need to convert;
_finddata_t fr;
if (_findfirst(filename, &fr) == -1) {
AfxMessageBox("Can not convert new result file !!");
return;
}
CString qs;
qs = filename.xls ;
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // exactly the same name as in the ODBC-Manager
CString sExcelFile = qs ; // Filename and path for the file to be created
CString sSql;
long hFile;
_finddata_t fr1;
if (hFile = _findfirst(sExcelFile, &fr1) != -1) { // filealready exists
Remove(sExcelFile);
}
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// Create the database (i.e. Excel sheet)
if (database.OpenEx(sSql,CDatabase::noOdbcDialog)) {
sSql.Format("CREATE TABLE %s (Srno NUMBER,Book VARCHAR(20),Author VARCHAR (20),Time_Stamp VARCHAR(20))",sExcelFile);
database.ExecuteSQL(sSql);
sSql.Format("INSERT INTO %s (Srno, Book,Author ,Time_Stamp) VALUES (%d,%s,%s,%s)",sExcelFile,SrNo,"MyBook","My Author",timeBuf);
database.ExecuteSQL(sSql);
CString qqsw;
qqsw.Format("Coverting Record %d out of %d records",CurRecordNo,TotalRecords);
pDoc->DisplayStatusBar();
database.Close();
AfxMessageBox("Excel Conversion completed Successfully !");
}
include : #include <afxdb.h>
With OLE Automation you will have many other advantages.
Re: How to create a Excel File(.XLSX) And(.XSL) through code in MFC
Thank you Everyone for the help...
it did help me alot:)