[ Foro de BASIC ]

exportar color de celdas datagrid a excel

03-Nov-2014 19:56
Invitado (yo)
3 Respuestas

Necesito exportar un datagrid en visual 2008 a excel, tengo problemas para exportar el color de las celdas... Debo respetar los colores del datagrid... El contenido no tengo problema, la cuestion son los colores de las celdas...He encontrado ayuda pero en lenguaje C... Como lo hago en Visual 8? Muchas gracias...


06-Nov-2014 22:06
Nacho Cabanes (+84)

Si pones lo que has encontrado en C quizá te podamos ayudar a convertirlo a BASIC...


11-Nov-2014 19:13
Invitado (Lalale)

Les paso lo que encontré en C#, a ver si pueden ayudarme a pasarlo a visual 8... GRACIAS!!!


PRIMERO IMPORTAMOS LAS LIBRERIAS DE EXCEL

using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

Y AGREGAMOS EL METODO

public void ExportToExcel(DataGridView dgView, ProgressBar pBar)
       {

           try
           {
               if (pBar != null)
               {
                   pBar.Maximum = dgView.RowCount;
                   pBar.Value = 0;
                   if (!pBar.Visible) pBar.Visible = true;
               }
               string sFont = "Verdana";
               int iSize = 8;
               //CREACIÓN DE LOS OBJETOS DE EXCEL
               Excel.Application xlsApp = new Excel.Application();
               Excel.Worksheet xlsSheet;
               Excel.Workbook xlsBook;                
               //AGREGAMOS EL LIBRO Y HOJA DE EXCEL
               xlsBook = xlsApp.Workbooks.Add(true);
               xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet;
               //ESPECIFICAMOS EL TIPO DE LETRA Y TAMAÑO DE LA LETRA DEL LIBRO
               xlsSheet.Rows.Cells.Font.Size = iSize;
               xlsSheet.Rows.Cells.Font.Name = sFont;
               //AGREGAMOS LOS ENCABEZADOS
               int iFil = 0,iCol = 0;
               foreach (DataGridViewColumn column in dgView.Columns)
                   if (column.Visible)
                       xlsSheet.Cells[1, ++iCol] = column.HeaderText;
               //MARCAMOS LAS CELDAS DEL ENCABEZADO EN NEGRITA Y EN COLOR DE RELLENO GRIS
               xlsSheet.get_Range(xlsSheet.Cells[1, 1], xlsSheet.Cells[1, dgView.ColumnCount]).Font.Bold = true;
               xlsSheet.get_Range(xlsSheet.Cells[1, 1], xlsSheet.Cells[1, dgView.ColumnCount]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);
               //RECORRIDO DE LAS FILAS Y COLUMNAS (PINTADO DE CELDAS)
               Excel.Range r;
               Color c;
               for (iFil = 0; iFil < dgView.RowCount; iFil++)
               {
                   for (iCol = 0; iCol < dgView.ColumnCount; iCol++)
                   {
                       xlsSheet.Cells[iFil + 2, iCol + 1] = dgView.Rows[iFil].Cells[iCol].Value.ToString();
                       c = dgView.Rows[iFil].Cells[iCol].Style.BackColor;
                       if (!c.IsEmpty){// COMPARAMOS SI ESTÁ PINTADA LA CELDA (SI ES VERDADERO PINTAMOS LA CELDA)
                           r = (Excel.Range)xlsSheet.Cells[iFil + 2, iCol + 1];
                           xlsSheet.get_Range(r, r).Interior.Color = System.Drawing.ColorTranslator.ToOle(dgView.Rows[iFil].Cells[iCol].Style.BackColor);
                       }
                   }
                   pBar.Value += 1;
               }                
               xlsSheet.Columns.AutoFit();
               xlsSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
               xlsSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLetter;
               xlsSheet.PageSetup.Zoom = 80;

               Excel.Range rango = xlsSheet.get_Range(xlsSheet.Cells[1, 1], xlsSheet.Cells[dgView.RowCount + 1, dgView.ColumnCount]);                
               rango.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
               rango.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
               //rango.Cells.AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatList1, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
               xlsApp.Visible = true;                
           }
           catch
           {
               MessageBox.Show("Error");
           }
           finally
           {
               if (pBar != null)
               {
                   pBar.Value = 0;
                   pBar.Visible = false;
               }                
           }
       }


11-Nov-2014 23:58
Nacho Cabanes (+84)

Puedes usar SharpDevelop, que te permite desarrollar en C# o en VB.Net (porque imagino que con "Visual" te refieres a "VisualBasic.net", y te permite convertir el fuente de un lenguaje a otro solamente con un click.






(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)