3.939 messaggi dal 28 gennaio 2003
Leggo una tabella Excel con la libreria NPOI.
Il valore di una cella letta la debbo mettere in una stringa.

1) se è vuota metto ""
2) se è piena metto nella stringa il suo valore.ToString
3) se contiene una formula VORREI IL SUO VALORE CALCOLATO

mi spiego. Io uso questo codice
If (sheet.GetRow(row) IsNot Nothing) AndAlso sheet.GetRow(row).GetCell(j) IsNot Nothing Then
c = sheet.GetRow(row).GetCell(j).ToString


se però nella cella c'è una formula (ps. = 1+2, io vorrei ottenere 3 e non '1+2'

mi dareste una mano?

ciao
11.886 messaggi dal 09 febbraio 2002
Contributi
ciao Pietro,
anziché usare GetCell(j).ToString() usa GetCell(j).NumericCellValue (se tale valore è numerico). Ti dovrebbe restituire il risultato della formula anziché il contenuto letterale che l'utente aveva digitato nella cella.

ciao,
Moreno

Enjoy learning and just keep making
3.939 messaggi dal 28 gennaio 2003
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))

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.