From c21a34b822f9687ad1c0008fa5239a54c20a51f6 Mon Sep 17 00:00:00 2001 From: MLopez Date: Wed, 18 Jan 2023 18:29:48 -0700 Subject: [PATCH] =?UTF-8?q?ModExtensiones:=20Validar=20que=20el=20nombre?= =?UTF-8?q?=20de=20la=20hoja=20sea=20v=C3=A1lido,=20si=20no,=20sustituir?= =?UTF-8?q?=20caracteres=20y/o=20truncar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App_Code/modExtensiones.vb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/App_Code/modExtensiones.vb b/App_Code/modExtensiones.vb index c05a379a4..c11e6ed30 100644 --- a/App_Code/modExtensiones.vb +++ b/App_Code/modExtensiones.vb @@ -145,6 +145,23 @@ Namespace Extensiones End If End Function + Public Function Truncate(value As String, length As Integer) As String + ' If argument is too big, return the original string. + ' ... Otherwise take a substring from the string's start index. + If length > value.Length Then + Return value + Else + Return value.Substring(0, length) + End If + End Function + + Public Function ValidFileName(ByVal name As String) As String + For Each c As Char In System.IO.Path.GetInvalidFileNameChars() + name = name.Replace(c.ToString(), "_") + Next + Return Truncate(name, 31) + End Function + Public Class RespuestaCancelacionSAT Public Property Verificado As Boolean = False Public Property CveEstatus As Integer = 0 @@ -2527,7 +2544,7 @@ Namespace Extensiones Dim TotalRowFooter As Integer = 2 Dim worksheet = excel.Worksheets(0) - worksheet.Name = NombreArchivoXLSx + worksheet.Name = ValidFileName(NombreArchivoXLSx) worksheet.Rows.InsertEmpty(0, TotalRowHeader) Dim celdasConTexto = worksheet.GetUsedCellRange(True) celdasConTexto.Style.Font.Name = "Calibri Light" @@ -2603,10 +2620,10 @@ Namespace Extensiones Public Sub ExportToExcelG(Dt As DataTable, NombreArchivoXLSx As String, Optional Titulo As String = "Reporte", Optional Usuario As Usuario = Nothing, Optional Filtros As String = "", Optional AlturaImagen As Double = 100, Optional AnchoImagen As Double = 100) GemBox.Spreadsheet.SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet) - If Dt.TableName Is Nothing Then Dt.TableName = NombreArchivoXLSx + If Dt.TableName Is Nothing Then Dt.TableName = ValidFileName(NombreArchivoXLSx) Dim excel As New GemBox.Spreadsheet.ExcelFile - Dim item = excel.Worksheets.Add(Dt.TableName) + Dim item = excel.Worksheets.Add(Truncate(Dt.TableName, 31)) item.InsertDataTable(Dt, New InsertDataTableOptions() With { @@ -2620,6 +2637,7 @@ Namespace Extensiones Dim TotalRowFooter As Integer = 2 Dim worksheet = excel.Worksheets(0) + 'worksheet.Name = ValidFileName(NombreArchivoXLSx) worksheet.Rows.InsertEmpty(0, TotalRowHeader) Dim celdasConTexto = worksheet.GetUsedCellRange(True) celdasConTexto.Style.Font.Name = "Calibri Light"