Write Data from managed C++ (VS2003 + PIA) into Excel 2003

G

Guest

I have to write a lot of data to excel for analysts who use it further from
there but would like to use managed C++ and not C# since a lot of code is
involved which already exists now in managed C++.

In C# I could handle it easily as you can see in this sample:

using Excel = Microsoft.Office.Interop.Excel;
....
private Excel.Application thisApplication;
private Excel.Workbook myWorkbook1;
private Excel.Worksheet theSheet1;
....
void simple_function()
{
thisApplication = new Excel.ApplicationClass();
thisApplication.Visible = true;
myWorkbook1 = thisApplication.Workbooks.Add(Type.Missing);
theSheet1 = (Excel.Worksheet) myWorkbook1.ActiveSheet;

thisApplication.ScreenUpdating = false;
for(int i=1;i<=20;i++)
theSheet1.Cells[i,3] = i;
thisApplication.ScreenUpdating = true;

}

In managed C++ (VS2003) I can do almost the same but get a certain error
message regarding pointer arithmetic during compilation:

using namespace Microsoft::Office::Interop::Excel;
....
private: Microsoft::Office::Interop::Excel::Application * myExcelApplication1;
private: Microsoft::Office::Interop::Excel::Workbook * myWorkbook1;
private: Microsoft::Office::Interop::Excel::Worksheet * theSheet1;
....
void simple_function()
{
myExcelApplication1 = new
Microsoft::Office::Interop::Excel::ApplicationClass();
myExcelApplication1->Visible = true;
myWorkbook1 = myExcelApplication1->Workbooks->Add(Type::Missing);

theSheet1 = dynamic_cast<Microsoft::Office::Interop::Excel::Worksheet *>
(myWorkbook1->ActiveSheet);

myExcelApplication1->ScreenUpdating = false;
for(int i=1;i<=20;i++)
theSheet1->Cells[i,3] = i;
myExcelApplication1->ScreenUpdating = true;
}

During compilation regarding the statement
theSheet1->Cells[i,3] = i;
I get the errormessage
error C2845: '[' : cannot perform pointer arithmetic on __gc pointer
'Microsoft::Office::Interop::Excel::Range __gc *'

Is someone able to help me walk around that problem or do I have to take
other measures to proceed?
Thank you for all your help in advance.
Hans from Vienna
 
J

Jens Thiel

Hello Hans,

try calling the indexer directly, e.g. something like
theSheet1->Cells->get_Item(i,3).

Jens.
 
G

Guest

Thanks a lot Jens, you solved the problem.
The answer in C++ is:

thesheet1->Cells->set_Item(__box(i), __box(3),__box(i) );

Thank you for your effort.
Hans Wien


Jens Thiel said:
Hello Hans,

try calling the indexer directly, e.g. something like
theSheet1->Cells->get_Item(i,3).

Jens.

--
http://ManagedXLL.net/ | http://www.stochastix.de/
Replace MSDN with my first name when replying to my email address!

Hans Wien said:
I have to write a lot of data to excel for analysts who use it further from
there but would like to use managed C++ and not C# since a lot of code is
involved which already exists now in managed C++.

In C# I could handle it easily as you can see in this sample:

using Excel = Microsoft.Office.Interop.Excel;
...
private Excel.Application thisApplication;
private Excel.Workbook myWorkbook1;
private Excel.Worksheet theSheet1;
...
void simple_function()
{
thisApplication = new Excel.ApplicationClass();
thisApplication.Visible = true;
myWorkbook1 = thisApplication.Workbooks.Add(Type.Missing);
theSheet1 = (Excel.Worksheet) myWorkbook1.ActiveSheet;

thisApplication.ScreenUpdating = false;
for(int i=1;i<=20;i++)
theSheet1.Cells[i,3] = i;
thisApplication.ScreenUpdating = true;

}

In managed C++ (VS2003) I can do almost the same but get a certain error
message regarding pointer arithmetic during compilation:

using namespace Microsoft::Office::Interop::Excel;
...
private: Microsoft::Office::Interop::Excel::Application *
myExcelApplication1;
private: Microsoft::Office::Interop::Excel::Workbook * myWorkbook1;
private: Microsoft::Office::Interop::Excel::Worksheet * theSheet1;
...
void simple_function()
{
myExcelApplication1 = new
Microsoft::Office::Interop::Excel::ApplicationClass();
myExcelApplication1->Visible = true;
myWorkbook1 = myExcelApplication1->Workbooks->Add(Type::Missing);

theSheet1 = dynamic_cast<Microsoft::Office::Interop::Excel::Worksheet *>
(myWorkbook1->ActiveSheet);

myExcelApplication1->ScreenUpdating = false;
for(int i=1;i<=20;i++)
theSheet1->Cells[i,3] = i;
myExcelApplication1->ScreenUpdating = true;
}

During compilation regarding the statement
theSheet1->Cells[i,3] = i;
I get the errormessage
error C2845: '[' : cannot perform pointer arithmetic on __gc pointer
'Microsoft::Office::Interop::Excel::Range __gc *'

Is someone able to help me walk around that problem or do I have to take
other measures to proceed?
Thank you for all your help in advance.
Hans from Vienna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top