Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# read multiple Excel files [closed]

Tags:

c#

excel

Is it possible to make an application that reads multiple excel files from a folder and extracts some information from them?

like image 390
Silvia Stoyanova Avatar asked Oct 05 '12 11:10

Silvia Stoyanova


1 Answers

Yes it is, and here is how using Interop. The first thing you need to do is add the Excel Interop library to your project. You can do this by creating a new Visual Studio solution, right clicking on References, selecting Add Reference and then selecting Microsoft.Office.Interop.Excel from the .NET tab.

Then you need to add a using statement for Excel, and one for InteropServices (as we are interoping with a COM object):

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

Then, inside a method, you need to create an Application object:

Excel.Application application = new Excel.Application();

Next, create a Workbook object for each workbook you want to read from, like so:

Excel.Workbook workbookOne;
Excel.Workbook workbookTwo;
Excel.Workbook workbookThree;

Now use the Application object to open each workbook, and load each one into its respective Workbook object:

workbookOne = application.Workbooks.Open(@"C:\workbookOneLocation.xlsx");
workbookTwo = application.Workbooks.Open(@"C:\workbookTwoLocation.xlsx");
workbookThree = application.Workbooks.Open(@"C:\workbookThreeLocation.xlsx");

Now you need to decide what information you want to extract. Once you have done this determine which worksheet in the workbook it is on,and then figure out the number by simply looking at the tabs and counting. In the below example, Sheet2 is number 1, Sheet1 is number 2 and Sheet3 is number 3:

Example

Create a variable for each piece of information you need like so (any value type variables will need to be nullable):

string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;

Let's say that the first piece of information we need is a string in Cell A1 on sheet one inside workbook one, the second piece is Cells B2 - B4 on worksheet two inside workbook two, and the third piece is a number on cell C5 on worksheet three inside workbook three. We would do this:

string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;

Excel.Worksheet worksheet;
Excel.Range range;

worksheet = workbookOne.Sheets[1];
range = worksheet.Cells["1", "1"];

firstPieceOfInformationINeed = range.Value as string;

worksheet = workbookTwo.Sheets[2];
range = worksheet.Range["B2", "B4"];

secondPieceOfInformationINeed = range.Value as string[];

worksheet = workbookThree.Sheets[3];
range = worksheet.Cells["3", "5"];

thirdPieceOfInformationINeed = range.Value as double?;

Then we close the workbook, using a boolean value to indicate whether or not we want to save changes:

workbookOne.Close(true);
workbookTwo.Close(false);
workbookThree.Close(true);

Now quit the application:

application.Quit();

And release the COM object:

Marshal.ReleaseComObject(application);

Now you are done with Excel, and have all of the different pieces of information you need stored as C# variables, and you can do with these what you wish.

like image 167
JMK Avatar answered Sep 24 '22 15:09

JMK