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

  • Home
  • SEARCH
  • 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 7417583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:43:42+00:00 2026-05-29T07:43:42+00:00

I have got below code sample with me where I am able to read

  • 0

I have got below code sample with me where I am able to read my excel file:

private bool excelvalidate(string requestType, string filename)
{
    Excel.Application ExcelObj = null;
    ExcelObj = new Excel.Application();
    Excel.Range range = null;
    Excel.Workbook theWorkbook = null;
    Excel.Sheets sheets = null;
    Excel.Worksheet worksheet = null;

    bool strValResult = false;
    string[] strArray = null;
    if (ExcelObj != null)
    {
        theWorkbook = ExcelObj.Workbooks.Open("D:/Support/" + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        sheets = theWorkbook.Worksheets;
        worksheet = (Excel.Worksheet)sheets.get_Item(1);
        for (int x = 1; x <= 1; x++)
        {
            range = worksheet.get_Range("A" + x.ToString(), "G" + x.ToString());
            System.Array myvalues = (System.Array)range.Cells.get_Value(null);
            strArray = ConvertToStringArray(myvalues);
        }
        if (requestType == "CreateCityKeys" || requestType == "CreateCityShortKeys")
        {
            strValResult = CheckStringInArray("City Name", strArray);
            if (strValResult == true)
            {
               //Here I want to call a function which will return all the column values in  City Name something like below
               string [] columnArray = ReadAllValuesFromColumn("City Name");
            }
        }
        else if (requestType == "CreateAirportKeys")
        {
            strValResult = CheckStringInArray("Full Airport Name", strArray);
        }
        else if (requestType == "CreateAirportShortKeys")
        {
            strValResult = CheckStringInArray("Airport Short Name", strArray);
        }
        ExcelObj.Workbooks.Close();
        ExcelObj.Quit();
        ExcelObj = null;
        theWorkbook = null;
        sheets = null;
        range = null;
        Marshal.ReleaseComObject(theWorkbook);
    }
    return strValResult;
}
private bool CheckStringInArray(string chkstr, string[] strArr)
{
    bool exists = false;
    foreach (string x in strArr)
    {
        if (x.Contains(chkstr))
        {
            exists = true;
        }
    }
    return exists;
}

private string[] ConvertToStringArray(System.Array values)
{
    // create a new string array
    string[] theArray = new string[values.Length];

    // loop through the 2-D System.Array and populate the 1-D String Array
    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;
}  

In above code you can see that, I am trying to call a function as below:

string [] columnArray = ReadAllValuesFromColumn("City Name");

Please suggest logic to write the above function to read all the column values if I pass Column Header name as “City Name”

Please suggest with some code snippets.

Thanks

  • 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-29T07:43:43+00:00Added an answer on May 29, 2026 at 7:43 am

    Below is the solution which I wrote for my above problem, please suggest for any changes.

     private string ReadAllValuesFromColumn(string fieldname, string filename)
        {
            Excel.Application ExcelObj = null;
            ExcelObj = new Excel.Application();
            Excel.Range cxRange = null;
            Excel.Range range = null; 
            Excel.Workbook theWorkbook = null;
            Excel.Sheets sheets = null;
            Excel.Worksheet worksheet = null;
            string[] strArray = null;
            StringBuilder sbReturn = new StringBuilder();
            Dictionary<string, ArrayList> dicArray = new Dictionary<string, ArrayList>();
            ArrayList fullArray = new ArrayList();
            try
            {
                if (ExcelObj != null)
                {
                    theWorkbook = ExcelObj.Workbooks.Open("D:/Support/" + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    sheets = theWorkbook.Worksheets;
                    worksheet = (Excel.Worksheet)sheets.get_Item(1);
                    cxRange = worksheet.UsedRange;
                    string str;
                    long lRowCount = cxRange.Rows.Count;
                    long lColumnCount = cxRange.Columns.Count;
                    for (int x = 1; x <= 1; x++)
                    {
                        range = worksheet.get_Range("A" + x.ToString(), "G" + x.ToString());
                        System.Array myvalues = (System.Array)range.Cells.get_Value(null);
                        strArray = ConvertToStringArray(myvalues);
                    }
                    int fiedIndex = Array.IndexOf(strArray, fieldname);
    
                    if (fiedIndex != -1)
                    {
                        for (int y = 2; y <= lRowCount; y++)
                        {
                            str = (string)(cxRange.Cells[y,fiedIndex+1] as Excel.Range).Value2;
                            if (!string.IsNullOrEmpty(str))
                            {
                                fullArray.Add(str);
                            }
                        }
                    }
                    ExcelObj.Workbooks.Close();
                    ExcelObj.Quit();
                    ExcelObj = null;
                    theWorkbook = null;
                    sheets = null;
                    range = null;
                    cxRange=null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return sbReturn.ToString();
        }
    

    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following code from here to read an Excel file using
I am using JQuery and I have got below JQuery Code sample. JQuery Code:
So far i have got the code below which works lovely when trying an
I am using XSLT 1.0 and have below sample code: In my XSLT, I
So I've got a pretty basic ListView, that has two columns. Sample code below:
I have got some code to load an assembly and get all types, which
Below I have the code that allows me to edit a table row inline.
i have this sample code i want to use. I'm not sure where to
I have a simple code below: class B; class A{ B b; }; class
i've built this sample code based on a real problem I have in an

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.