Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to store the excel sheet values in a string (ex mobile numbers)
I have windows application which has the mobile number field .and an excel sheet which contains mobile numbers in a column. I have to fetch those mobile numbers from excel sheet and store them in a string.
Posted
Updated 11-Jul-10 10:33am
v2

Use Excel Interop to access the worksheet and cells necessary. There are a multitude of resources on the subject available here and elsewhere.
 
Share this answer
 
<pre>
private string strConnectionString, SelectString;
private OleDbCommand objCmdSelect;
private OleDbDataAdapter objAdapter;
private OleDbConnection objConn;
private DataSet dsUpload, ds;
bool strRet;
</pre>

<pre>
objConn = new OleDbConnection(strConnectionString);
objConn.Open();
DataTable SchemaTable = objConn.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
objConn.Close();
string ExcelFirstSheet = "";
string NotUploadedFiles = "";
String[] excelSheets = new String[SchemaTable.Rows.Count];
</pre>

<pre>
strRet = readExcelFile(SelectString);
</pre>

<pre>
private bool readExcelFile(string SelectString)
{
ds = new DataSet();
ds.Clear();
int idx = strFileUpldName.LastIndexOf(".");
if (idx != -1)
{
fileExtension = strFileUpldName.Substring(idx + 1);
}
if (fileExtension == "xls")
{
try
{
objConn = new OleDbConnection(strConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand (SelectString,objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(ds);
Session["dsCRM"] = ds;
objConn.Close();
objConn.Dispose();
return true;
}
catch (Exception e)
{
if (objConn != null)
{
if (objConn.State == ConnectionState.Open)
{
objConn.Close();
}
}
objConn = null;
return false;
}
}
else
{
return false;
}
}
</pre>
 
Share this answer
 
Here's some sample code, is VB but is easily convertible to C#:

You need to add a reference to Microsoft.Office.Interop.Excel.

<code>Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim vValue As Object

oXL = New Microsoft.Office.Interop.Excel.Application
oBook = oXL.Workbooks.Open(OpenFileDialog1.FileName)
oSheet = oBook.Worksheets("Sheet1")
'Here you have cell value, just build a loop to get the data you want
'This will loop through all rows in the first column

For i As Integer = 0 To oSheet.Rows.Count
vValue = vValue + ";" + oSheet.Cells(1, i).Value 
Next


myTextBox.Text = vValue</code>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900