Excel Automation in vc++
-
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..
-
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..
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";