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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nitesh TarbaniPosted Jul 11, 2010, 5:02 AM
Sam HobbsPosted Jul 11, 2010, 4:42 AM
Nitesh TarbaniPosted Jul 11, 2010, 4:30 AM
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 HobbsPosted Jul 11, 2010, 12:55 AM
Do you still neeed help?
Nitesh TarbaniPosted Jul 10, 2010, 5:12 AM
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.
HoneyPosted Jul 9, 2010, 9:37 AM
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]));
}