Hi,
I would like to create, modify insert del and read from excel files I found some articles on daniweb and other sites and even on msdn but they are very difficult and not complete.

I was wondering if someone could please give some tutorial link to explain how to get this done.

Thank you.

This is not my code but essentially this is what you need to do.

#import "C:\Program Files\Common Files\Microsoft Shared\Office11\mso.dll"

#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

#import "C:\Program Files\Microsoft Office\Office11\excel.exe" \

        rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \

        exclude("IFont","IPicture")

#include <stdexcept>

#include <iostream>

 

using namespace std;

using namespace Excel;

 

int main()

{

  // Initialize COM

  CoInitialize(NULL);

  try

  {

       _ApplicationPtr excel;

       

       

      //Excel::_ApplicationPtr excel;

      //excel->Visible(true);

      excel->put_Visible(10, true);

//    excel->put_Visible(true);

      // Initialize Excel and make sure it's initialized

    HRESULT hr = excel.CreateInstance(L"Excel.Application");

      //excel->visible = true;

 

      if(FAILED(hr))

    {

    char msg[1024] = {0};

    sprintf(msg, "E: There was an error initializing Excel: %d", hr);

      throw std::runtime_error(msg);

       }

     

      cout<< "HERE!";

    _WorkbookPtr workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook

      _WorksheetPtr worksheet = excel->ActiveSheet; // Get the active sheet

    // This is how you put the values into the worksheet

    worksheet->Range["A1"]->Value = "Hello"; // Set a value

     

    worksheet->SaveAs("c:\\test.xls"); // Save it

    workbook->Close(); // Close the workbook

    excel->Quit(); // Quit excel

  }

  catch(_com_error &ce)

  {

      cout<<"caught";

        // Handle the error

  }

  CoUninitialize();

 

 

  return 0;

 

  }

This is not my code but essentially this is what you need to do.

#import "C:\Program Files\Common Files\Microsoft Shared\Office11\mso.dll"

#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

#import "C:\Program Files\Microsoft Office\Office11\excel.exe" \

        rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \

        exclude("IFont","IPicture")

#include <stdexcept>

#include <iostream>

 

using namespace std;

using namespace Excel;

 

int main()

{

  // Initialize COM

  CoInitialize(NULL);

  try

  {

       _ApplicationPtr excel;

       

       

      //Excel::_ApplicationPtr excel;

      //excel->Visible(true);

      excel->put_Visible(10, true);

//    excel->put_Visible(true);

      // Initialize Excel and make sure it's initialized

    HRESULT hr = excel.CreateInstance(L"Excel.Application");

      //excel->visible = true;

 

      if(FAILED(hr))

    {

    char msg[1024] = {0};

    sprintf(msg, "E: There was an error initializing Excel: %d", hr);

      throw std::runtime_error(msg);

       }

     

      cout<< "HERE!";

    _WorkbookPtr workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook

      _WorksheetPtr worksheet = excel->ActiveSheet; // Get the active sheet

    // This is how you put the values into the worksheet

    worksheet->Range["A1"]->Value = "Hello"; // Set a value

     

    worksheet->SaveAs("c:\\test.xls"); // Save it

    workbook->Close(); // Close the workbook

    excel->Quit(); // Quit excel

  }

  catch(_com_error &ce)

  {

      cout<<"caught";

        // Handle the error

  }

  CoUninitialize();

 

 

  return 0;

 

  }

Thank u for your help, the above code seems to be in VB I need to code in c++. Please advice.

sorry, the said code is in c++ :) stupid me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.