Ciao. Ti ringrazio.
In una colonna ho notato che mi mettono:
numeri
numeri formattati come testo (numero preceduto da')
numeri in formula (es = 17,2 + 43,4)
premesso questo, ho pensato di leggere i dati in un datatable, con le colonne tutte di tipo stringa
Questo il codice, che mi pare funzioni...
'---------------------------------------------------------------------------------
'Restituisce datatable da uno Sheet Excel NPOI
'La tabella Excel inizia nella cella 0,0 e la prima riga contiene i nomi dei campi
'la scansione si interrompe quando il primo campo è vuoto
'---------------------------------------------------------------------------------
Public Function SheetToDataTable(sheet As NPOI.SS.UserModel.ISheet) As DataTable
Dim dt As New DataTable()
'creo le colonne tutte di tipo stringa
Dim cmax As Integer = 0
For j As Integer = 0 To 999
If (sheet.GetRow(0) IsNot Nothing) AndAlso sheet.GetRow(0).GetCell(j) IsNot Nothing Then
Dim nome As String = sheet.GetRow(0).GetCell(j).ToString
Dim c As New DataColumn(nome, GetType(String))
cmax += 1
dt.Columns.Add(c)
Else
Exit For
End If
Next
'estraggo i dati e riempio la tabella
For row As Integer = 1 To sheet.LastRowNum 'escludo la prima riga
If Not (sheet.GetRow(row) IsNot Nothing AndAlso sheet.GetRow(row).GetCell(0) IsNot Nothing) Then
Exit For
End If
Dim dr As DataRow = dt.NewRow()
For j As Integer = 0 To cmax - 1
Dim c As String = Nothing
If (sheet.GetRow(row) IsNot Nothing) AndAlso sheet.GetRow(row).GetCell(j) IsNot Nothing Then
Dim cell As ICell = sheet.GetRow(row).GetCell(j)
Select Case cell.CellType
Case CellType.Numeric
If NPOI.HSSF.UserModel.HSSFDateUtil.IsCellDateFormatted(cell) Then
c = cell.DateCellValue.ToString("d")
Else
c = cell.NumericCellValue.ToString
End If
Case CellType.String
c = cell.StringCellValue
Case CellType.Formula
Select Case cell.CachedFormulaResultType
Case CellType.Numeric
c = cell.NumericCellValue.ToString
Case CellType.String
c = cell.StringCellValue
Case Else
c = cell.CellFormula
End Select
Case CellType.Boolean
c = cell.BooleanCellValue.ToString
End Select
End If
dr(j) = c
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
il filtro su datatable lo faccio così:
dt.DefaultView.RowFilter = String.Format("[{0}] is not null and ([{1}] is not null and Convert([{1}], 'System.Double') {2})", dt.Columns(0).ColumnName, dt.Columns(3).ColumnName, Me.TextBox1.Text.Replace(","c, "."c))