Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I determine the end-of-file in a spreadsheet/excel application

How do I determine the end-of-file in a spreadsheet/excel application

Scheduled Pinned Locked Moved C#
questioncomdata-structureshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Walaza
    wrote on last edited by
    #1

    Hi, I developed an application that opens an excel doc, and reads/displays its contents to a datagrid. How do I determine its start and end rows? I have tried some code but it does not give me the result I need. e.g. I tried: //get start and end rows Object[] startRow = {worksheet.get_Range("A4", "O4")}; Object[] endRow = {worksheet.Rows.get_End(XlDirection.xlDown)}; which did not work for me. See below: // loop through all rows of the spreadsheet and place each row in the datagrid. for (int i = 4; i <= 50; i++) { Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "Z" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value2; string[] strArray = ConvertToStringArray(myvalues); DataGridViewRow row = new DataGridViewRow(); Object[] vals = strArray; row.CreateCells(dgOutput, vals); dgOutput.Rows.Add(row); } This displays the 1st 50 rows. I do not want to display 50 rows, I want to display all the rows in the spreadsheet. I need a variable that will replace the 50 (in the for loop) and represent the end-of-file. Please help. :doh:

    Mvelo Walaza Developer Telkom SA

    J 1 Reply Last reply
    0
    • W Walaza

      Hi, I developed an application that opens an excel doc, and reads/displays its contents to a datagrid. How do I determine its start and end rows? I have tried some code but it does not give me the result I need. e.g. I tried: //get start and end rows Object[] startRow = {worksheet.get_Range("A4", "O4")}; Object[] endRow = {worksheet.Rows.get_End(XlDirection.xlDown)}; which did not work for me. See below: // loop through all rows of the spreadsheet and place each row in the datagrid. for (int i = 4; i <= 50; i++) { Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "Z" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value2; string[] strArray = ConvertToStringArray(myvalues); DataGridViewRow row = new DataGridViewRow(); Object[] vals = strArray; row.CreateCells(dgOutput, vals); dgOutput.Rows.Add(row); } This displays the 1st 50 rows. I do not want to display 50 rows, I want to display all the rows in the spreadsheet. I need a variable that will replace the 50 (in the for loop) and represent the end-of-file. Please help. :doh:

      Mvelo Walaza Developer Telkom SA

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      int lastUsedRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;

      W 1 Reply Last reply
      0
      • J Jimmanuel

        int lastUsedRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;

        W Offline
        W Offline
        Walaza
        wrote on last edited by
        #3

        Thanks man, it worked!! Can you please recommend any sites I can get more info/code about Excel applications. I still have lots to do in this application like manipulating the data in the cells and inserting the data from the spreadsheet to an Oracle database table. Thanks again!

        Mvelo Walaza Developer Telkom SA

        J 1 Reply Last reply
        0
        • W Walaza

          Thanks man, it worked!! Can you please recommend any sites I can get more info/code about Excel applications. I still have lots to do in this application like manipulating the data in the cells and inserting the data from the spreadsheet to an Oracle database table. Thanks again!

          Mvelo Walaza Developer Telkom SA

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          Glad to help. The best I can do is give you some MSDN links, other than that good documentation isn't easy to find for the Excel stuff. After MSDN and Google I'd recommend using Intellisense to explore the Excel namespace and just try anything that looks useful (and of course posting here)! http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.excel(VS.80).aspx[^] http://support.microsoft.com/kb/302084[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups