Step 1: ADD XAML CODE FOR LAYOUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <Grid> <Border BorderBrush="Black" BorderThickness="5"> <StackPanel HorizontalAlignment="Center"> <Border BorderBrush="Black" BorderThickness="0,0,0,5" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center" Width="890"> <WrapPanel HorizontalAlignment="Left" VerticalAlignment="Center"> <TextBox Height="30" Name="txtFilePath" Width="550" Margin="10,0,10,0" FontSize="15" Opacity="1" /> <Button Content="Browse Excel" Name="btnOpen" Width="120" FontSize="15" Height="30" FontWeight="Bold" Click="btnOpen_Click" /> <Button Content="Close" FontSize="15" Height="30" FontWeight="Bold" Name="btnClose" Width="75" Margin="110,0,0,0" Background="#FFEFE5E5" Click="btnClose_Click" /> </WrapPanel> </Border> <Label Name="txtIns" Content="View Uploaded records to DataGridView" FontSize="15" FontWeight="Bold" Margin="15" HorizontalAlignment="Center" /> <DataGrid AutoGenerateColumns="True" IsReadOnly="True" HorizontalAlignment="Center" Name="dtGrid" VerticalAlignment="Center" /> </StackPanel> </Border> </Grid> |
Step 2: C# CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.DefaultExt = ".xlsx"; openfile.Filter = "(.xlsx)|*.xlsx"; var browsefile = openfile.ShowDialog(); if (browsefile == true) { txtFilePath.Text = openfile.FileName; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 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; 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++) { try { strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2; strData += strCellData + "|"; } catch (Exception ex) { douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2; strData += douCellData.ToString() + "|"; } } strData = strData.Remove(strData.Length - 1, 1); dt.Rows.Add(strData.Split('|')); } dtGrid.ItemsSource = dt.DefaultView; excelBook.Close(true, null, null); excelApp.Quit(); } } private void btnClose_Click(object sender, RoutedEventArgs e) { this.Close(); } } |
Step 3: ADD Microsoft Office Assembly as follows
Output: