Click here to Skip to main content
15,860,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want users can be able to import some data from their excel files so i use this

// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



// Set filter for file extension and default file extension
dlg.DefaultExt = ".txt";
dlg.Filter = "EXCEL Files (*.xls)|*.xlsx";


// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();


// Get the selected file name and display in a TextBox
if (result == true)
{
    // Open document
    string filename = dlg.FileName;


By this they can browse their excel file. But how can i make the WPF import the data
from the selected excel file?

Thanks.
Posted
Comments
Richard C Bishop 7-Feb-13 12:20pm    
You already posted this thread. Next time use the "Improve question" widget to alter your current thread to avoid confusion.

1 solution

 
Share this answer
 
Comments
Hslldm 7-Feb-13 14:14pm    
How do i use that code?
Hslldm 7-Feb-13 15:18pm    
This is not really what i want. I want choose the excel file when program runs.
Andreas Gieriet 7-Feb-13 15:36pm    
From your answer I must conclude that you don't understand the problem (I may be wrong, though):
1) you can not magically "display" the content of an excel file in WPF - you have to somehow read the data, convert to some C# data structure and again pass that read data to WPF (e.g. in a data grid) for display.
2) the suggested solution in the link above is the Microsoft way to achieve 1) above.
3) you might find some 3rd party libraris that read in an excel file without having Excel installed on that machine - you have to search the web for that and maybe spend a bit of money for it.

What you show us in your example code in the question is only the simple FileOpenDialog handling - that's the easy part compared to reading Excel data and display. Pass your file path you got from the dialog to the given solution above.

Or am I missing something?

Cheers
Andi
Hslldm 8-Feb-13 8:03am    
Yes you're right. I am new at C# so i dont know exactly where to look at? All i want users choose the excel file by this dialog, and the data from the selected excel files, and i want the data imported to my GridControl

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