Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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
Updated 9-Nov-12 2:36am
v4

Use this tool: http://exceldatareader.codeplex.com/[^]

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

to show the excel content, you can make this:

C#
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());
    }
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 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 18-Mar-13 13:50pm    
I just read the question ;X
christmars 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 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...
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.
 
Share this answer
 
Comments
christmars 22-Mar-13 8:34am    
Thank you for the google-link! :P

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900