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



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Reading Excel Sheet Names in C#


Posted Date: 23 Jun 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: Pradeep Kintali       Member Level: Gold
Rating:     Points: 7



The code sample defines a function GetExcelSheetNames that takes an Excel file's path as input, opens it, and find out the sheet names in that file.

The function uses a OleDbConnection to connect to the data source (the excel file). The file is is referred by a DataTable and each row in this DataTable is a sheet in Excel file. And thus, the row names are obtained easily.


public string[] GetExcelSheetNames(string excelFileName)
{
OleDbConnection con = null;
DataTable dt = null;
String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + excelFileName + ";Extended Properties=Excel 8.0;";
con= new OleDbConnection(conStr);
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

if (dt == null)
{
return null;
}

String[] excelSheetNames = new String[dt.Rows.Count];
int i = 0;

foreach (DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}

return excelSheetNames;
}




Responses

Author: Amit Kumar Verma    14 Jul 2008Member Level: Gold   Points : 0
Read this one http://www.technodevelopers.com/ArticleDetails26.aspx


Author: RamyaNaidu    06 Aug 2008Member Level: Silver   Points : 2
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;

try
{
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
MessageBox.Show(objSheet.Name.ToString());
//here u can dispaly the sheet name
//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(20, 20);

if (this.FillWithStrings.Checked == false)
{
//Create an array.
double[,] saRet = new double[20,20];

//Fill the array.
for (long iRow = 0; iRow < 20; iRow++)
{
for (long iCol = 0; iCol < 20; iCol++)
{
//Put a counter in the cell.
saRet[iRow, iCol] = iRow * iCol;
}
}

//Set the range value to the array.
range.set_Value(Missing.Value, saRet);
}

else
{
//Create an array.
string[,] saRet = new string[20, 20];

//Fill the array.
for (long iRow = 0; iRow < 20; iRow++)
{
for (long iCol = 0; iCol < 20; iCol++)
{
//Put the row and column address in the cell.
saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
}
}

//Set the range value to the array.
range.set_Value(Missing.Value, saRet);
}

//Return control of Excel to the user.
objApp.Visible = true;
objApp.UserControl = true;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);

MessageBox.Show(errorMessage, "Error");
}
}


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Read Excel Sheet Names in C#  .  Read Excel Files in C#  .  Obtain Excel Sheet Names in C#  .  

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: Send SMS to a Mobile Phone Using C#
Previous Resource: Random helper class
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use