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 6332417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:15:36+00:00 2026-05-24T18:15:36+00:00

i need to read data from some excel sheets. The data in the excel

  • 0

i need to read data from some excel sheets. The data in the excel sheets is already formatted in such a way that i am able to get the desired data by using this method.

This is what i am doing:

using (var conn = new OleDbConnection(strBld.ToString()))
{
    conn.Open();

    IEnumerable<IDictionary<string, object>> excelDataRaw = 
        conn.Query("select * from [Curves$A:IT]").
        Cast<IDictionary<string, object>>();

    int i = 0;
    string previousKey = null;
    var curve = new List<IEnumerable<object>>();
    var excelData = new Dictionary<string, IDictionary<object, object>>();
    //var excelData = new Dictionary<string, IDictionary<string, decimal>>();

    foreach (var key in excelDataRaw.Select(dictionary => dictionary.Keys).
        ElementAt(i))
    {
        string key1 = key;
        // gets the data from one column within the excel file
        curve.Add(excelDataRaw.Select(col => col[key1]).
            Where(row => row != null).ToList());

        if (i % 2 == 0)
        {
            // store the column header
            previousKey = key;
        }

        if (i % 2 == 1)
        {
            // merge the data from the first column (keys)
            // with the data from the second column (values)
            IEnumerable<object> keys = curve[i - 1];
            IEnumerable<object> values = curve[i];
            // cast works but than you can't zip the lists together
            //IEnumerable<string> keys = curve[i - 1].Cast<string>();
            //IEnumerable<decimal> values = curve[i].Cast<decimal>();

            // zip them together on their index
            var dic = keys.Zip(values, (k, v) => new { k, v }).
                ToDictionary(x => x.k, x => x.v);

            if (previousKey != null)
            {
                if (!excelData.ContainsKey(previousKey))
                {
                    excelData.Add(previousKey, dic);
                }
            }
        }

        ++i;
    }
}

I extract all data from the excel file (excelDataRaw). i then select all data that belongs together into a list (curve) and combine two lists that belong to each other into a dictionary (dic). The final result is a dictionary (excelData) that contains the column head from the excel file as a key (previousKey) and the data relevant to this column head as a dictionary (dic).

I would like to cast the dictionary (excelData) from

Dictionary<string, IDictionary<object, object>> 

into

Dictionary<string, IDictionary<string, decimal>> 

but i can’t cast object into string or decimal and i can’t zip the lists together to get the dictionary (dic) after casting each list to the desired type. Has anyone an idea how to achieve the desired result(type)?

  • 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-24T18:15:37+00:00Added an answer on May 24, 2026 at 6:15 pm

    Well i found a solution, I must have overlooked the obvious yesterday. A good night of sleep helps sometimes :).

    IEnumerable<object> keys = curve[i - 1];
    IEnumerable<object> values =
        curve[i].Where(content => decimal.TryParse(content.ToString(), out num));
        Dictionary<string, decimal> dic =
            keys.Zip(values, (k, v) => new { k, v }).ToDictionary(
                x => (string)x.k, x => decimal.Parse(x.v.ToString()));
    if (previousKey != null)
    {
        if (!excelData.ContainsKey(previousKey))
        {
            excelData.Add(previousKey, dic);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a script that extracts some data from a complex Excel
I need to read some data from an input file and plot a graph
I need to read the data from a file that can be either comma
I need to read in data from excel files in my c# winforms app.
I have a .NET C# windows app that I need to read some excel
I need to import some data from a excel file and a folder with
I need to read some data from database my controller is class Pro_ajax extends
Can I read a XML file from remote server such as http://dealer.somedomain.com/data/inventory.xml I need
Hi there I'm use to SQL, but I need to read data from a
I need to read data added to the end of an executable from within

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.