Excel 2007 arrays -> visual C++

Excel 2007 arrays -> visual C++

Postby tallent_e » Tue Nov 09, 2010 2:44 pm

Bonjour ! Mr Duffy and everyone .



Thank you for your books.

I get in touch with you again.



I want to ask you questions regarding the COM interfacing between visual c++ and Excel 2007.

I hope you will have time to answer.



////



I aim at sourcing arrays out of Excel (those are numerical arrays) . And, turning them into c++ vector so that to ?play? with the number in question.



For now, I wish to print out those ranges in a c++ console window, as well as the value of some Excel cells (very basic stuff to start with !)



My Excel ranges look like this : cf the attached image (source file : C:\Users\?douard\Desktop\test.xlsx)









My c++ code (gathering together different pieces of code found here and there) :





#include <iostream>

#include <fstream>

#include<stdio.h>

#include <cstdlib>

#include <vector>



using namespace std;



// Microsoft Office Objects

#import \

"C:\Program Files\Common Files\Microsoft Shared\OFFICE12\mso.dll" \

rename("DocumentProperties", "DocumentPropertiesXL") \

rename("RGB", "RBGXL")



using namespace Office;



// Microsoft VBA Objects

#import \

"C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\vbe6ext.olb"



using namespace VBIDE;



// Excel Application Objects

#import \

"C:\Program Files\Microsoft Office\OFFICE12\EXCEL.EXE" \

rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \

rename("DocumentProperties", "DocumentPropertiesXL") \

rename("ReplaceText", "ReplaceTextXL") \

rename("CopyFile", "CopyFileXL") \

exclude("IFont", "IPicture") no_dual_interfaces





class ExcelUtils

{

public:

// Convert Excel range to vector with doubles

static vector<double> ExcelRangeTovector(Excel::RangePtr pRange);

}

;



// Convert an Excel range to STL vector template class

vector<double> ExcelUtils::ExcelRangeTovector(Excel::RangePtr pRange)

{

// Get the number of rows and columns in the range

int columns=pRange->Columns->Count;

int rows=pRange->Rows->Count;



// Create the vector with the correct size

vector<double> v(columns*rows);



// Iterate the rows and columns

int index=0;

for (int r=1; r<=rows; r++)

{

for (int c=1; c<=columns; c++)

{

// Add each element in the range to our vector

v[index++]=(((Excel::RangePtr) pRange->Item[r][c])->Value).dblVal;

}

}



// Return the vector

return v;

}





int main ()

{

//Initialise the COM interface

CoInitialize(NULL);

//Define a pointer to the Excel application

Excel::_ApplicationPtr XL;

//Start one instance of Excel

XL.CreateInstance(L"Excel.Application");

// pointeur vers fichier excel

XL->Workbooks->Open(L"C:\\Users\\?douard\\Desktop\\test.xlsx");

//Get a pointer to the 1st worksheet

Excel::_WorksheetPtr pSheet = XL->Sheets->Item[1];

// Activer la 1?re feuille

pSheet->Activate();

//Get a pointer to the cells on the active worksheet

Excel::RangePtr pRange = pSheet->Cells;

// Convert input to vector





As you guessed nothing happens as the ExcelRangeTovector function is even not called ! :P



My questions :





1) Has my main function have to get through all the steps as above ?



2) Do I actually have to active my pSheet ?



3) , how do I call the ExcelRangeTovector function ? and how do I get the c++ vector on my screen ?



4) How to get let?s say the value of the Excel cell B3 ?







Would you be kind to modify my c++ so that to get what I want ?

Thank you very much, indeed. :oops:



N.B I attach a Word file, where the c++ code lines may read more clearly. [/i]
Attachments
my test excel file.docx
a printout of my excel file
(19.24 KiB) Downloaded 522 times
D.Duffy Excel c++.docx
my messages in Word format
(32.62 KiB) Downloaded 585 times
tallent_e
 
Posts: 5
Joined: Tue Nov 09, 2010 1:52 pm
Location: France

Postby Cuchulainn » Wed Nov 17, 2010 12:35 am

Merci,

I will have a look and get back to you.



BTW which book did you take the code from? Did you chek out ExcelDriver.cpp and ExcelMechanism.cpp?



They answer your questions, to a large extent.
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby tallent_e » Mon Nov 29, 2010 5:34 pm

bonjour mr duffy.



i built the code from pieces i found here and there over the internet.

fr exemple : A brief introduction to C++ and Interfacing with Excel.



i am going to have a look at ExcelDriver.cpp and ExcelMechanism.cpp, right now !



?douard
tallent_e
 
Posts: 5
Joined: Tue Nov 09, 2010 1:52 pm
Location: France

Postby Cuchulainn » Mon Nov 29, 2010 9:15 pm

ed

So you are working with ILLEGAL SOFTWARE??. You are supposed to purchase my book.



Cannot support.





Daniel
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

what ?

Postby tallent_e » Tue Nov 30, 2010 10:15 pm

mr duffy.



what's wrong did i say ?



i bought Financial Instrument Pricing Using C++ few weeks ago. it is second-hand. but, i didn't steal anything. (if that actually is illegal, i am very sorry but i genuinely didn't know. here in france, there are libraries for students specialized in second-hand books. is that business all unlawful ?? ) .



in my previous post, i tried to explain that i have searched really quick and easy solutions over the internet.

here are some web sites that i consulted :

http://www.codeproject.com/KB/wtl/WTLExcel.aspx

http://www.codeproject.com/Messages/321 ... w-iCo.aspx

www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf



i am not satisfied with what i have had until now. in my previous post, i thus concluded that i would have a look at the excel driver files that you had mentionned. Let me have a look at the table of content, the interfacing between excel and c++ is studied from chapter 28 onwards of Financial Instrument Pricing Using C++ . i simply didn?t reach those last chapters of your books, yet. and, that?s all ! and, i realised afterwards that i don't have the ExcelMechanism.cpp file.



here i don't mean to sound unpolite. but, i want to explain that i didn't do anything wrong. i have too much respect for you, the knowledge that you share and your insights on the forums your participe to. besides, i can't stand feeling ashamed for all that, and i wish i didn't post anything. i have never illegally obtained nor a copy of a book, nor a movie, nor a piece o music.



kind regards,

?douard.
tallent_e
 
Posts: 5
Joined: Tue Nov 09, 2010 1:52 pm
Location: France

Postby Cuchulainn » Wed Dec 08, 2010 11:39 am

?douard,

Maybe a misunderstanding. I thought you said you plucked my code from Internet, which was not so.



Going back to the original post, it is better to use the high-level Excel functionality ExcelMechanisms from my book.



Apologies, no offence intended.
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby crossxlui » Sun Jan 01, 2012 10:07 am

Hi I am not sure whether it's appropriate to revive ths thread. Am I correct to say that financial instruments using c++ book teaches you how to interface c++ with excel? Do you have Some other things like library building and API linking up?



Cheers

Ken
crossxlui
 
Posts: 2
Joined: Wed Apr 27, 2011 6:23 pm

Postby Cuchulainn » Tue Jan 03, 2012 5:47 pm

There is a chapter on this , but is somewhat outdated.



The Monte Carlo C++ with Dr. Kiienitz is a recent update.
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands


Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron