Hi guys
Can anyone help me with this one?
I want to insert a loop in my code to read all the data in excel file here's a portion of my code.
private void btnUpdate_Click(object sender, EventArgs e)
{
string directory = "D:/myFile.xls";
if (File.Exists(directory))
{
Excel._Application myPartFile = new Excel.ApplicationClass();
string workbookPath = directory;
try
{
Excel.Workbook excelWorkbook = myPartFile.Workbooks.Open(workbookPath, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows,
"\t", false, false, 0, false, true, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Excel.Worksheet excelWorkSheet = (Excel.Worksheet)excelSheets.get_Item(1);
Excel.Range excelPartNumber = (Excel.Range)excelWorkSheet.get_Range("A1", "A1");
textBox1.Text = String.Format("{0:N0}", excelPartNumber.Value2);
Excel.Range excelPartName = (Excel.Range)excelWorkSheet.get_Range("B1", "B1");
textBox2.Text = String.Format("{0:N0}", excelPartName.Value2);
Excel.Range excelLocCode = (Excel.Range)excelWorkSheet.get_Range("C1", "C1");
textBox3.Text = String.Format("{0:N0}", excelLocCode.Value2);
Marshal.ReleaseComObject(excelSheets);
excelWorkbook.Close(false, "", false);
Marshal.ReleaseComObject(excelWorkbook);
myPartFile.Quit();
Marshal.ReleaseComObject(myPartFile);
GC.Collect();
}
catch (COMException)
{
MessageBox.Show("Cannot open the file exclusively.Other program maybe using it.\r\nClose the file/program and try again.", "File Opening Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
I need to increment the A1, B1, C1 until i reach the last rows with value.
Hope to received any reply with this.
Thanks in advance ;)