234 lines
7.8 KiB
VB.net
234 lines
7.8 KiB
VB.net
Option Explicit On
|
|
Option Strict On
|
|
|
|
Imports System.Data
|
|
Imports Extensiones
|
|
Imports System.IO
|
|
Imports DocumentFormat.OpenXml.Spreadsheet
|
|
|
|
Partial Class ZDG_ActualizaPadron
|
|
Inherits ClsPaginas
|
|
|
|
#Region " PROPIEDADES "
|
|
Private Property DTExcel As DataTable
|
|
Get
|
|
DTExcel = CType(ViewState("DTExcel"), DataTable)
|
|
End Get
|
|
Set(value As DataTable)
|
|
ViewState("DTExcel") = value
|
|
End Set
|
|
End Property
|
|
Private Property NombreArchivo As String
|
|
Get
|
|
NombreArchivo = ViewState("NombreArchivo").ToString
|
|
End Get
|
|
Set(value As String)
|
|
ViewState("NombreArchivo") = 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
|
|
CargaDatos()
|
|
End If
|
|
End Sub
|
|
|
|
|
|
#Region "C A R G A - D A T O S"
|
|
Public Sub CargaDatos()
|
|
Dim ObjDG As New ClsDG
|
|
|
|
If ObjDG.GetActualizaPadron And ObjDG.DtResultados.Rows.Count = 1 Then
|
|
DivArchivoCargado.Visible = True
|
|
|
|
Dim Dr = ObjDG.DtResultados.Rows(0)
|
|
|
|
LtNombreArchivo.Text = Dr("NombreArchivo").ToString
|
|
LtRegistrosActualizados.Text = Dr("RegistrosActualizados").ToString
|
|
LtFechaArchivo.Text = Dr("Fecha").ToString.ToDateTime.ToString("dd/MM/yyyy hh:mm tt")
|
|
LtUsuario.Text = Dr("Usuario").ToString
|
|
|
|
End If
|
|
|
|
ObjDG.Dispose()
|
|
End Sub
|
|
|
|
#End Region
|
|
|
|
|
|
#Region " DESCARGA PLANTILLA "
|
|
Private Sub DescargaPlantillaExcel()
|
|
Dim Path As String = String.Format("{0}Temp\PlantillaExcel.xlsx", UsuarioActual.Empresa.Ruta)
|
|
Dim Archivo As New System.IO.FileInfo(Path)
|
|
|
|
If Archivo.Exists Then
|
|
HttpContext.Current.Response.Clear()
|
|
HttpContext.Current.Response.ClearContent()
|
|
HttpContext.Current.Response.ContentType = If(IO.Path.GetExtension(Archivo.Name) = "xml", "application/xml", "application/pdf")
|
|
HttpContext.Current.Response.AppendHeader("Content-disposition", String.Format("attachment; filename={0}", Archivo.Name))
|
|
HttpContext.Current.Response.AddHeader("Content-Length", Archivo.Length.ToString())
|
|
HttpContext.Current.Response.TransmitFile(Path)
|
|
'HttpContext.Current.Response.Flush()
|
|
HttpContext.Current.Response.End()
|
|
End If
|
|
Archivo = Nothing
|
|
End Sub
|
|
#End Region
|
|
|
|
|
|
#Region "E X C E L"
|
|
|
|
Protected Sub BtnCargaExcel_Click(sender As Object, e As EventArgs) Handles BtnCargaExcel.Click
|
|
Dim Ruta = ""
|
|
If FUExcel.HasFile Then
|
|
|
|
'GUARDA ARCHIVO TEMPORAL PARA LECTURA
|
|
NombreArchivo = Path.GetFileName(FUExcel.PostedFile.FileName)
|
|
Dim FilePath As String = Server.MapPath(String.Format("{0}Temp\{1}", Request.ApplicationPath, NombreArchivo))
|
|
FUExcel.SaveAs(FilePath)
|
|
LeerExcel(FilePath)
|
|
|
|
Else
|
|
Alert("No se ha cargado ningun archivo Excel para actualizar. Por favor rectifique y vuelva a intentar", TipoMensaje.MsgAdvertencia)
|
|
End If
|
|
End Sub
|
|
Private Sub LeerExcel(rutaExcel As String)
|
|
|
|
Dim conn As Odbc.OdbcConnection = Nothing
|
|
Dim ErrorExcel = False
|
|
DTExcel = New DataTable
|
|
|
|
Try
|
|
conn = New Odbc.OdbcConnection("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & rutaExcel)
|
|
conn.Open()
|
|
Dim cmd = conn.CreateCommand()
|
|
cmd.CommandType = CommandType.Text
|
|
cmd.CommandText = "SELECT * FROM [Padron$]"
|
|
|
|
Dim da = New Odbc.OdbcDataAdapter(cmd)
|
|
da.Fill(DTExcel)
|
|
Catch ex As Exception
|
|
ErrorExcel = True
|
|
Alert("Ocurrió un error al leer el Excel: " + ex.ToString, TipoMensaje.MsgError)
|
|
End Try
|
|
conn.Close()
|
|
|
|
|
|
'CAMBIA NOMBRE COLUMNAS
|
|
If Not ErrorExcel Then
|
|
DTExcel.Columns(0).ColumnName = "RNM"
|
|
DTExcel.Columns(1).ColumnName = "Direccion"
|
|
DTExcel.Columns(2).ColumnName = "Colonia"
|
|
DTExcel.Columns(3).ColumnName = "Telefono"
|
|
DTExcel.Columns(4).ColumnName = "Correo"
|
|
DTExcel.Columns(5).ColumnName = "Tendencia"
|
|
DTExcel.Columns(6).ColumnName = "Coordinador"
|
|
DTExcel.Columns(7).ColumnName = "TelefonoCoordinador"
|
|
DTExcel.Columns(8).ColumnName = "Lider"
|
|
DTExcel.Columns(9).ColumnName = "TelefonoLider"
|
|
DTExcel.Columns(10).ColumnName = "Movilizador"
|
|
DTExcel.Columns(11).ColumnName = "TelefonoMovilizador"
|
|
|
|
End If
|
|
|
|
LbNumeroRegistros.Text = String.Format("Numero de registros: <b>{0}</b>", DTExcel.Rows.Count)
|
|
|
|
GvDatos.DataSource = DTExcel
|
|
GvDatos.DataBind()
|
|
|
|
If (GvDatos.Rows.Count > 0) Then
|
|
GvDatos.UseAccessibleHeader = True
|
|
GvDatos.HeaderRow.TableSection = TableRowSection.TableHeader
|
|
RegistraScript("OrdenarGV();")
|
|
|
|
BtnProcesarExcel.Visible = True
|
|
|
|
End If
|
|
|
|
UpDatos.Update()
|
|
|
|
End Sub
|
|
Protected Sub BtnProcesarExcel_Click(sender As Object, e As EventArgs)
|
|
If DTExcel.Rows.Count > 0 Then
|
|
ProcesarExcel()
|
|
Else
|
|
Alert("No hay ningun registro a actualizar.", TipoMensaje.MsgAdvertencia)
|
|
End If
|
|
End Sub
|
|
Private Sub ProcesarExcel()
|
|
Dim ObjDG As New ClsDG
|
|
ObjDG.IniciaTransacSQL()
|
|
|
|
Dim ErrorActualizacion = False
|
|
Dim RegistrosActualizados = 0
|
|
|
|
'LEE LINEA POR LINEA
|
|
For Each Dr As DataRow In DTExcel.Rows
|
|
|
|
If ObjDG.UpdatePadron(LimpiarTexto(Dr("RNM").ToString.Trim), Dr("Direccion").ToString, Dr("Colonia").ToString, Dr("Telefono").ToString, Dr("Correo").ToString, Dr("Tendencia").ToString.ToEntero,
|
|
Dr("Coordinador").ToString, Dr("TelefonoCoordinador").ToString, Dr("Lider").ToString, Dr("TelefonoLider").ToString, Dr("Movilizador").ToString, Dr("TelefonoMovilizador").ToString) Then
|
|
|
|
RegistrosActualizados += ObjDG.RegistrosAfectados
|
|
Else
|
|
ErrorActualizacion = True
|
|
Exit For
|
|
End If
|
|
|
|
Next
|
|
|
|
'ACTUALIZA BITACORA
|
|
If Not ErrorActualizacion Then
|
|
If Not ObjDG.AddActualizacionPadron(NombreArchivo, RegistrosActualizados, DateTime.Now, UsuarioActual.Usuario) Then
|
|
ErrorActualizacion = True
|
|
End If
|
|
End If
|
|
|
|
|
|
If Not ErrorActualizacion Then
|
|
ObjDG.Commit()
|
|
Alert("La actualización fue exitosa.", TipoMensaje.MsgInformacion)
|
|
Else
|
|
ObjDG.RollBack()
|
|
Alert("Ocurrió un error en la actualización del Padron. Por favor verifique el archivo excel e intente de nuevo.", TipoMensaje.MsgError)
|
|
End If
|
|
ObjDG.Dispose()
|
|
|
|
|
|
DTExcel.Rows.Clear()
|
|
GvDatos.DataSource = DTExcel
|
|
GvDatos.DataBind()
|
|
BtnProcesarExcel.Visible = False
|
|
CargaDatos()
|
|
UpDatos.Update()
|
|
|
|
End Sub
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
Function LimpiarTexto(texto As String) As String
|
|
Dim consignos As String = "áàäéèëíìïóòöúùuñÁÀÄÉÈËÍÌÏÓÒÖÚÙÜÑçÇ"
|
|
Dim sinsignos As String = "aaaeeeiiiooouuunAAAEEEIIIOOOUUUNcC"
|
|
Dim textoSinAcentos As System.Text.StringBuilder = New System.Text.StringBuilder(texto.Length)
|
|
Dim indexConAcento As Integer
|
|
|
|
For Each caracter As Char In texto
|
|
indexConAcento = consignos.IndexOf(caracter)
|
|
If (indexConAcento > -1) Then
|
|
textoSinAcentos.Append(sinsignos.Substring(indexConAcento, 1))
|
|
Else
|
|
textoSinAcentos.Append(caracter)
|
|
End If
|
|
Next
|
|
|
|
Dim regex As New Regex("[ ](?=[ ])|[^A-Za-z0-9]+")
|
|
|
|
Return regex.Replace(textoSinAcentos.ToString, "")
|
|
End Function
|
|
|
|
|
|
End Class
|