CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2016
    Posts
    2

    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..

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    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
    Nobody cares how it works as long as it works

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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/
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    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.
    Last edited by new_2012; January 12th, 2016 at 02:29 AM.

  5. #5
    Join Date
    Jan 2016
    Posts
    2

    Thumbs up 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured