Click here to Skip to main content
12,820,236 members (24,902 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF VS2012
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?

Posted 7-Feb-13 7:16am
richcb 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?

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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2017
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