وارد کردن فایل اکسل در دیتاگریدویو

یک پروژه ویندوزی مانند شکل زیر ایجاد کنید :

import-the-excel-file-into-the-datagridview-csharp-01

یک فایل اکسل در پوشه bin پروژه با نام Book1.xlsx قرار دهید.
در قسمت SolutionEplorer بر روی راست کلیک  refrence  و سپس بر روی Add Refrences کلیک کنید :

import-the-excel-file-into-the-datagridview-csharp-02

یک پنجره به صورت زیر باز می شود ، مسیرهای نمایش داده شده را طی کنید :

import-the-excel-file-into-the-datagridview-csharp-03

حال به محیط کدنویسی رفته و کدهای زیر را کپی کنید:

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 


        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(Application.StartupPath + "\\Book1.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;

            for (int columnCounter = 1; columnCounter <= range.Columns.Count; columnCounter++)
            {
                this.dataGridView1.Columns.Add(xlWorkSheet.Cells[1, columnCounter].Value2, xlWorkSheet.Cells[1, columnCounter].Value2);
            }
            //get the value of each excel cell and put into this variable
            string str;
            //Loop counters
            int rowCounter = 0;
            int columnCounter2 = 0;

            for (rowCounter = 2; rowCounter <= range.Rows.Count; rowCounter++)
            {
                //Create a new row into the datagridview
                this.dataGridView1.Rows.Add();
                for (columnCounter2 = 1; columnCounter2 <= range.Columns.Count; columnCounter2++)
                {
                    str = Convert.ToString((range.Cells[rowCounter, columnCounter2] as Excel.Range).Value2);
                    //add the value of excel file into the new row
                    this.dataGridView1.Rows[rowCounter - 2].Cells[columnCounter2 - 1].Value = str;
                }
            }
            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }
    }
}

برنامه را اجرا و نتیجه را مشاهده کنید :

import-the-excel-file-into-the-datagridview-csharp-04