Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7616943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:00:56+00:00 2026-05-31T03:00:56+00:00

I’m having some troubles while reading from an exel in c#. I have this

  • 0

I’m having some troubles while reading from an exel in c#.
I have this code which i read every cell from A to X.

            int i = 1;
            int number;
            System.Array myvalues; string[] strArray;
            Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString());
            while(range.Count!=0)
            {
                    i++;
                    range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString());
                    myvalues = (System.Array)range.Cells.Value;
                    strArray = ConvertToStringArray(myvalues);
                    number = Convert.ToInt32(strArray[0]);
            }

My question is: How could i read next 4 * “number” rows in excel based on “number” value ?

For example:

         A B C D E F G H I J
         a a a a a 1 a a a a 

F’s cell value is 1 so i would like to read ( G H I J)
If F’s cell value is 2 the i would like to read ( G H I J K L M N)

         A B C D E F G H I J K L M N
         a a a a a 2 a a a a a a a a

F’s cell value 3 :

        A B C D E F G H I J K L M N O P Q R
        a a a a a 3 a a a a a a a a a a a a
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:00:57+00:00Added an answer on May 31, 2026 at 3:00 am

    code:

    int i = 1;
    do
    {
    
       i++;
       var range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString());
       if (range.Count != 0) 
       {
          var myvalues = (System.Array)range.Cells.Value;
          var strArray = ConvertToStringArray(myvalues);
          var number = Convert.ToInt32(strArray[0]);
          MyMethodReadRage(worksheet, number);
       }
    
    } while(range.Count!=0);
    

    method for read range:

    void MyMethodReadRage(Microsoft.Office.Interop.Excel.Worksheet worksheet, int numberRows)
    {
       var range = worksheet.get_Range("A" + numberRows, "X" + (numberRows + 4));
       /*code*/
    }
    

    EDIT

    Reads the value of the column “F”, multiplicas the value by 4 and add the amount of previous columns (up to “F”). You then use this (this unproven) function to get the letter.

        /// <summary>
        /// http://www.freevbcode.com/ShowCode.asp?ID=4303
        /// </summary>
        private string ColumnLetter(int ColumnNumber)
        {
            if (ColumnNumber > 26)
            {
                return string.Format("{0}{1}", (char)(Convert.ToInt32((ColumnNumber - 1) / 26) + 64), (char)(((ColumnNumber - 1) % 26) + 65));
            }
            else
            {
                return string.Format("{0}", (char)(ColumnNumber + 64));
            }
        }
    

    EDIT II

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplicationExcelCeldas
    {
        class Program
        { 
            private const int NumberPositionColumnF = 6;
    
            static void Main(string[] args)
            {
                Microsoft.Office.Interop.Excel.Worksheet worksheet = new Microsoft.Office.Interop.Excel.Worksheet();
    
                var i = 0;
                var test = new TestExcel();
                do
                {
                    i++;
                    var rangeF = worksheet.get_Range(string.Format("F{0}" , i));
                    if (rangeF.Count == 0)
                    {
                        break;
                    }
    
                    var values = test.GetCellsStringArray(rangeF);
                    if (values.Count() > 0) 
                    {
                        int number = 0;
                        if (int.TryParse(values[0], out number))
                        {
                            var rangeAll = worksheet.get_Range(
                                string.Format("A{0}" , i), 
                                string.Format("{0}{1}", test.ColumnLetter(Program.NumberPositionColumnF + (number * 4)), i));
    
    
                            /*
                             your code for work with rangeAll
                            */
                        }
                    }
    
                } while (true);
            }
        }
    
        class TestExcel
        {
            internal string[] GetCellsStringArray(Microsoft.Office.Interop.Excel.Range range)
            {
                var myvalues = (System.Array)range.Cells.Value;
                return this.ConvertToStringArray(myvalues);
            }
    
            internal string[] ConvertToStringArray(System.Array values)
            {
                string[] theArray = new string[values.Length];
                for (int i = 1; i <= values.Length; i++)
                {
                    if (values.GetValue(1, i) == null)
                        theArray[i - 1] = "";
                    else
                        theArray[i - 1] = (string)values.GetValue(1, i).ToString();
                }
                return theArray;
            }
    
            internal string ColumnLetter(int columnNumber)
            {
                if (columnNumber > 26)
                {
                    return string.Format("{0}{1}", (char)(Convert.ToInt32((columnNumber - 1) / 26) + 64), (char)(((columnNumber - 1) % 26) + 65));
                }
                else
                {
                    return string.Format("{0}", (char)(columnNumber + 64));
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.