02/06/2026 V 5.0.8 Se añade parametro opcional a docx.combinar
This commit is contained in:
@@ -11,11 +11,11 @@ Imports System.Data
|
||||
|
||||
Namespace Utilidades
|
||||
Public Class Docx
|
||||
Public Shared Function CombinaDocx(tabla As DataTable, Plantilla() As Byte, FormatoPDF As Boolean) As IO.MemoryStream
|
||||
Public Shared Function CombinaDocx(tabla As DataTable, Plantilla() As Byte, FormatoPDF As Boolean, Optional manejadorCalculateVariable As CalculateDocumentVariableEventHandler = Nothing) As IO.MemoryStream
|
||||
Try
|
||||
Dim ms As New IO.MemoryStream
|
||||
'Dim fs As New IO.FileStream("c:\tmp\pruebamerge.docx", IO.FileMode.Create)
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), tabla, ms, 0, tabla.Rows.Count, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), tabla, ms, 0, tabla.Rows.Count, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, manejadorCalculateVariable)
|
||||
ms.Seek(0, IO.SeekOrigin.Begin)
|
||||
'Dim fs As New IO.FileStream("c:\tmp\ms.docx", IO.FileMode.Create)
|
||||
'ms.WriteTo(fs)
|
||||
@@ -29,7 +29,7 @@ Namespace Utilidades
|
||||
Throw New Exception(ex.Message, ex)
|
||||
End Try
|
||||
End Function
|
||||
Public Shared Function CombinaDocx(tabla As DataTable, Plantilla() As Byte, FormatoPDF As Boolean, Optional NumRegBloque As Integer = 1000) As Byte()
|
||||
Public Shared Function CombinaDocx(tabla As DataTable, Plantilla() As Byte, FormatoPDF As Boolean, Optional NumRegBloque As Integer = 1000, Optional manejadorCalculateVariable As CalculateDocumentVariableEventHandler = Nothing) As Byte()
|
||||
Try
|
||||
|
||||
If FormatoPDF Then
|
||||
@@ -49,7 +49,7 @@ Namespace Utilidades
|
||||
iRegFin = Math.Min((i * NumRegBloque) + NumRegBloque - 1, tabla.Rows.Count - 1)
|
||||
Debug.WriteLine(Now.ToString & " Bloque " & i.ToString & " " & iRegIni & "-" & iRegFin)
|
||||
p = New IO.MemoryStream(Plantilla)
|
||||
Utilidades.Docx.Combinar(p, tabla, fs, iRegIni, iRegFin, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
||||
Utilidades.Docx.Combinar(p, tabla, fs, iRegIni, iRegFin, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, manejadorCalculateVariable)
|
||||
fs.Close()
|
||||
GC.Collect()
|
||||
GC.WaitForPendingFinalizers()
|
||||
@@ -72,7 +72,7 @@ Namespace Utilidades
|
||||
|
||||
Dim fs As New IO.FileStream(sdocx, IO.FileMode.CreateNew, IO.FileAccess.Write)
|
||||
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), tabla, fs, 0, tabla.Rows.Count, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), tabla, fs, 0, tabla.Rows.Count, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, manejadorCalculateVariable)
|
||||
fs.Close()
|
||||
Return IO.File.ReadAllBytes(sdocx) ' tsl5.Ficheros.FicheroAArrayBytes(sdocx)
|
||||
End If
|
||||
@@ -81,26 +81,76 @@ Namespace Utilidades
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Shared Sub Combinar(Plantilla As IO.Stream, Datos As Object, Destino As IO.Stream, PrimerRegistro As Integer, UltimoRegistro As Integer, Formato As DevExpress.XtraRichEdit.DocumentFormat)
|
||||
'Public Shared Sub Combinar(Plantilla As IO.Stream, Datos As Object, Destino As IO.Stream, PrimerRegistro As Integer, UltimoRegistro As Integer, Formato As DevExpress.XtraRichEdit.DocumentFormat)
|
||||
' Try
|
||||
' Dim docServer As New RichEditDocumentServer
|
||||
' docServer.LoadDocument(Plantilla, Formato)
|
||||
' Dim options = docServer.CreateMailMergeOptions()
|
||||
' options.FirstRecordIndex = PrimerRegistro
|
||||
' options.LastRecordIndex = UltimoRegistro
|
||||
' options.MergeMode = API.Native.MergeMode.NewSection
|
||||
' docServer.Options.MailMerge.DataSource = Datos
|
||||
' docServer.Options.MailMerge.ViewMergedData = True
|
||||
' docServer.Options.Export.Html.EmbedImages = True
|
||||
' docServer.Options.MailMerge.ActiveRecord = 0
|
||||
' docServer.MailMerge(options, Destino, Formato)
|
||||
' docServer.Dispose()
|
||||
' GC.Collect()
|
||||
' GC.WaitForPendingFinalizers()
|
||||
' Catch ex As Exception
|
||||
' Throw New Exception(ex.Message, ex)
|
||||
' End Try
|
||||
'End Sub
|
||||
Public Shared Sub Combinar(
|
||||
Plantilla As IO.Stream,
|
||||
Datos As Object,
|
||||
Destino As IO.Stream,
|
||||
PrimerRegistro As Integer,
|
||||
UltimoRegistro As Integer,
|
||||
Formato As DevExpress.XtraRichEdit.DocumentFormat,
|
||||
Optional manejadorCalculateVariable As CalculateDocumentVariableEventHandler = Nothing
|
||||
)
|
||||
|
||||
Try
|
||||
Dim docServer As New RichEditDocumentServer
|
||||
Dim docServer As New RichEditDocumentServer()
|
||||
|
||||
' Cargar plantilla
|
||||
docServer.LoadDocument(Plantilla, Formato)
|
||||
|
||||
' Si el usuario pasa un manejador, lo enganchamos
|
||||
If manejadorCalculateVariable IsNot Nothing Then
|
||||
AddHandler docServer.CalculateDocumentVariable, manejadorCalculateVariable
|
||||
End If
|
||||
|
||||
' Configuración del MailMerge
|
||||
Dim options = docServer.CreateMailMergeOptions()
|
||||
options.FirstRecordIndex = PrimerRegistro
|
||||
options.LastRecordIndex = UltimoRegistro
|
||||
options.MergeMode = API.Native.MergeMode.NewSection
|
||||
|
||||
docServer.Options.MailMerge.DataSource = Datos
|
||||
docServer.Options.MailMerge.ViewMergedData = True
|
||||
docServer.Options.Export.Html.EmbedImages = True
|
||||
docServer.Options.MailMerge.ActiveRecord = 0
|
||||
|
||||
' Ejecutar combinación
|
||||
docServer.MailMerge(options, Destino, Formato)
|
||||
|
||||
' Desenganchar el manejador si se usó
|
||||
If manejadorCalculateVariable IsNot Nothing Then
|
||||
RemoveHandler docServer.CalculateDocumentVariable, manejadorCalculateVariable
|
||||
End If
|
||||
|
||||
docServer.Dispose()
|
||||
GC.Collect()
|
||||
GC.WaitForPendingFinalizers()
|
||||
|
||||
Catch ex As Exception
|
||||
Throw New Exception(ex.Message, ex)
|
||||
End Try
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub ExportarApdf(FicheroOrigen As String, FicheroDestino As String)
|
||||
Try
|
||||
Dim docServer As New RichEditDocumentServer
|
||||
@@ -298,11 +348,11 @@ Namespace Utilidades
|
||||
sourceSection.EndUpdateFooter(source)
|
||||
targetSection.EndUpdateFooter(target)
|
||||
End Sub
|
||||
Public Shared Function CombinaDocxStream(Datos As Object, Plantilla() As Byte, PrimerRegistro As Integer, UltimoRegistro As Integer, FormatoPDF As Boolean) As IO.Stream
|
||||
Public Shared Function CombinaDocxStream(Datos As Object, Plantilla() As Byte, PrimerRegistro As Integer, UltimoRegistro As Integer, FormatoPDF As Boolean, Optional manejadorCalculateVariable As CalculateDocumentVariableEventHandler = Nothing) As IO.Stream
|
||||
Try
|
||||
Dim ms As New IO.MemoryStream
|
||||
'Dim fs As New IO.FileStream("c:\tmp\pruebamerge.docx", IO.FileMode.Create)
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), Datos, ms, PrimerRegistro, UltimoRegistro, Global.DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
||||
Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), Datos, ms, PrimerRegistro, UltimoRegistro, Global.DevExpress.XtraRichEdit.DocumentFormat.OpenXml, manejadorCalculateVariable)
|
||||
If ms.CanSeek Then ms.Seek(0, IO.SeekOrigin.Begin)
|
||||
'Dim fs As New IO.FileStream("c:\tmp\ms.docx", IO.FileMode.Create)
|
||||
'ms.WriteTo(fs)
|
||||
|
||||
Reference in New Issue
Block a user