Click here to Skip to main content
12,249,821 members (55,966 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
HI

I want to display data on an excel spreadsheet in DataGridView please am using C#.
Posted 1-Jan-13 23:10pm
Jnrpsy208
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

I used this a long time back in my project, maybe it will help:

public ArrayList ProcessWorkbook(string filePath)
        {
            string file = filePath;
 
            Excel.Application excel = null;
            Excel.Workbook wkb = null;
            ArrayList al = new ArrayList();
            try
            {
                excel = new Excel.Application();
 
                wkb = ExcelTools.OpenBook(excel, file, false, true, false);
 
                Excel.Worksheet sheet = wkb.Sheets["Adresses"] as Excel.Worksheet;
 
                Excel.Range range = null;
 
                if (sheet != null)
                    range = sheet.get_Range("A1:X6702", Missing.Value);
 

                if (range != null)
                {
                    foreach (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 Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
        bool updateLinks)
        {
            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;
            }
        }
    }
  Permalink  
Comments
Marcus Kramer 2-Feb-13 11:10am
   
Plagiarized from Source and OP makes a blatant claim that it's his own.

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
Web01 | 2.8.160426.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2016
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