You are here:   Blogs
Register   |  Login

How to use Nasosoft Excel in C++/COM

Minimize
Location: Blogs.NET Programming    
Posted by: host 12/2/2010 8:08 PM

Nasosoft Excel provides COM+ interfaces for C++/COM interop. You can use these API in your C++ applications to manage Excel documents, without Microsoft Office installed on your servers.

Step 1:

Register Nasosoft.Documents.Xls.dll with Regasm.exe.  Run:

Regasm.exe Nasosoft.Documents.Xls.dll

Step 2:

Refer the Nasosoft.Documents.Xls.tlb file in your C++ project

Step 3:

Initialize COM+, and get a pointer to IXlsDocumentBuilder instance

::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 IXlsDocumentBuilderPtr comObj(__uuidof(XlsDocumentBuilder));

Step 4:

Dispose the instance and Uninitialize COM+.

Code:

#include "stdafx.h"
#include
#include
#include
#include
#import "mscorlib.tlb"
#import "System.Drawing.tlb"
#import "D:\\nasosoft\\private\\Runtime\\public\\Net 2.0\\Nasosoft.Documents.Xls.tlb" no_namespace
int _tmain(int argc, _TCHAR* argv[])
{
 
 ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 IXlsDocumentBuilderPtr comObj(__uuidof(XlsDocumentBuilder));
 
 if(comObj)
 {
  comObj->SetLicense(L"D:\\nasosoft\\license\\Nasosoft.All.lic");
  IXlsWorkbookPtr workbook= comObj->Create();
  IXlsWorksheetCollectionPtr worksheets = workbook->GetWorksheets();
  IXlsWorksheetPtr worksheet = worksheets->GetItem_2(0);
  
  _bstr_t s = worksheet->GetText(1, 1);
  worksheet->SetText(1, 2, L"hello cpp interop!");
  
  workbook->Save_2(L"d:\\hello3.xls");
  
  workbook->Release();
  comObj.Release();
 }

 ::CoUninitialize();
 return 0;
}

Permalink |  Trackback