M
Mendel Lim
Guest
Hi all,
i would like to seek help if my code works or not. Currently i have a button for navigation of files.
private void Button2_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Excel File to Edit";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Excel File|*.xlsx;*.xls";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
sFileName = openFileDialog1.FileName;
if (sFileName.Trim() != "")
{
readExcel(sFileName); // READ EXCEL NAME.
}
}
}//choose & read button
private void readExcel(string sFile)
{
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(sFile);
xlWorkSheet = xlWorkBook.Worksheets["Sheet1"];
int iCol;
for (iCol = 1; iCol <= xlWorkSheet.Columns.Count; iCol++)
{
if (xlWorkSheet.Cells[1, iCol].value == null)
{
break; // BREAK LOOP.
}
else
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = xlWorkSheet.Cells[1, iCol].value;
int colIndex = dataGridView1.Columns.Add(col); // ADD A NEW COLUMN.
}
}
for (iRow = 2; iRow <= xlWorkSheet.Rows.Count; iRow++) // START FROM THE SECOND ROW.
{
if (xlWorkSheet.Cells[iRow, 1].value == null)
{
break;
}
else
{
string[] row = new string[] { xlWorkSheet.Cells[iRow, 1].value.ToString(),
xlWorkSheet.Cells[iRow, 2].value.ToString(),
xlWorkSheet.Cells[iRow, 3].value.ToString() };
dataGridView1.Rows.Add(row);
}
}
xlWorkBook.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
}
Thank you
Continue reading...
i would like to seek help if my code works or not. Currently i have a button for navigation of files.
private void Button2_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Excel File to Edit";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Excel File|*.xlsx;*.xls";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
sFileName = openFileDialog1.FileName;
if (sFileName.Trim() != "")
{
readExcel(sFileName); // READ EXCEL NAME.
}
}
}//choose & read button
private void readExcel(string sFile)
{
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(sFile);
xlWorkSheet = xlWorkBook.Worksheets["Sheet1"];
int iCol;
for (iCol = 1; iCol <= xlWorkSheet.Columns.Count; iCol++)
{
if (xlWorkSheet.Cells[1, iCol].value == null)
{
break; // BREAK LOOP.
}
else
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = xlWorkSheet.Cells[1, iCol].value;
int colIndex = dataGridView1.Columns.Add(col); // ADD A NEW COLUMN.
}
}
for (iRow = 2; iRow <= xlWorkSheet.Rows.Count; iRow++) // START FROM THE SECOND ROW.
{
if (xlWorkSheet.Cells[iRow, 1].value == null)
{
break;
}
else
{
string[] row = new string[] { xlWorkSheet.Cells[iRow, 1].value.ToString(),
xlWorkSheet.Cells[iRow, 2].value.ToString(),
xlWorkSheet.Cells[iRow, 3].value.ToString() };
dataGridView1.Rows.Add(row);
}
}
xlWorkBook.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
}
Thank you
Continue reading...