Está en la página 1de 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

public void ExportarDataGridViewExcel(DataGridView grd)


{
try
{
SaveFileDialog archivo = new SaveFileDialog();
archivo.Filter = "Excel (*.xls)|*.xls";
archivo.FileName = "Inventario Apps Easy " +
DateTime.Now.Date.ToShortDateString().Replace('/', '-');
if (archivo.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application aplicacion;
Microsoft.Office.Interop.Excel.Workbook libroDeTrabajo;
Microsoft.Office.Interop.Excel.Worksheet hojaDeTrabajo;

aplicacion = new Microsoft.Office.Interop.Excel.Application();


libroDeTrabajo = aplicacion.Workbooks.Add();
hojaDeTrabajo
=(Microsoft.Office.Interop.Excel.Worksheet)libroDeTrabajo.Worksheets.get_Item(1);

hojaDeTrabajo.Cells[1, "A"] = grd.Columns[0].HeaderText;


hojaDeTrabajo.Cells[1, "B"] = grd.Columns[1].HeaderText;
hojaDeTrabajo.Cells[1, "C"] = grd.Columns[2].HeaderText;
hojaDeTrabajo.Cells[1, "D"] = grd.Columns[3].HeaderText;

hojaDeTrabajo.Columns[1].AutoFit();
hojaDeTrabajo.Columns[2].AutoFit();
hojaDeTrabajo.Columns[3].AutoFit();
hojaDeTrabajo.Columns[4].AutoFit();
hojaDeTrabajo.Name = "Inventario Actualizado";

//Recorremos el DataGridView rellenando la hoja de trabajo


for (int i = 0; i < grd.Rows.Count - 1; i++)
{
for (int j = 0; j < grd.Columns.Count; j++)
{
if (grd.Rows[i].Cells[j].Value != null)
{
hojaDeTrabajo.Cells[i + 2, j + 1] =
grd.Rows[i].Cells[j].Value.ToString();
}
}
}
libroDeTrabajo.SaveAs(archivo.FileName,

Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
libroDeTrabajo.Close(true);
aplicacion.Quit();
MessageBox.Show("Inventario Exportado a Excel", "Apps Easy");
}
}
catch (Exception ex)
{
MessageBox.Show("Error al exportar la informacion debido a: " +
ex.ToString());
}

}
}

También podría gustarte