Hello well I have this problem, I want to export my datagrid in a existing excel sheet but I do not how I can conect with my sheet Any help is appreciated.
`try { if (dgvLog.DataSource != null) { SaveFileDialog fichero = new SaveFileDialog(); fichero.Filter = "Excel (.xls)|.xls"; if (fichero.ShowDialog() == DialogResult.OK) { Microsoft.Office.Interop.Excel.Application aplicacion; Microsoft.Office.Interop.Excel.Workbook libros_trabajo; Microsoft.Office.Interop.Excel.Worksheet hoja_trabajo; aplicacion = new Microsoft.Office.Interop.Excel.Application(); libros_trabajo = aplicacion.Workbooks.Add(); hoja_trabajo = (Microsoft.Office.Interop.Excel.Worksheet)libros_trabajo.Worksheets.get_Item(1);
//exportar cabeceras dgvLog
for (int i = 1; i <= this.dgvLog.Columns.Count; i++)
{
hoja_trabajo.Cells[1, i] = this.dgvLog.Columns[i - 1].HeaderText;
}
//Recorremos el DataGridView rellenando la hoja de trabajo con los datos
for (int i = 0; i < this.dgvLog.Rows.Count - 1; i++)
{
for (int j = 0; j < this.dgvLog.Columns.Count; j++)
{
hoja_trabajo.Cells[i + 2, j + 1] = this.dgvLog.Rows[i].Cells[j].Value.ToString();
}
}
libros_trabajo.SaveAs(fichero.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
libros_trabajo.Close(true);
aplicacion.Quit();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
`
