Option Explicit On Option Strict On Imports System.Data Imports System.IO Imports Extensiones Imports GemBox.Spreadsheet Imports Microsoft.Reporting.WebForms Partial Class AF_RegistroRevision Inherits ClsPaginas #Region " PROPIEDADES" Property DT_Cabeceras As DataTable Get If ViewState("DT_Cabeceras") Is Nothing Then Return New DataTable Else Return DirectCast(ViewState("DT_Cabeceras"), DataTable) End If End Get Set(value As DataTable) ViewState("DT_Cabeceras") = value End Set End Property Property DT_Datos As DataTable Get If ViewState("DT_Datos") Is Nothing Then Return New DataTable Else Return DirectCast(ViewState("DT_Datos"), DataTable) End If End Get Set(value As DataTable) ViewState("DT_Datos") = value End Set End Property Property ReporteBytes As Byte() Get If ViewState("ReporteBytes") Is Nothing Then Return Nothing Else Return DirectCast(ViewState("ReporteBytes"), Byte()) End If End Get Set(value As Byte()) ViewState("ReporteBytes") = value End Set End Property #End Region Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then CargaCombos() 'CargaDatos() TxtFecha.Text = $"{NowADS().Day.ToString("00")}/{NowADS().Month.ToString("00")}/{NowADS().Year}" End If End Sub #Region " CARGA PARAMETROS " #End Region #Region " CARGA COMBOS " Protected Sub CargaCombos() CargarCentrosDeCosto(Nothing, DdlSubProyecto, Nothing, LblSubProyecto) LblSubProyecto2.Text = LblSubProyecto.Text End Sub #End Region #Region " CARGA DATOS " 'Protected Sub BtnFiltrar_Click(sender As Object, e As EventArgs) ' CargaDatos() 'End Sub #End Region #Region " GV DATOS " Private Sub CargaDatos() Dim ObjAF As New ClsAF If ObjAF.GetMetricasRegistro(DdlSubProyecto.SelectedValue.ToEntero, UsuarioActual.CveEmpresa, TxtFecha.Text.ToDate) Then DT_Cabeceras = ObjAF.DSResultados.Tables(0) DT_Datos = ObjAF.DSResultados.Tables(1) If DT_Cabeceras.Rows.Count > 0 AndAlso DT_Datos.Rows.Count > 0 Then BtnGuardar.Visible = True Else BtnGuardar.Visible = False GvDatos.DataSource = Nothing GvDatos.DataBind() End If Else GvDatos.DataSource = Nothing GvDatos.DataBind() BtnGuardar.Visible = False End If ObjAF.Dispose() If DT_Datos.Rows.Count > 0 Then CargaTablas() End If UpGuardar.Update() UpDatos.Update() End Sub Private Sub CargaTablas() Dim IDX_LIMPIAR As Integer = 1 For IDX_LIMPIAR = 1 To 15 GvDatos.Columns(IDX_LIMPIAR).HeaderText = "" GvDatos.Columns(IDX_LIMPIAR).Visible = False Next Dim x As Integer = 0 Dim limite As Integer = DT_Cabeceras.Rows.Count - 1 For x = 0 To limite GvDatos.Columns(x + 1).HeaderText = DT_Cabeceras.Rows(x)("Metrica").ToString GvDatos.Columns(x + 1).Visible = True Next x GvDatos.DataSource = DT_Datos GvDatos.DataBind() End Sub Protected Sub GvDatos_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvDatos.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.FCDropDownList("DdlPersonal").DataSource = GlobalDTRHPersonal(UsuarioActual.CveEmpresa).FiltraParametros("CveEstatusPersonal = 1") e.Row.FCDropDownList("DdlPersonal").DataBind() e.Row.FCDropDownList("DdlPersonal").Items.Insert(0, New ListItem("Selecione uno", "-1")) e.Row.FCDropDownList("DdlPersonal").SelectedValueExist(e.Row.DataKey("CvePersonaResponsable")) End If End Sub #End Region Protected Sub LnkEliminarMetricaValor_Click(sender As Object, e As EventArgs) End Sub Protected Sub DdlSubProyecto_SelectedIndexChanged(sender As Object, e As EventArgs) If DdlSubProyecto.SelectedValue.ToEntero > 0 Then CargaDatos() Else GvDatos.DataSource = Nothing GvDatos.DataBind() UpDatos.Update() End If End Sub Protected Sub BtnGuardar_Click(sender As Object, e As EventArgs) Dim ObjAF As New ClsAF Dim limite As Integer = DT_Cabeceras.Rows.Count - 1 ObjAF.IniciaTransacSQL() Dim ErrorProceso As Boolean = False For Each GvRow As GridViewRow In GvDatos.Rows If GvRow.RowType = DataControlRowType.DataRow Then Dim DdlPersonal As DropDownList = GvRow.FCDropDownList("DdlPersonal") If DdlPersonal.SelectedValue.ToEntero <= 0 Then Alert($"unidad: {GvRow.DataKey("Unidad")} require personal", TipoMensaje.MsgAdvertencia) ErrorProceso = True Exit For End If If ObjAF.AddMetricasValores(GvRow.DataKey("CveActivoFijo").ToEntero, DdlPersonal.SelectedValue.ToEntero, UsuarioActual.CveUsuario, DdlSubProyecto.SelectedValue.ToEntero, TxtFecha.Text.ToDate, UsuarioActual.CveEmpresa) Then Dim CveMetricaValor As Integer = ObjAF.CveMetricaValor Dim x As Integer = 0 For x = 0 To limite Dim valor As Decimal = GvRow.FCTextBox($"TxtMetrica{x + 1}").Text.ToDecimal ' valor = If(valor.Trim.Length = 0, "0", valor) If ObjAF.AddMetricasValoresDetalle(CveMetricaValor, DT_Cabeceras(x)("CveMetrica").ToString.ToEntero, valor) Then ErrorProceso = False Else ErrorProceso = True Exit For End If Next x If ErrorProceso Then Exit For End If Else ErrorProceso = True Exit For End If End If Next If Not ErrorProceso Then ObjAF.Commit() ExportToExcelG(GvDatos, $"RegistroRevision{NowADS().ToString("yyyyMMddmmss")}", UsuarioActual, "ReporteRevision", 100, 100, filtros:=$"Filtros {LblSubProyecto.Text}: {DdlSubProyecto.SelectedItem.Text} Fecha: {TxtFecha.Text}") RegistraScript("$('#ModalDescargaReporte').modal({ backdrop: 'static', keyboard: false });") GvDatos.DataSource = Nothing GvDatos.DataBind() BtnGuardar.Visible = False UpDatos.Update() UpGuardar.Update() Else ObjAF.RollBack() End If ObjAF.Dispose() End Sub #Region "DESCARGA EXCEL" Public Sub ExportToExcelG(Gv1 As GridView, NombreArchivoXLSx As String, Usuario As Usuario, Titulo As String, Optional AlturaImagen As Double = 100, Optional AnchoImagen As Double = 100, Optional filtros As String = "") For Each GvRow As GridViewRow In Gv1.Rows Dim IDX_LIMPIAR As Integer = 1 For IDX_LIMPIAR = 1 To 15 GvRow.Cells(IDX_LIMPIAR).Text = GvRow.FCTextBox($"TxtMetrica{IDX_LIMPIAR}").Text Next If GvRow.FCDropDownList("DDLPersonal").SelectedValue.ToEntero > 0 Then GvRow.Cells(16).Text = GvRow.FCDropDownList("DDLPersonal").SelectedItem.Text Else GvRow.Cells(16).Text = "" End If Next Dim sw1 As New StringWriter() Dim htw1 As New HtmlTextWriter(sw1) Gv1.RenderControl(htw1) Dim options = GemBox.Spreadsheet.LoadOptions.HtmlDefault Dim htmlStream1 = New MemoryStream(options.Encoding.GetBytes(sw1.ToString())) htmlStream1.Position = 0 Dim sr1 As New StreamReader(htmlStream1) Dim myStr1 = sr1.ReadToEnd() sr1.Dispose() htmlStream1.Dispose() sw1.Dispose() Dim HtmlCompleto As String = myStr1 HtmlCompleto = Regex.Replace(HtmlCompleto, "\<[\/]*a[^\>]*\>", "") Dim asd As String = "" Dim htmlStream5 = New MemoryStream(options.Encoding.GetBytes(HtmlCompleto)) GemBox.Spreadsheet.SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet()) Dim excel As GemBox.Spreadsheet.ExcelFile excel = GemBox.Spreadsheet.ExcelFile.Load(htmlStream5, options) If Usuario IsNot Nothing Then Dim TotalRowHeader As Integer = 8 Dim TotalRowFooter As Integer = 2 Dim worksheet = excel.Worksheets(0) worksheet.Rows.InsertEmpty(0, TotalRowHeader) Dim celdasConTexto = worksheet.GetUsedCellRange(True) celdasConTexto.Style.Font.Name = "Calibri Light" Dim RangoNombreEmpresa = worksheet.Cells.GetSubrangeAbsolute(1, celdasConTexto.FirstColumnIndex, 1, celdasConTexto.LastColumnIndex) RangoNombreEmpresa.Merged = True RangoNombreEmpresa.Value = Usuario.Empresa.Empresa RangoNombreEmpresa.Style.Font.Size = 18 * 20 RangoNombreEmpresa.Style.Font.Name = "Calibri Light" RangoNombreEmpresa.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center RangoNombreEmpresa.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center Dim RangoFiltros = worksheet.Cells.GetSubrangeAbsolute(6, celdasConTexto.FirstColumnIndex, 6, celdasConTexto.LastColumnIndex) RangoFiltros.Merged = True RangoFiltros.Value = filtros RangoFiltros.Style.Font.Size = 15 * 20 RangoFiltros.Style.Font.Name = "Calibri Light" RangoFiltros.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Left RangoFiltros.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center Dim RangoTitulo = worksheet.Cells.GetSubrangeAbsolute(3, celdasConTexto.FirstColumnIndex, 3, celdasConTexto.LastColumnIndex) RangoTitulo.Merged = True RangoTitulo.Value = Titulo RangoTitulo.Style.Font.Size = 20 * 20 RangoTitulo.Style.Font.Name = "Calibri Light" RangoTitulo.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center RangoTitulo.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center For i = celdasConTexto.FirstColumnIndex To celdasConTexto.LastColumnIndex worksheet.Rows(8).Cells(i).Style.Font.Size = 12 * 20 worksheet.Rows(8).Cells(i).Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center worksheet.Rows(8).Cells(i).Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center worksheet.Rows(8).Cells(i).Style.FillPattern.SetPattern(GemBox.Spreadsheet.FillPatternStyle.Solid, GemBox.Spreadsheet.SpreadsheetColor.FromArgb(75, 119, 190), GemBox.Spreadsheet.SpreadsheetColor.FromArgb(255, 255, 255)) worksheet.Rows(8).Cells(i).Style.Font.Color = GemBox.Spreadsheet.SpreadsheetColor.FromArgb(255, 255, 255) If i > 0 Then Try worksheet.Columns(i).AutoFit(1) Catch ex As Exception End Try End If Next Dim IndexEndTable As Integer = Gv1.Rows.Count + 4 + TotalRowHeader + TotalRowFooter worksheet.Rows(IndexEndTable).Cells(0).Value = "Usuario Imprimió" worksheet.Rows(IndexEndTable + 1).Cells(0).Value = Usuario.Nombre worksheet.Rows(IndexEndTable).Cells(2).Value = "Fecha Impresión" worksheet.Rows(IndexEndTable + 1).Cells(2).Value = NowADS().ToString("dd/MM/yyyy") Dim Imagen As New System.IO.FileInfo(String.Format("{0}/Logo.png", Usuario.Empresa.Ruta)) If Imagen.Exists Then Dim Ruta As String = String.Format("{0}/Logo.png", Usuario.Empresa.Ruta) Using sr As New StreamReader(Ruta) Dim ms As New MemoryStream() sr.BaseStream.CopyTo(ms) worksheet.Pictures.Add(ms, ExcelPictureFormat.Png, "A1", AnchoImagen, AlturaImagen, LengthUnit.Pixel) End Using End If End If Dim memoryStream As New MemoryStream() excel.Save(memoryStream, SaveOptions.XlsxDefault) ReporteBytes = memoryStream.ToArray() memoryStream.Dispose() End Sub Protected Sub LnkDescargaReporte_Click(sender As Object, e As EventArgs) Response.ContentType = "application/force-download" Response.AppendHeader("Content-Disposition", $"attachment;filename=RegistroRevision{NowADS().ToString("yyyyMMddmmss")}.xlsx") Response.BinaryWrite(ReporteBytes) Response.End() End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub #End Region End Class