C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Retriving Values from an Excel Sheet


Posted Date: 06 Oct 2008    Resource Type: Code Snippets    Category: File Operations

Posted By: ravi       Member Level: Gold
Rating:     Points: 10



This code shows how to retrive values from an Excel file

Namesspaces to import :
using System;
using System.Data;
using Excel;

References to ADD:
Microsoft.Office.Core


static void Main(string[] args)
{
//Declarations
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;

try
{
//Start Excel and get Application object.
oXL = new Excel.Application();

//Get a new workbook.
int index = 0;
object rowIndex = 2;
object colIndex1 = 1;
object colIndex2 = 2;

oWB = (Excel._Workbook)(oXL.Workbooks.Add("c:/Test.xls")); // Gets the Workbook
oSheet = (Excel._Worksheet)oWB.Sheets[1]; // Gets the first Sheet of the workbook

while ( ((Excel.Range)oSheet.Cells[rowIndex,colIndex1]).Value2 != null )
{
rowIndex = 2+index;
string firstName = ((Excel.Range)oSheet.Cells[rowIndex,colIndex1]).Value2.ToString(); // retrives the firstColumn Value
string lastName = ((Excel.Range)oSheet.Cells[rowIndex,colIndex2]).Value2.ToString(); // retrives the secondColumn Value
Console.WriteLine("Name : {0},{1} ",firstName,lastName);
index++;
}
}
catch(Exception ex)
{

ex.Message.ToString();
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Retriving values from an Excel sheet  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Uploading Multiple files using ASP.NET
Previous Resource: Moving file from sourcePath to DestinationPath
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

it outsourcing

Contact Us    Privacy Policy    Terms Of Use