C# - SPLessons

Loading and reading Microsoft Excel fil...

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Loading and reading Microsoft Excel file content using C#

 Loading and reading Microsoft Excel file content using C#

 

Description :

Hi every body, today i would like to demonstrate how to loading and reading Microsoft excel file content using C# console application.

 

Step1 :

Please open visual studio, right now i have visual studio professional 2012 i just opened my visual studio and click on new project.

 

 

Step2 :
 In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

 

 

Step3 :
 If you see the visual studio will gives you a predefined template program  for us.

 

Step4 :
 Here we are going to add Microsoft office Excel package reference into our solution, why because in order to read Microsoft excel document we have add the references of Microsoft office excel package into our solution. So please Right click on References under the our solution and then select add references.

 

Step5 :
 Now we will get a new window called as reference manager. In the reference manager window on the right corner we will have a search box, please type Microsoft.Office.Interop.Excel and please select Microsoft.Office.Interpro.Excel and click on ok button.

 

Step6 :
 Please add the below code in the main section of our program if you see we are getting an error message under the Excel.Application this is because we have imported the Microsoft office excel package into our solution package but we have not imported the Microsoft office excel package into our actual program. In the next step we are going to see how to import Microsoft office excel package into our program.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadSholarshipExcelFile
{
class Program
{
static void Main(string[] args)
{
//Read the excel document using Microsoft Office package
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
int sprowCnt = 0; // row count
int spcolumnCnt = 0; // column count

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(@"<strong>C:\splessons.xlsx</strong>", 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;

for (sprowCnt = 1; sprowCnt <= range.Rows.Count; sprowCnt++)
{
for (spcolumnCnt = 1; spcolumnCnt <= range.Columns.Count; spcolumnCnt++)
{
Console.WriteLine(" Coulmn Number: " + spcolumnCnt + "--> " + (range.Cells[sprowCnt, spcolumnCnt] as Excel.Range).Value2);
}
}

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

Console.ReadKey();

} // end of the function

&nbsp;

&nbsp;

} // end of the class
} // end of the namespace

&nbsp;

Step7 :

with the help of using statement we can import the microsoft office excel packaging into our application.

 

Step8 :

Please place your excel file under the bin debug directory.

 

 

Output :