Click here to Skip to main content
11,706,799 members (48,568 online)
Rate this: bad
good
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
Jnrpsy308
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 at 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
0 OriginalGriff 156
1 Sergey Alexandrovich Kryukov 155
2 chainerlt 110
3 ppolymorphe 91
4 Mika Wendelius 70
0 OriginalGriff 9,190
1 Sergey Alexandrovich Kryukov 8,407
2 CPallini 5,189
3 Maciej Los 4,756
4 Mika Wendelius 3,686


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
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