Click here to Skip to main content
16,014,662 members

Comments by deepakkumargautam (Top 4 by date)

deepakkumargautam 19-Aug-10 6:34am View    
void g_fnWriteExcelFile(CSite *_objSite,char *_strPath)
{
char Range[7],bufCtr[10];
CString _strOutputName;
CoInitialize(NULL);
Excel::_WorkbookPtr workbook ;
Excel::_ApplicationPtr excel;
Excel::_WorksheetPtr worksheet ;
try
{
Excel::_ApplicationPtr excel ;
// Initialize Excel and make sure it's initialized
HRESULT hr = excel.CreateInstance(L"Excel.Application");
if(FAILED(hr))
{
return ;
}


workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook
worksheet = excel->ActiveSheet; // Get the active sheet


//tempstrPath = (char*)malloc(sizeof(_strPath));
_strOutputName = _objSite->m_strPrefix + "_" + "MappingResult";
worksheet->Name=(_bstr_t)_strOutputName;

strcat(_strPath,"\");

_strOutputName = _strOutputName + ".xlsx"; // excel 2007
strcat(_strPath,_strOutputName) ;



_variant_t var;

CDevice *_objDevice = _objSite->m_objDevice;
CDevicePoint *_objDevicePoint = NULL;

///////////////////////////////Header Start/////////////////////////////////////////
int nCounter = 1;

while(_objDevice!=NULL)
{
_objDevicePoint = _objDevice->m_objDevicePoint;
while(_objDevicePoint!=NULL)
{
if(nCounter == 2)
{
CString tempColor = "15" ;

strcpy(Range,"A");
itoa(nCounter,bufCtr,10);
strcat(Range,bufCtr);
worksheet->Range[Range]->Value2 = "Tag Name";
_objDevicePoint = _objDevicePoint->next ;
}
nCounter++;

}
_objDevice = _objDevice->next ;
}
worksheet->SaveAs(_strPath); // saving workshee
workbook->Close(); // Close the workbook
excel->Quit(); // Quit excel
}
catch(_com_error &ce)
{
// Handle the error
AfxMessageBox(ce.ErrorMessage()) ;

}
CoUninitialize();
}
deepakkumargautam 19-Aug-10 6:00am View    
Done that Shiva but stil getting some errorrs "steam underclared undentifier, same for FILE " ?
deepakkumargautam 19-Aug-10 5:34am View    
Hi Shiva,
Thanks for replying, I have embed the code but getting some errors. I guess I need to #include something ? specially for stream and FILE?
: I have accepted the answer, I am new @ codeproject so don't know much.

Thanks
deepakkumargautam 19-Aug-10 5:20am View    
workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook
worksheet = excel->ActiveSheet; // Get the active sheet

//some code//

worksheet->SaveAs(_strPath); // here comes the error when I try to save an
// excel file alraedy open