Function DataGridAExcel(ByVal MiGrid As DataGridView) As Boolean
Try
'AÑADIMOS EL LIBRO AL PROGRAMA Y LA HOJA AL LIBRO
exLibro = exApp.Workbooks.Add
exHoja = exApp.Sheets(1)
'CONTAMOS COLUMNAS Y FILAS
Dim NCol As Integer = MiGrid.ColumnCount
Dim NRow As Integer = MiGrid.RowCount
'RECORREMOS TODAS LAS FILAS Y POR CADA COLUMNA ESCRIBIMOS
For i As Integer = 1 To NCol
exHoja.Cells.Item(1, i) = MiGrid.Columns(i - 1).Name.ToString
Next
For Fila As Integer = 0 To NRow - 1
For Col As Integer = 0 To NCol - 1
exHoja.Cells.Item(Fila + 2, Col + 1) = MiGrid.Rows(Fila).Cells(Col).Value
Next
Next
Dim M_Izq As Integer = 63
Dim M_Der As Integer = 43
Dim M_Sup As Integer = 10
Dim M_Inf As Integer = 10
'ORIENTACIÓN DE LA HOJA
With exHoja.PageSetup
.Orientation = Excel.XlPageOrientation.xlPortrait
'CONFIGURACIÓN DE MÁRGENES
.LeftMargin = M_Izq
.RightMargin = M_Der
.TopMargin = M_Sup
.BottomMargin = M_Inf
End With
'TÍTULO EN NEGRITA, ALINEADO AL CENTRO DE LAS CELDAS Y COLOR
Dim objRango As Excel.Range = exHoja.Range(exHoja.Cells(1, 1), exHoja.Cells(exHoja.UsedRange.Rows.Count, exHoja.UsedRange.Columns.Count))
Dim contador As Integer = exHoja.Rows.Count
exHoja.PageSetup.PrintTitleRows = exHoja.Rows(1).Address 'PONEMOS LA FILA DE ENCABEZADO EN TODAS LAS HOJAS IMPRESAS
exHoja.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4 'TAMAÑO DE PAPEL A4
exHoja.Name = "HISTÓRICO TENSIÓN"
exHoja.Rows.Item(1).Font.Bold = 1 'NEGRITA
exHoja.Rows.Item(1).Font.ColorIndex = 49 'COLOR DEL ENCABEZADO
exHoja.Rows.Item(1).HorizontalAlignment = 3 'ALINEADO DEL ENCABEZADO
objRango.HorizontalAlignment = 3 'ALINEADO DE LAS COLUMNAS
exHoja.Range("A1").Value = " FECHA "
exHoja.Range("B1").Value = " SISTÓLICA "
exHoja.Range("C1").Value = " DIASTÓLICA "
exHoja.Range("D1").Value = " PULSACIONES "
exHoja.Range("E1").Value = " SATURACIÓN "
objRango.Borders.LineStyle = 1 'BORDES DE LA HOJA
exHoja.Rows.Font.Size = 12 ' TAMAÑO DE LA FUENTE
exHoja.Rows.Font.Name = "Adobe Garamond Pro Bold" 'TIPO DE FUENTE
exHoja.Columns.AutoFit() 'AJUSTE DE LAS COLUMNAS
exHoja.Range("A2:A1048576").Font.ColorIndex = 9 'COLOR DE LA FUENTE DE LA COLUMNA DE FECHAS
'DAMOS FORMATO CONDICIONAL DE LAS CELDAS
For Fila As Integer = 2 To NRow + 1
For Col As Integer = 2 To NCol
Dim FC As String = Chr(64 + Col) & Fila
exHoja.Range(FC).Select()
exHoja.Range(FC).Font.ColorIndex = 32
exHoja.Range(FC).Font.Bold = True
Next
Next
For Fila As Integer = 2 To NRow + 1
For Col As Integer = 2 To NCol - 3
Dim FC As String = Chr(64 + Col) & Fila
exHoja.Range(FC).Select()
If exHoja.Range(FC).Value >= 13 Or exHoja.Range(FC).Value <= 11 Then
exHoja.Range(FC).Font.ColorIndex = 3
exHoja.Range(FC).Font.Bold = True
End If
Next
Next
For Fila As Integer = 2 To NRow + 1
For Col As Integer = 3 To NCol - 2
Dim FC As String = Chr(64 + Col) & Fila
exHoja.Range(FC).Select()
If exHoja.Range(FC).Value <= 5.5 Or exHoja.Range(FC).Value >= 7.5 Then
exHoja.Range(FC).Font.ColorIndex = 3
exHoja.Range(FC).Font.Bold = True
End If
Next
Next
For Fila As Integer = 2 To NRow + 1
For Col As Integer = 4 To NCol - 1
Dim FC As String = Chr(64 + Col) & Fila
exHoja.Range(FC).Select()
If exHoja.Range(FC).Value <= 59 Or exHoja.Range(FC).Value >= 80 Then
exHoja.Range(FC).Font.ColorIndex = 3
exHoja.Range(FC).Font.Bold = True
End If
Next
Next
For Fila As Integer = 2 To NRow + 1
For Col As Integer = 5 To NCol
Dim FC As String = Chr(64 + Col) & Fila
exHoja.Range(FC).Select()
If exHoja.Range(FC).Value <= 90 Then
exHoja.Range(FC).Font.ColorIndex = 3
exHoja.Range(FC).Font.Bold = True
End If
Next
Next
'COMPROBAMOS SI EXISTE EL FICHERO Y LO SOBRESCRIBIMOS SI ES ASÍ
Dim Message As String = "¿QUIERES GUARDAR LA HOJA EXCEL? "
Dim Caption As String = "OPCIÓN DE GUARDAR"
Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
Dim Result As DialogResult
'Mostramos el MessageBox
Result = MessageBox.Show(Message, Caption, Buttons)
exApp.ActiveWindow.WindowState = Excel.XlWindowState.xlMaximized
'RESULTADO DE LA OPCIÓN AFIRMATIVA
If Result = System.Windows.Forms.DialogResult.Yes Then
If System.IO.File.Exists(archivo) = True Then
System.IO.File.Delete(archivo)
exLibro.SaveAs(archivo)
Else
exLibro.SaveAs(archivo)
End If
Else
System.IO.File.OpenRead(archivo)
End If
'APLICACIÓN VISIBLE
exApp.Application.Visible = True
exHoja = Nothing
exLibro = Nothing
exApp = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR AL EXPORTAR A EXCEL", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
Return True
End Function