5

I am aware that we can read excel .xls file using following way

  • OleDB : But we are not allowed to install OleDB driver on server.
  • Interop : But we are not allowed to install MS Office on server.
  • ExcelDataReader : 3rd party packages or DLL are not allowed to use.

We can use FileStream to read flat files, is not there any way to read .xls file using inbuilt class library in .NET Framework ?

7
  • Can you make those .xls files be compatible with .xlsx? That way you can use the System.Io.Package namespace Commented Mar 2, 2018 at 22:53
  • There is probably no way to read an Excel file, other than a third party library. You can maybe study the file structure and create your own library. In Java, we use POI. Commented Mar 2, 2018 at 22:53
  • 1
    I could suggest a preprocessing on that excel file, translating it to a text file or something easier on server side. BTW, open it in the notepad to check if it's only a renamed HTML file. Commented Mar 2, 2018 at 23:05
  • 1
    You can read an .xls file with filestream but the issue isn't reading the file it interpreting the content. That is something your not going to do successfully without a 3rd party library or Excel PIA (.xls file structure is way too complicated, .xlsx is hard but doable). I don't believe Microsoft supports using Excel PIA as a multi-user server solution only as a desktop single user, probably for both licensing and threading reasons. Commented Mar 2, 2018 at 23:36
  • 1
    Your best option is to use the ACE OleDB driver as it is the only option that can be used in server environments. "Windows Server 2003 R2 (32-Bit x86), Windows Server 2003 R2 x64 editions, Windows Server 2008 R2, Windows Server 2008 Service Pack 2, Windows Server 2012 R2". Remember you may not use Excel COM interop on a server Commented Mar 3, 2018 at 0:28

3 Answers 3

4

Here is a simple example to get you going.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel; 

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

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;

            string str;
            int rCnt ;
            int cCnt ;
            int rw = 0;
            int cl = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"d:\csharp-Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            rw = range.Rows.Count;
            cl = range.Columns.Count;


            for (rCnt = 1; rCnt  < = rw; rCnt++)
            {
                for (cCnt = 1; cCnt  < = cl; cCnt++)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    MessageBox.Show(str);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

        }

    }
}

http://csharp.net-informations.com/excel/csharp-read-excel.htm

Remember to add a reference to Microsoft Excel 15.0 Object Library

Sign up to request clarification or add additional context in comments.

Comments

1

The most fair way to read xls from c# is to use Microsoft Primary interop assemblies (PIA) for Excel (that's "Interop" from your options). However there is a big disadvantage that it needs to have Microsoft Excel installed on computer running this code. These libraries are some kind of wrappers above Excel application, but it should allow you to read everything that can be available from Excel object model.

From my experience this way is more or less good for desktop applications, but it is not good for server-side Excel file processing.

5 Comments

PIA is awful for servers because it actually runs a complete Excel program instance in the background. Awful memory hog.
OP has already made it quite clear "Interop : But we are not allowed to install MS Office on server". When OP says "not allowed...server" that's not from his employer's point of view but Microsoft's. That's the "biggest disadvantage"
I don't believe Microsoft disallows it - they simply don't recommend or support it.
@MickyD Microsoft Volume Licensing Terms are different.
0

If you can add the Microsoft Open XML 2 SDK to your application, you can read the modern Open XML file formats and then parse the XML files for the Excel data.

If you can't use Microsoft's solution, you would have to write your own, using the Open XML file format as your guide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.