How to Read Input from Excel File Using Borland C++ Builder

Borland C++ Builder (BCB) is a very useful object oriented programming language and builder for a small up to a large scale software development. Unfortunately, there is no available introductory level tutorial tool for the use of this programming tool. I had found one from the internet, www.functionx.com, but it only provides a limited amount of tutorial for some of BCB`s objects. The second unfortunate thing about the use of this tool is that majority of the forums available are non-English so its hard to understand what they are discussing about although I know that these are all essentials.

The very recent thing that I want to know is how to read an input from an excel file using Borland C++. I want to shift to this level because it is to messy to read input data from a text file specially if it is numerical data and of relatively large size. And after doing tedious internet research, I finally come up with the following code. I provide some comments to help anyone who read this to understand this crap.

//declaration of variables of type variant
Variant XL,v0,v1,vcell;

//a string where you will temporarily put the content of a single Cell
AnsiString tmp;

//create an object which is an excel application and store it to XL
XL=Variant::CreateObject(”excel.application”);

//Set the Excel Application as invisible once you have opened it
XL.OlePropertySet(”Visible”,false);

//Get the workbooks while has a path stored in “file” variable and open it.
XL.OlePropertyGet(”Workbooks”).OleProcedure(”Open”,file);

//Get the Sheet which has a title “Sheet1″
v0=XL.OlePropertyGet(”Sheets”,”Sheet1″);

//Get the Cells of that particular Sheet.
v1=v0.OlePropertyGet(”Cells”);

//Get the content of the Cell located at row 2 and column 3
vcell=v1.OlePropertyGet(”Item”,2,3);

//store that content to ansistring “tmp”
tmp=vcell.OlePropertyGet(”Value”);

That’s all. You can loop the,

vcell=v1.OlePropertyGet(”Item”,2,3);
tmp=vcell.OlePropertyGet(”Value”);

if you want to copy more cells.
The content stored in “tmp” for empty cell is “(null)”

If done, don`t forget to close the excel application using this code;

XL.OleProcedure(”Quit”);
XL=Unassigned;



Filed in: C++

»Next article in C++: How to write output on excel file using Borland C++

Search This Site

 
Web www.jcmiras.net

Sponsored Links


Translations

English flagItalian flagKorean flagChinese (Simplified) flagPortuguese flag
German flagFrench flagSpanish flagJapanese flagArabic flag
Russian flagHindi flag   
By N2H