1

Me gustaria saber como puedo darle formato al encabezado de mi archivo de excel que genero para exportar, estoy usando COM/interop excel 16.0, les muestro mi codigo con el que recorre mi datagridview y despues pasa a excel:

        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        excel.Application.Workbooks.Add(true);

        object[,] datos = new object[tabla.Rows.Count + 1, tabla.Columns.Count]; // +1 por la cabecera
        for (int j = 0; j < tabla.Columns.Count; j++) //cabeceras
        {
            datos[0, j] = tabla.Columns[j].Name;
        }


        for (int i = 0; i < tabla.Rows.Count; i++)
        {
            for (int j = 0; j < tabla.Columns.Count; j++)
            {

                datos[i + 1, j] = tabla.Rows[i].Cells[j].Value;
            }
        }

1 respuesta 1

1

Debes crear un estilo y asignarlo a un rango

Ejemplo

private Style GetStyleHeader(Workbook wb)
{
    Style style = wb.Styles.Add("MiEstilo");
    style.Font.Bold = true;
    style.HorizontalAlignment = XlHAlign.xlHAlignCenter;
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
          
    return style;
}

Código completo:

private object[,] GetDataFromDGV(DataGridView dgv)
{
    object[,] datos = new object[dataGridView1.Rows.Count + 1, dataGridView1.Columns.Count]; // +1 por la cabecera
    for (int j = 0; j < dataGridView1.Columns.Count; j++)
    {
        datos[0, j] = dataGridView1.Columns[j].Name;
    }
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
            datos[i + 1, j] = dataGridView1.Rows[i].Cells[j].Value;
        }
        progressBar1.Value++;
    }
    return datos;
}
private Style GetStyleHeader(Workbook wb)
{
    Style style = wb.Styles.Add("MiEstilo");
    style.Font.Bold = true;
    style.HorizontalAlignment = XlHAlign.xlHAlignCenter;
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
          
    return style;
}
private void button1_Click(object sender, EventArgs e)
{
         
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    excel.Application.Workbooks.Add(true);
    var datos = GetDataFromDGV(dataGridView1);
    var style = GetStyleHeader(excel.Application.ActiveWorkbook);

    excel.Range[excel.Cells[1, 1], excel.Cells[1, datos.GetLength(1)]].Style = style;
    excel.Range[excel.Cells[1, 1], excel.Cells[datos.GetLength(0), datos.GetLength(1)]].Value = datos;
    excel.Visible = true;
    Worksheet worksheet = (Worksheet)excel.ActiveSheet;
    worksheet.Activate();
}

Fuente: Documentación

5
  • Excelente, te agradezco una vez más por tu ayuda... una pregunta más sabrias como hacer para que cuando se exporte las columnas y filas tomen todo el espacio de las celdas? no se si me explique bien el 1 feb. a las 14:33
  • @RicardoPalacios Me alegro te haya servido. Te refieres a que la cela de expanda para que el contenido no se vea cortado?? o es otra cosa?
    – Yussef
    el 1 feb. a las 14:36
  • Si, asi es... tengo entendido que es al rango y aplicar autifill verdad? pero solo es para columnas y quisiera que sea para filas y columnas el 1 feb. a las 14:48
  • @RicardoPalacios La verdad es que para las filas no se hacerlo ni a mano en el mismo Excel
    – Yussef
    el 2 feb. a las 3:42
  • Bueno igualmente te agradezco tu ayuda :) el 2 feb. a las 22:37

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.