El método más rápido para eliminar filas y columnas vacías de archivos de Excel usando Interop
Tengo muchos archivos de Excel que contienen datos y contiene filas y columnas vacías. como se muestra abajo
Estoy tratando de eliminar filas y columnas vacías de Excel usando interoperabilidad. Creo una aplicación winform simple y utilicé el siguiente código y funciona bien.
Dim lstFiles As New List(Of String)
lstFiles.AddRange(IO.Directory.GetFiles(m_strFolderPath, "*.xls", IO.SearchOption.AllDirectories))
Dim m_XlApp = New Excel.Application
Dim m_xlWrkbs As Excel.Workbooks = m_XlApp.Workbooks
Dim m_xlWrkb As Excel.Workbook
For Each strFile As String In lstFiles
m_xlWrkb = m_xlWrkbs.Open(strFile)
Dim m_XlWrkSheet As Excel.Worksheet = m_xlWrkb.Worksheets(1)
Dim intRow As Integer = 1
While intRow <= m_XlWrkSheet.UsedRange.Rows.Count
If m_XlApp.WorksheetFunction.CountA(m_XlWrkSheet.Cells(intRow, 1).EntireRow) = 0 Then
m_XlWrkSheet.Cells(intRow, 1).EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp)
Else
intRow += 1
End If
End While
Dim intCol As Integer = 1
While intCol <= m_XlWrkSheet.UsedRange.Columns.Count
If m_XlApp.WorksheetFunction.CountA(m_XlWrkSheet.Cells(1, intCol).EntireColumn) = 0 Then
m_XlWrkSheet.Cells(1, intCol).EntireColumn.Delete(Excel.XlDeleteShiftDirection.xlShiftToLeft)
Else
intCol += 1
End If
End While
Next
m_xlWrkb.Save()
m_xlWrkb.Close(SaveChanges:=True)
Marshal.ReleaseComObject(m_xlWrkb)
Marshal.ReleaseComObject(m_xlWrkbs)
m_XlApp.Quit()
Marshal.ReleaseComObject(m_XlApp)
Pero al limpiar grandes archivos de Excel lleva mucho tiempo. ¿Alguna sugerencia para optimizar este código? u otra forma de limpiar estos archivos de Excel más rápido? ¿Existe una función que pueda eliminar filas vacías con un solo clic?
No tengo problema si las respuestas están usando C #
EDITAR:
Subí un archivo de muestraArchivo de muestra. Pero no todos los archivos tienen la misma estructura.