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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:12:53+00:00 2026-05-21T09:12:53+00:00

I have written some code based on a specification from my boss. A requirement

  • 0

I have written some code based on a specification from my boss. A requirement was to import a lot of Excel data into the program. I did this by copying the used range of the Excel sheet to the Clipboard and then used that to write to the array. This was done using Interop. This does work well, and it’s code thats been in use for around 4months without any problems.

However, I wrote this code, without thinking too much at the beginning of my internship. I am now re-visiting it, in a quiet moment, and it got me thinking, is there a better (more efficient, elegant) way of doing this? Looking back now, it seems like a bit of a hack.

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-21T09:12:53+00:00Added an answer on May 21, 2026 at 9:12 am

    Using a c# driver you can read the excel file directly without using interop:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    
    namespace Immo.Areas.Administration.Controllers
    {
        public class SomethingSometingExcelClass
        {
            public void DoSomethingWithExcel(string filePath)
            {
    
                List<DataTable> worksheets = ImportExcel(filePath);
                foreach(var item in worksheets){
                    foreach (DataRow row in  item.Rows)
                    {
                       //add to array
                    }
    
                }
            }
            /// <summary>
            /// Imports Data from Microsoft Excel File.
            /// </summary>
            /// <param name="FileName">Filename from which data need to import</param>
            /// <returns>List of DataTables, based on the number of sheets</returns>
            private List<DataTable> ImportExcel(string FileName)
            {
                List<DataTable> _dataTables = new List<DataTable>();
                string _ConnectionString = string.Empty;
                string _Extension = Path.GetExtension(FileName);
                //Checking for the extentions, if XLS connect using Jet OleDB
                if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
                {
                    _ConnectionString =
                        "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0";
                }
                //Use ACE OleDb
                else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
                {
                    _ConnectionString =
                        "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0";
                }
    
                DataTable dataTable = null;
    
                using (OleDbConnection oleDbConnection =
                    new OleDbConnection(string.Format(_ConnectionString, FileName)))
                {
                    oleDbConnection.Open();
                    //Getting the meta data information.
                    //This DataTable will return the details of Sheets in the Excel File.
                    DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
                    foreach (DataRow item in dbSchema.Rows)
                    {
                        //reading data from excel to Data Table
                        using (OleDbCommand oleDbCommand = new OleDbCommand())
                        {
                            oleDbCommand.Connection = oleDbConnection;
                            oleDbCommand.CommandText = string.Format("SELECT * FROM [{0}]",
                                item["TABLE_NAME"].ToString());
                            using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
                            {
                                oleDbDataAdapter.SelectCommand = oleDbCommand;
                                dataTable = new DataTable(item["TABLE_NAME"].ToString());
                                oleDbDataAdapter.Fill(dataTable);
                                _dataTables.Add(dataTable);
                            }
                        }
                    }
                }
                return _dataTables;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have written a code based upon a requirement. (field1_6) (field2_30) (field3_16) (field4_16)
Occasionally I'll have a situation where I've written some code and, based on its
I have written some code in my VB.NET application to send an HTML e-mail
I have some code I've written in PHP for consuming our simple webservice, which
I have inherited some legacy PHP code what was written back when it was
I'm just getting into unit testing, and have written some short tests to check
I have written a new custom component derived from TLabel. The component adds some
I have written some jQuery ajax code where I am sending a request to
I am running some code that I have written in C which calls the
I have just written some code, which as i was writing i thought, this

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.