Import Excel File to DataGrid in WPF

You may also like...

12 Responses

  1. Margery says:

    Heya i’m for the first time here. I came across
    this board and I find It really useful & it helped
    me out much. I hope to give something back and aid others
    like you aided me.

  2. Alvin says:

    Hello there I am so grateful I found your blog, I
    really found you by error, while I was looking on Digg for something
    else, Nonetheless I am here now and would just like to say thanks
    a lot for a incredible post and a all round exciting
    blog (I also love the theme/design), I don’t have time to read it all at the
    moment but I have book-marked it and also included
    your RSS feeds, so when I have time I will be back to read
    a lot more, Please do keep up the excellent b.

  3. Bev says:

    There is definately a lot to learn about this subject. I really like all the points you’ve made.

  4. Maher says:

    in WPF only I need to establish connection with SQL Express database.mdf through the path in app.config file
    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    and add in references … System.Configuration.dll
    public partial class MainWindow : Window
    public MainWindow()

    private void FillDataGrid()
    String ConString = ConfigurationManager.ConnectionStrings[“ConString”].ConnectionString;
    String CmdString = String.Empty;
    using (SqlConnection con = new SqlConnection(ConString))
    CmdString = “Select BookID, BookTitle, AuthorID from dbo.Table_1_Books”;
    SqlCommand cmd = new SqlCommand(CmdString, con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable(“Books”);
    DataGrid_Loaded.ItemsSource = dt.DefaultView;

    the error is from
    ConfigurationManager.ConnectionStrings[“ConString”].ConnectionString in wpf in vs 2013
    can’t fill data

    please I need zip file with sample wpf datagrid using ConfigurationManager in sql database with vs 2013

  5. Aravindh says:

    I can’t get images while using this code…can u plz reply how to import images from excel file??thanks

    • Admin says:

      You can’t directly retrieve image using the Excel Office interop. You have to send the image to clipboard and then get from there.
      You can try to check the following code.

      Excel.Picture pic = (Excel.Picture)CurSheet.Pictures(1);
      IDataObject data = Clipboard.GetDataObject();
      Image pic2 = (Image)data.GetData(DataFormats.Bitmap, true);
      pic2.Save("pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

      Hope this will help you..

  6. Aravindh says:

    Admin Sir,

    I m trying this code,but “pic2.Save(“pic.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg);” this line comes error..the error line is “ does not contain a definition for save”…..plz help me…

    thanks in advance!!

  7. Aravindh says:

    hello admin,

    I had used these code. I have a error .
    ERROR- Unable to cast object of type ‘System.Windows.Interop.InteropBitmap’ to type ‘System.Drawing.Image’. at ReadExcel_And_BindToDataGrid.MainWindow.btnOpen_Click(Object sender, RoutedEventArgs e) in j:\projects\dpIm\files\excel.xaml.cs:line 62

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Win32;
    using System.Data;
    using WpfApplication1;
    using System.Drawing;

    namespace ReadExcel_And_BindToDataGrid
    /// Interaction logic for MainWindow.xaml
    public partial class MainWindow : Window
    public MainWindow()
    //new Class1();


    private void btnOpen_Click(object sender, RoutedEventArgs e)
    OpenFileDialog openfile = new OpenFileDialog();
    openfile.DefaultExt = “.xls”;
    openfile.Filter = “(.xls)|*.xls”;

    var browsefile = openfile.ShowDialog();

    if (browsefile == true)
    txtFilePath.Text = openfile.FileName;

    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    //Static File From Base Path………..
    //Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + “TestExcel.xlsx”, 0, true, 5, “”, “”, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, 1, 0);
    //Dynamic File Using Uploader………..
    Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, “”, “”, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, 1, 0);
    Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
    Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;

    Microsoft.Office.Interop.Excel.Picture pic = (Microsoft.Office.Interop.Excel.Picture)excelSheet.Pictures(1);
    IDataObject data = Clipboard.GetDataObject();
    System.Drawing.Image pic2 = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
    pic2.Save(“pic.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg);

    string strCellData = “”;
    double douCellData;
    int rowCnt = 0;
    int colCnt = 0;

    DataTable dt = new DataTable();
    for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
    string strColumn = "";
    strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
    dt.Columns.Add(strColumn, typeof(string));

    for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
    string strData = "";
    for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
    strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
    strData += strCellData + "|";
    catch (Exception ex)
    MessageBox.Show(ex.Message + ex.StackTrace);
    douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
    strData += douCellData.ToString() + "|";
    strData = strData.Remove(strData.Length – 1, 1);


    dtGrid.ItemsSource = dt.DefaultView;

    excelBook.Close(true, null, null);

    catch (Exception exx)
    MessageBox.Show(exx.Message + exx.StackTrace);

    private void btnClose_Click(object sender, RoutedEventArgs e)


  8. Aravindh says:

    I am Waiting !….

    As soon as possible please provide the correct solution sir, I need to import excel sheet and add that value in grid or list view ,that sheet contain both text and images.

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">