Hi friends,
I want to add item in combo box from excel file and Items are column name. So if Anybody knows please help me out.
Thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Honey22 reputationPosted Jul 9, 2010, 5:37 AM EDT
try this code... hope it will help you....
//connection string for 2003 excel sheet
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
//connection string for 2007 excel sheet
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\test1.xlsx;Extended Properties='Excel 12.0;HDR=YES';");
con.Open();
//Create Dataset and fill with imformation from the Excel Spreadsheet for easier reference
DataSet myDataSet = new DataSet();
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con);
myCommand.Fill(myDataSet);
con.Close();
//Travers through each row in the dataset
foreach (DataRow myDataRow in myDataSet.Tables[0].Rows)
{
Object[] cells = myDataRow.ItemArray;
comboBox1.Items.Add(Convert.ToString(cells[1]));
}
Nitesh Tarbani13 reputationPosted Jul 10, 2010, 1:12 AM EDT
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(fp, 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);
where fp is string variable. if I provide directly path not using variable then its working.
If someone knows answer to this question then help me out.
Sam HobbsRank #108 · 20.5K reputationPosted Jul 10, 2010, 8:55 PM EDT
Do you still neeed help?
Nitesh Tarbani13 reputationPosted Jul 11, 2010, 12:30 AM EDT
public void opt(string textranger, string textrangec, string dataranger, string datarangec,string filepath)
{
string tr = textranger;
string tc = textrangec;
string dr = dataranger;
string dc = datarangec;
string fp = filepath;
textBox1.Text = fp;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str;
int rCnt = 0;
int cCnt = 0;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(fp, 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;
rCnt =Convert.ToInt16(tr);
for (cCnt = Convert.ToInt16(tc); cCnt < Convert.ToInt16(dc); cCnt++)
{
str=(string)(range.Cells[2, 1] as Excel.Range).Value2;
textBox1.Text = str;
}
}
And also wanna know how to convert string into integer. I mean if textbox contain row no of excel sheet and I have to use integer to contain that value then how to load that value from textbox to integer. As text in textbox will be in text format. I have tried "convert.ToInt16/32()" but i think its not working.
Thanks.
Sam HobbsRank #108 · 20.5K reputationPosted Jul 11, 2010, 12:42 AM EDT
Nitesh Tarbani13 reputationPosted Jul 11, 2010, 1:02 AM EDT