Exportar Excel en ActualizarPrecios.aspx

This commit is contained in:
2021-09-06 15:39:50 -07:00
parent 8b726f41fa
commit 7baa146c1d
2 changed files with 228 additions and 11 deletions

View File

@@ -19,6 +19,16 @@
<li>
<asp:HyperLink ID="HlAgregarExcel" NavigateUrl="#" Visible="True" data-toggle="modal" data-target="#DivCargaExcel" runat="server"><i class='fa fa-server'></i> Importar Listado</asp:HyperLink>
</li>
<li>
<asp:UpdatePanel ID="UpExportarExcel" runat="server">
<ContentTemplate>
<asp:LinkButton ID="LnkExportarExcel" Text="<i class='fa fa-file-excel-o'></i> ExportarExcel" runat="server" OnClick="LnkExportarExcel_Click"></asp:LinkButton>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="LnkExportarExcel"/>
</Triggers>
</asp:UpdatePanel>
</li>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="C" runat="Server">

View File

@@ -5,7 +5,8 @@ Option Strict On
Imports Extensiones
Imports System.Data
Imports GemBox.Spreadsheet
Imports System.IO
Partial Class Ventas_ActualizarPrecios
Inherits ClsPaginas
@@ -17,6 +18,16 @@ Partial Class Ventas_ActualizarPrecios
ViewState("DtSubProyectos") = value
End Set
End Property
Private Property DtDatosPrecios As DataTable
Get
Return If(IsNothing(ViewState("DtDatosPrecios")), Nothing, CType(ViewState("DtDatosPrecios"), DataTable))
End Get
Set(value As DataTable)
ViewState("DtDatosPrecios") = value
End Set
End Property
Private Property CveSucursal As Integer
Get
If ViewState("CveSucursal") Is Nothing Then
@@ -145,7 +156,7 @@ Partial Class Ventas_ActualizarPrecios
End Sub
Private Sub CargarModelos()
If UsuarioActual.Empresa.Funcionalidad(558).ToString = "1" Then
lblModelo.visible = True
lblModelo.Visible = True
DdlModelo.Visible = True
Dim ObjGeneral As New ClsGenerales
If ObjGeneral.GetModelos(UsuarioActual.CveEmpresa) Then
@@ -173,7 +184,8 @@ Partial Class Ventas_ActualizarPrecios
If objPS.GetPSPreciosLista(UsuarioActual.CveEmpresa, DdlPrecioLista.SelectedValue.ToEntero, DDlClasificacion.SelectedValue.ToEntero, CvePS,
CveProveedor:=CveProveedor, Estado:=DdlEstado.SelectedValue.ToEntero, Marcas:=Marcas, CveModelo:=DdlModelo.SelectedValue.ToEntero) Then
GVPrecios.DataSource = objPS.DtResultados
DtDatosPrecios = objPS.DtResultados
GVPrecios.DataSource = DtDatosPrecios
End If
objPS.Dispose()
@@ -246,8 +258,8 @@ Partial Class Ventas_ActualizarPrecios
TxtImporte.Text = ""
TxtPrecio.Text = ""
TxtPorcentaje.Text = ""
End Sub
End Sub
#Region "MODAL PRECIOS MINIMOS"
Public Sub LnkPrecioMinActual_Click(sender As Object, e As EventArgs)
@@ -305,10 +317,10 @@ Partial Class Ventas_ActualizarPrecios
ObjProductosServicios.Dispose()
End Sub
End Sub
#End Region
#Region "C A R G A R E X C E L "
Private Sub BtnDescargarFormato_Click(sender As Object, e As EventArgs) Handles BtnDescargarFormato.Click
@@ -544,9 +556,9 @@ Partial Class Ventas_ActualizarPrecios
End While
Return dataTable
End Function
End Function
#End Region
Protected Sub gvSucursal_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.FCTextBox("TxtPrecioMinimo").Attributes.Add("onKeyUp", String.Format("FncSeleccionaChk({0});", e.Row.FCCheckBox("chkSucursal").ClientID))
@@ -592,5 +604,200 @@ Partial Class Ventas_ActualizarPrecios
Return True
End If
End Function
End Function
Protected Sub LnkExportarExcel_Click(sender As Object, e As EventArgs)
Dim DtExcel As New DataTable("Precios")
DtExcel.Columns.Add(New DataColumn("Código", GetType(String)))
If UsuarioActual.Empresa.Funcionalidad(531) = "1" Then 'Empresa Acumuladores
DtExcel.Columns.Add(New DataColumn("Código CPK", GetType(String)))
End If
DtExcel.Columns.Add(New DataColumn("Producto/Servicio", GetType(String)))
DtExcel.Columns.Add(New DataColumn("Clasificación", GetType(String)))
DtExcel.Columns.Add(New DataColumn("Importe", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Precio Mínimo", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("U.M.", GetType(String)))
DtExcel.Columns.Add(New DataColumn("Moneda", GetType(String)))
If UsuarioActual.CveEmpresa = 153 Then 'Empresa servec
DtExcel.Columns.Add(New DataColumn("C. Promedio", GetType(Decimal)))
End If
DtExcel.Columns.Add(New DataColumn("E. Mínimo", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Importe Anterior", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Precio Mínimo Anterior", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Fecha Modificación", GetType(DateTime)))
DtExcel.Columns.Add(New DataColumn("Usuario Modificó", GetType(String)))
DtExcel.Columns.Add(New DataColumn("Porcentaje", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Fecha Ingreso", GetType(Decimal)))
DtExcel.Columns.Add(New DataColumn("Usuario Ingresó", GetType(String)))
If DtDatosPrecios IsNot Nothing Then
For Each Row As DataRow In DtDatosPrecios.Rows
Dim NewRow As DataRow = DtExcel.NewRow()
NewRow("Código") = Row("Codigo")
If UsuarioActual.Empresa.Funcionalidad(531) = "1" Then 'Empresa Acumuladores
NewRow("Código CPK") = Row("Codigo2")
End If
NewRow("Producto/Servicio") = Row("PS")
NewRow("Clasificación") = Row("Clasificacion")
NewRow("Importe") = Row("PrecioLista")
NewRow("Precio Mínimo") = Row("PrecioMinimo")
NewRow("U.M.") = Row("UnidadMedida")
NewRow("Moneda") = Row("Moneda")
If UsuarioActual.CveEmpresa = 153 Then 'Empresa servec
NewRow("C. Promedio") = Row("CostoPromedio")
End If
NewRow("E. Mínimo") = Row("VentaMinima")
NewRow("Importe Anterior") = Row("ImporteAnterior")
NewRow("Precio Mínimo Anterior") = Row("PrecioMinimoAnterior")
NewRow("Fecha Modificación") = Row("FechaModificacion")
NewRow("Usuario Modificó") = Row("UsuarioModifico")
NewRow("Porcentaje") = Row("Porcentaje")
NewRow("Fecha Ingreso") = Row("FechaIng")
NewRow("Usuario Ingresó") = Row("Usuario")
DtExcel.Rows.Add(NewRow)
Next
End If
SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet)
Dim FileExcel As New ExcelFile()
Dim worksheet = FileExcel.Worksheets.Add(DtExcel.TableName)
worksheet.InsertDataTable(DtExcel, New InsertDataTableOptions() With
{
.ColumnHeaders = True,
.StartRow = 0,
.StartColumn = 0
})
worksheet.Rows(0).Style.FillPattern.SetPattern(FillPatternStyle.Solid, SpreadsheetColor.FromArgb(75, 119, 190), SpreadsheetColor.FromArgb(255, 255, 255))
worksheet.Rows(0).Style.Font.Color = SpreadsheetColor.FromArgb(255, 255, 255)
Dim columnCount = worksheet.CalculateMaxUsedColumns()
For i = 0 To columnCount
worksheet.Columns(i).AutoFit(1, worksheet.Rows(0), worksheet.Rows(worksheet.Rows.Count - 1))
Next
Dim FechaIMes As String = "", FechaFMes As String = "", Titulo As String = "LISTADO DE PRECIOS", ListaPrecios As String = DdlPrecioLista.SelectedItem.Text, Divisor As Integer = 1, AlturaImagen As Double = 100, AnchoImagen As Double = 100, DatoTituloExtra As String = ""
Dim TotalRowHeader As Integer = 8
Dim TotalRowFooter As Integer = 2
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 = UsuarioActual.Empresa.Empresa
RangoNombreEmpresa.Style.Font.Size = 20 * 20
RangoNombreEmpresa.Style.Font.Name = "Calibri Light"
RangoNombreEmpresa.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center
RangoNombreEmpresa.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 = 17 * 20
RangoTitulo.Style.Font.Name = "Calibri Light"
RangoTitulo.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center
RangoTitulo.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center
'If FechaIMes.Trim.Length > 0 And FechaFMes.Trim.Length > 0 Then
' Dim RangoFechas = worksheet.Cells.GetSubrangeAbsolute(4, celdasConTexto.FirstColumnIndex, 4, celdasConTexto.LastColumnIndex)
' RangoFechas.Merged = True
' RangoFechas.Value = $"Fechas: {FechaIMes} a {FechaFMes}"
' RangoFechas.Style.Font.Size = 15 * 20
' RangoFechas.Style.Font.Name = "Calibri Light"
' RangoFechas.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center
' RangoFechas.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center
'End If
If ListaPrecios.Length > 0 Then
Dim RangoSubProyecto = worksheet.Cells.GetSubrangeAbsolute(5, celdasConTexto.FirstColumnIndex, 5, celdasConTexto.LastColumnIndex)
RangoSubProyecto.Merged = True
RangoSubProyecto.Value = $"Lista de precio: {ListaPrecios}"
RangoSubProyecto.Style.Font.Size = 14 * 20
RangoSubProyecto.Style.Font.Name = "Calibri Light"
RangoSubProyecto.Style.HorizontalAlignment = GemBox.Spreadsheet.HorizontalAlignmentStyle.Center
RangoSubProyecto.Style.VerticalAlignment = GemBox.Spreadsheet.VerticalAlignmentStyle.Center
End If
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 = DtExcel.Rows.Count + TotalRowHeader + TotalRowFooter
worksheet.Rows(IndexEndTable).Cells(0).Value = "Usuario Imprimió"
worksheet.Rows(IndexEndTable + 1).Cells(0).Value = UsuarioActual.Nombre
worksheet.Rows(IndexEndTable).Cells(2).Value = "Fecha Impresión"
worksheet.Rows(IndexEndTable + 1).Cells(2).Value = DateTime.Now.ToString("dd/MM/yyyy")
Dim Imagen As New System.IO.FileInfo(String.Format("{0}/Logo.png", UsuarioActual.Empresa.Ruta))
If Imagen.Exists Then
Dim Ruta As String = String.Format("{0}/Logo.png", UsuarioActual.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
For I = celdasConTexto.FirstColumnIndex To celdasConTexto.LastColumnIndex
If I > 0 Then
If Divisor = 1 Then
worksheet.Columns(I + 1).Style.NumberFormat = "#,##0.00"
Else
worksheet.Columns(I + 1).Style.NumberFormat = "#,##"
End If
Try
worksheet.Columns(I).AutoFit(1)
Catch ex As Exception
End Try
End If
Next
FileExcel.Save(Response, "Precios.xlsx", SaveOptions.XlsxDefault)
End Sub
End Class