C# Excel Tutorial - #1 - Open and Read Excel Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to the Excel C short tutorial so to start off create a new project and create a custom class axle now first we'll need to add library so go right click on references add reference and find Microsoft Excel object library and click OK now that that's done we're going to have to need add namespaces for it so using Microsoft put office dot Interop dot Excel and we'll create an object of it as well just for future so using Excel equals Microsoft Office interrupt excellent now that that's done let's see what we'll need we'll need the path to the file we'll need to open that file and to look at a specific sheet in it so let's create a constructor public Excel and we'll need path and sheet now before we continue let's just make global references to these so string path we really don't need a string for a sheet although we might just wanna make it an empty string don't leave it at now we'll need the reference to the application application Excel equals new Excel that's the object here dot application and we'll need a workbook basically a file we're opening and it's worksheet so workbook WB and worksheet WS and let's just add that path so this dot path equals path and then Excel equals new cell application actually we already done that then we then we just add workbook and worksheet so workbook equals Excel workbooks dot open and we pass in the path and for a worksheet worksheet is workbook but worksheets and then add the sheet number and that's it now we can open up an excel file let's now try and read it so public will make this string just so we can return the text value read cell and we'll need which cell we read so it's row and it's column let's mark them just into I n int J now since Excel actually reads from one and we usually iterate from 0 when we're programming let's just add this scene axel this first cell is not 0 0 its 1 1 0 0 doesn't exist so just for easier iterations let's just increase them N 1 now to read it first what we need to do is actually check if there's anything there if the values are not now so if worksheet dot cells and we pass in which so that value to no command is not now value 2 is basically the value stored inside the cell will return worksheet cells this hope dot value 2 now actually we don't introduce else we just returned blank and there we go now I created a test file just so you can see what's going on so in the main form I already edited just a little function open up a file it will create a new Excel instance passing the location and the worksheet number and display it in a little message box so let's see what happens and there we go that's the message let's try something else save and run and there we go and that would be it for today bye bye
Info
Channel: Bospear Programming
Views: 254,134
Rating: 4.7403975 out of 5
Keywords: C#, Excel, C# Excel Tutorial - #1 - Open and Read Excel Files, How to, Tutorial, Open Excel, Read Excel
Id: lsv7rAsvYuA
Channel Id: undefined
Length: 7min 3sec (423 seconds)
Published: Sat Apr 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.