Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Excel Automation in vc++

Excel Automation in vc++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++testingtoolsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    ledallam
    wrote on last edited by
    #1

    Hi I am working with excel automation. I need to write an excel file from VC++. The format of the excel file should be Events Table Date&Time,Source,Cause,Description,Fault - 3rd row Mar 13 2006 12:25:05.000078,HMI,H-0001,Startup,N Mar 13 2006 12:25:05.000187,HMI,H-0005,Caution,Y ect... Footer I used excel automation as shown below. CoInitialize(NULL); try { Excel::_ApplicationPtr excel; // Initialize Excel and make sure it's initialized HRESULT hr = excel.CreateInstance(L"Excel.Application"); Excel::_WorkbookPtr workbook = excel->Workbooks->Add(static_cast (Excel::xlWorksheet)); Excel::_WorksheetPtr worksheet = excel->ActiveSheet; worksheet->Range["A1"]->Value = "Header1"; worksheet->Range["A2"]->Value = "Header2"; worksheet->SaveAs("c:\\test1.xls"); // Save it workbook->Close(); // Close the workbook excel->Quit(); // Quit excel } catch(_com_error &ce) { AfxMessageBox("Error"); } CoUninitialize(); Now the problem is, since I don't have the number of cells varry , I want to run a ForLoop where I can input the values in the Range function as shown here.....worksheet->Range["A1"]->Value = "Header1"; I am facing some problems when I try to give the values here as char* str = new char(3); str = "B3"; worksheet->Range[str]->Value = "Header1"; Can anyone please help me with some sample code for doing this? Thanks Madhavi..

    W 1 Reply Last reply
    0
    • L ledallam

      Hi I am working with excel automation. I need to write an excel file from VC++. The format of the excel file should be Events Table Date&Time,Source,Cause,Description,Fault - 3rd row Mar 13 2006 12:25:05.000078,HMI,H-0001,Startup,N Mar 13 2006 12:25:05.000187,HMI,H-0005,Caution,Y ect... Footer I used excel automation as shown below. CoInitialize(NULL); try { Excel::_ApplicationPtr excel; // Initialize Excel and make sure it's initialized HRESULT hr = excel.CreateInstance(L"Excel.Application"); Excel::_WorkbookPtr workbook = excel->Workbooks->Add(static_cast (Excel::xlWorksheet)); Excel::_WorksheetPtr worksheet = excel->ActiveSheet; worksheet->Range["A1"]->Value = "Header1"; worksheet->Range["A2"]->Value = "Header2"; worksheet->SaveAs("c:\\test1.xls"); // Save it workbook->Close(); // Close the workbook excel->Quit(); // Quit excel } catch(_com_error &ce) { AfxMessageBox("Error"); } CoUninitialize(); Now the problem is, since I don't have the number of cells varry , I want to run a ForLoop where I can input the values in the Range function as shown here.....worksheet->Range["A1"]->Value = "Header1"; I am facing some problems when I try to give the values here as char* str = new char(3); str = "B3"; worksheet->Range[str]->Value = "Header1"; Can anyone please help me with some sample code for doing this? Thanks Madhavi..

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      What exacty are the problems you are getting? I have excel up and running in my current project, but I used a slightly different method. I created a DLL coded in Visual Basic and included it into my project. As for your problem, instead of inserting the value using the range method, why not try the cell method instead? worksheet->Cell[inty,intX]->Value = "Header";

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups