Click here to Skip to main content
11,479,951 members (57,608 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++/CLI .NET CLR WinForm , +
Hello mates,

in my testing program, I'm going to get values from an excel file into my windows forms appilication under C++CLI/CLR.

The excel file looks like this:
No.  peronal Number     Name    Age   Height   other info
1    1001               Tom     10    153cm    ...
2    1105               Jack    11    158cm    ...
3    1320               Ben     13    160cm    ...
.                       .       .     .        .
.                       .       .     .        .
.                       .       .     .        .
100  1213               George  12    162cm    ...
The informations of the children should be showed after I give the number 1...100 in a textBox and click a button1. For example, when I give a "3" into the textBox,
and click button1, then the info of Ben should be showed on my windows form.

As far as I've serched in the internet. It should be handled with use of OleDb or ADO or namespace "Microsoft::Office::Interop::Excel". I've choosed the "Microsoft::Office::Interop::Excel". But I don't know how to get values from a exited xls-file.
I coded this to open an excel file on my windows form:
using namespace Microsoft::Office::Interop::Excel;
#define Excel Microsoft::Office::Interop::Excel
Excel::Application^ xlApp =gcnew Excel::ApplicationClass();
Excel::Workbook^ mybook = xlApp->Workbooks->Open("C:\\test.xlsx",Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing,Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing,Type::Missing, Type::Missing);
xlApp->Visible = true;
This works well. I think now I need to use the WorkSheets->Cell[ ][ ] to get the values, or have I missunderstood it? Msdn doesn't give introductions for using this namespace detailly. Could you help?

And I have another question to the WinForm experts, a textBox question:

I've coded to limit the typing of textBox only with numbers and backspace.
But when I type "01" or "0021" etc. such numbers with one or more zeros as beginning,
it shows no error. But I want to make limit for that. How could I make it?

Thank you in advance!
Posted 8-Nov-12 22:53pm
Edited 9-Nov-12 3:36am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Some suggestions[^] about using Excel from C++/CLI. I cannot suggest which is the best but they should get you started with accessing the data.

As to limiting the content of a textbox, you need to have a delegate method that gets called on the entry of each character, which can validate the content. Take a look at the events in the textbox properties to decide which one to use.
christmars at 22-Mar-13 8:34am
Thank you for the google-link! :P
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use this tool:[^]

IT's extreme mor easy to read the excel file Smile | :)

to show the excel content, you can make this:

DataTable excelTable = null;
IExcelDataReader excelReader = null;
    FileStream stream = File.Open(inputFileName, FileMode.Open, FileAccess.Read);
    if (Path.GetExtension(inputFileName).ToLower() == ".xls")
        excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    else if (Path.GetExtension(inputFileName).ToLower() == ".xlsx")
        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    excelTable = excelReader.AsDataSet().Tables[0];
    if (excelTable == null || excelTable.Rows.Count == 0 || excelTable.Columns.Count == 0)
        throw new Exception("Error...");
catch (Exception ex)
for (int i = 0; i < excelTable.Rows.Count; i++)
    for (int j = 0; j < excelTable.Columns.Count; j++)
Richard MacCutchan at 18-Mar-13 13:42pm
There is really no point in posting answers to questions that are more than a month old. It is obvious that the OP lost interest in this.
Alexandre Bencz at 18-Mar-13 13:50pm
I just read the question ;X
christmars at 22-Mar-13 8:32am
>There is really no point...
there's still point for codeproject here, maybe somebody else needs the answer. It's not only for the OP.
And I have to say, i would be always glad to learn what I still don't know! ;)
christmars at 22-Mar-13 8:26am
Hi there,
thank you, this tool is cool!
But i already made an own prog to realise it. ;)
Sorry that my answer is a bit later...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 235
1 Sergey Alexandrovich Kryukov 215
2 Richard MacCutchan 100
3 F-ES Sitecore 85
4 DamithSL 65
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100