简体   繁体   中英

How to interact with Excel files using C# ASP.NET

I hope someone can point me in the right direction. I'm planning to build a web application using Webforms in ASP.NET. In the application, I'll be interacting with Excel files stored in a shared folder to compute the desired output and show the output in the front end. So far, I can accomplish the desired output using the VLOOKUP functions manually, but I have no idea on how to accomplish the same thing using C#.

Am I looking at this in the wrong perspective, if so, are there any better ways to accomplish the same result?

This can be done using OleDB, an API made by Microsoft to access data from various datasources. This method saves you the pain of manually accessing an Excel file and parsing the data. Using the OleDb connection will also give you metadata information about the excel file, its tables and columns etc.

There are plenty of tutorials on this out there, here's one of them: OleDb Excel Tutorial . This will teach you everything you need to achieve your goal.

The basic steps to get data through an OleDbConnection are:

  1. Create an OleDbConnection using a connectionstring
  2. Open the connection
  3. Execute a query on the connection
  4. Access the result from a DataTable object

Connectionstrings.com shows you how to form connection strings for many different datasources, including Excel through OleDb.

You need to download and install the OleDb Access driver from Microsoft, which you can get at their website https://www.microsoft.com/en-us/download/details.aspx?id=54920 (it's called "Access driver", but it enables data access for most Microsoft Office system files)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM