9 Replies - 380 Views - Last Post: 18 June 2019 - 11:17 AM Rate Topic: **--- 3 Votes

#1 Jahanzaib_bokhari   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-June 19

Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 09:52 AM

I made an application C# that searches for a code entered by the user in an excel file and then it displays some description corresponding to the code.

The excel have file have 2 Columns. One is code one is description. e.g

code description
1 hello
2 this is
3 a test



I made an application C# that searches for a code entered by the user in an excel file and then it displays some description corresponding to the code.

The excel have file have 2 Columns. One is code one is description. e.g

code description
1 hello
2 this is
3 a test

So if User enters 3. It will return with "a test"

I am using Microsoft.Office.Interop.Excel;

However, it came to my attention that this will only work if you have Excel Installed on your computer. I need something that doesn't require you to have excel installed on your computer.

The code I have for my current program.

using Excel = Microsoft.Office.Interop.Excel;

namespace Excel_Finder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = "Searching For Code!";
            label3.Visible = true;
            Excel.Application xlApp = new Excel.Application();
            string path = textBox3.Text;
            Excel.Workbook workBook = xlApp.Workbooks.Open(path);
            Excel.Worksheet workSheet = workBook.Worksheets["Sheet1"];
            Excel.Range range = workSheet.Columns[1];//Range of Column A
            Excel.Range findRange;
            string strToFind = textBox1.Text;
            string description;
            findRange = range.Find(strToFind);
            if (findRange is null)
            {
                label3.Text = "Code " + textBox1.Text + " Does not Exist!";               
            }
            else
            {
                description = workSheet.Cells[findRange.Row, 2].VALUE;
                textBox2.Text = description;                             
            }
        }
    }
}


This code works as expected but as I said this does not work if I don't have excel installed. I found some other libraries that does not require excel to be installed but I am not sure how to use them to do this same thing. If someone can provide an example that would be great with any library.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Search For a code in Excel File without Excel Installed

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15215
  • View blog
  • Posts: 60,899
  • Joined: 12-June 08

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 09:54 AM

Do you have to use excel? Why not a plain .txt doc?
Was This Post Helpful? 0
  • +
  • -

#3 Jahanzaib_bokhari   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-June 19

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 09:57 AM

View Postmodi123_1, on 18 June 2019 - 09:54 AM, said:

Do you have to use excel? Why not a plain .txt doc?


Yes it has to be excel, its a requirement.
- Has to be excel and needs to work without excel installed.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15215
  • View blog
  • Posts: 60,899
  • Joined: 12-June 08

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:02 AM

Is this an XLS excel or CSV? If the latter then you can read that like a text file with StreamReader.

As it is.. it seems weird to be using an Excel file on a computer that doesn't have excel installed, right? I mean the user won't be able to open it anyways!

I would go look for excel and .net things on github.
Was This Post Helpful? 0
  • +
  • -

#5 Jahanzaib_bokhari   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-June 19

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:05 AM

View Postmodi123_1, on 18 June 2019 - 10:01 AM, said:

Is this an XLS excel or CSV? If the latter then you can read that like a text file with StreamReader.


It's an XLS

The XLS file will be provided by someone else to multiple users, so not everyone will have excel installed on their computer. As long as they will have the file, the software just needs to read data from it.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15215
  • View blog
  • Posts: 60,899
  • Joined: 12-June 08

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:07 AM

Upfront I will tell you that's a bad plan, but so it goes.

You cannot use 'Microsoft.Office.Interop' without a redistribute (at the minimum) installed. Your choices are to make that part of the installer, or find a third party .NET library to interact with Excel.
Was This Post Helpful? 0
  • +
  • -

#7 Dankwansere   User is offline

  • D.I.C Regular

Reputation: 83
  • View blog
  • Posts: 257
  • Joined: 09-November 09

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:10 AM

Use the OpenXML sdk that microsoft provided. You can find the link here

Microsoft actually prefers openxml to be used rather interop, as one of the benefits to it is it does not require office to be installed. Please note that openxml only works with the microsoft office post 2000. So extensions such as docx or xlsx etc will work. Old files such as .doc will not work.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 Jahanzaib_bokhari   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-June 19

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:11 AM

View Postmodi123_1, on 18 June 2019 - 10:07 AM, said:

Upfront I will tell you that's a bad plan, but so it goes.

You cannot use 'Microsoft.Office.Interop' without a redistribute (at the minimum) installed. Your choices are to make that part of the installer, or find a third party .NET library to interact with Excel.


I know I can't use Microsoft.Office.Interop if I don't have excel installed as I said earlier. That is why I am looking for an alternative. I also know that it is possible to do same thing with other libraries without having excel installed but I am not able to figure out how to use those other libraries to do this task.
Was This Post Helpful? 0
  • +
  • -

#9 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1416
  • View blog
  • Posts: 2,017
  • Joined: 24-August 11

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 10:40 AM

In addition to Dankwansere's suggestion to use OpenXML, you can also use an OLEDB provider to connect to XLS and XLSX. I believe you use the JET provider for XLS, and ACE for XLSX. There should be plenty of documentation and sample code out there for this. However, if you're using an Office format supported by OpenXML, I think that would be the better route to go.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7021
  • View blog
  • Posts: 23,845
  • Joined: 05-May 12

Re: Search For a code in Excel File without Excel Installed

Posted 18 June 2019 - 11:17 AM

You can't use OpenXML for .xls files since they don't use the OpenXML data file format. They use the older OLE Compound document format where the streams within compound document are in proprietary (albeit documented to satisfy the Justice Department consent decree) data format.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1