簡體   English   中英

使用Interop C#將Excel數據導入到DataGrid

[英]Importing excel data to datagrid using interop C#

我剛開始將excel數據導入到datagrid中。我找到了一個代碼並在系統上進行了嘗試,但是我在datagrid中得到了任何東西,有人可以告訴我我在這里做錯了什么。 另外,如果任何人都有有效的代碼,您可以與我分享。 這是我的代碼

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;



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

        private void Form1_Load(object sender, EventArgs e)
        {
            ArrayList test = ProcessWorkbook("C:\\Users\\s_kamalaksha_prabhu\\Desktop\\Book1.xlsx");
            if (test != null)
                dataGridView1.DataSource = test;

        }


        public ArrayList ProcessWorkbook(string filePath)
        {

            string file = filePath;

            Microsoft.Office.Interop.Excel.Application excel = null;
            Microsoft.Office.Interop.Excel.Workbook wkb = null;
            ArrayList al = new ArrayList();
            try
            {
                excel = new Microsoft.Office.Interop.Excel.Application();

                wkb = ExcelTools.OpenBook(excel, file, false, true, false);

                Microsoft.Office.Interop.Excel.Worksheet sheet = wkb.Sheets["Employees$"] as Microsoft.Office.Interop.Excel.Worksheet;

                Microsoft.Office.Interop.Excel.Range range = null;

                if (sheet != null)
                    range = sheet.get_Range("A1:X6702", System.Type.Missing);


                if (range != null)
                {
                    foreach (Microsoft.Office.Interop.Excel.Range r in range)
                    {
                        al.Add(r.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                //if you need to handle stuff
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (wkb != null)
                    ExcelTools.ReleaseRCM(wkb);

                if (excel != null)
                    ExcelTools.ReleaseRCM(excel);
            }
            return al;
        }

        //----------------
        public static class ExcelTools
        {
            public static Microsoft.Office.Interop.Excel.Workbook OpenBook(Microsoft.Office.Interop.Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
            bool updateLinks)
            {
                Microsoft.Office.Interop.Excel.Workbook book = excelInstance.Workbooks.Open(
                    fileName, updateLinks, readOnly,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
                return book;
            }

            public static void ReleaseRCM(object o)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                catch
                {
                }
                finally
                {
                    o = null;
                }
            }
        }

    }
}

謝謝。

我不知道這應該做什么:

foreach (Microsoft.Office.Interop.Excel.Range r in range)
{
    al.Add(r.Text);
}

如果要遍歷單元格,請使用range.Cells。 對於行,請使用range.Rows。我想如果要填充網格,則需要執行以下操作:

foreach (Microsoft.Office.Interop.Excel.Range row in range.Rows)
{
    // Add a new row
    foreach (Microsoft.Office.Interop.Excel.Range cell in row.Cells)
    {
       // Write to the cells' column on the current row
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM