Click here to Skip to main content
Rate this: bad
good
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
v4
Rate this: bad
good
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.
  Permalink  
Comments
christmars at 22-Mar-13 8:34am
   
Thank you for the google-link! :P
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use this tool: http://exceldatareader.codeplex.com/[^]
 
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;
try
{
    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);
    else
	    return;
 
    excelTable = excelReader.AsDataSet().Tables[0];
    if (excelTable == null || excelTable.Rows.Count == 0 || excelTable.Columns.Count == 0)
        throw new Exception("Error...");
 
    excelReader.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    return;
}
 
for (int i = 0; i < excelTable.Rows.Count; i++)
{
    for (int j = 0; j < excelTable.Columns.Count; j++)
    {
        Console.WriteLine(excelTable.Rows[i][j].ToString());
    }
}
  Permalink  
v2
Comments
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 Maciej Los 424
1 ridoy 410
2 DamithSL 240
3 OriginalGriff 179
4 Suvendu Shekhar Giri 150
0 OriginalGriff 8,033
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,538
3 Maciej Los 5,508
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
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