IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

contrôle des données-Import excel dans GridView en vb.net


Sujet :

ASP.NET

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut contrôle des données-Import excel dans GridView en vb.net
    Salut
    ce bout de code me permet d'importer un fichier excel dans un GridView:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
        Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
            If FileUpload2.HasFile Then
                Dim FileName As String = Path.GetFileName(FileUpload2.PostedFile.FileName)
                Dim Extension As String = Path.GetExtension(FileUpload2.PostedFile.FileName)
                Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
                Dim FilePath As String = Server.MapPath(FolderPath + FileName)
                FileUpload2.SaveAs(FilePath)
                Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
            End If
        End Sub
     
        Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
            Dim conStr As String = ""
            Select Case Extension
                Case ".xls"
                    'Excel 97-03
                    conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString()
                    Exit Select
                Case ".xlsx"
                    'Excel 07
                    conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString()
                    Exit Select
            End Select
            conStr = String.Format(conStr, FilePath, isHDR)
            Dim connExcel As New OleDbConnection(conStr)
            Dim cmdExcel As New OleDbCommand()
            Dim oda As New OleDbDataAdapter()
            Dim dt As New DataTable()
            cmdExcel.Connection = connExcel
            'Get the name of First Sheet
            connExcel.Open()
            Dim dtExcelSchema As DataTable
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            connExcel.Close()
            'Read Data from First Sheet
            connExcel.Open()
            cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
            oda.SelectCommand = cmdExcel
            oda.Fill(dt)
            connExcel.Close()
            'Bind Data to GridView
            GridViewXX.Caption = Path.GetFileName(FilePath)
            GridViewXX.DataSource = dt
            '----
     
            '----
            GridViewXX.DataBind()
        End Sub
     
        Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
            Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
            Dim FileName As String = GridViewXX.Caption
            Dim Extension As String = Path.GetExtension(FileName)
            Dim FilePath As String = Server.MapPath(FolderPath + FileName)
            Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
            GridViewXX.PageIndex = e.NewPageIndex
            GridViewXX.DataBind()
        End Sub
    Et je veux maintenant faire un contrôle des données au moment de l'import (exp:champs nuls).
    j'ai essayé d'ajouter ceci , dans Import_To_Grid, seulement pour voir si la sélection de la cellule marche mais cela ne donne aucun effet:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    For Each gvr As GridViewRow In GridViewXX.Rows
                    GridViewXX.Columns(1).ControlStyle.BackColor = Drawing.Color.Yellow
                Next gvr
    une idée ?
    et Merci par avance.

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    Bonjour,

    voilà, le contrôle RowDataBound me permet de sélectionner toutes les cellules
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewXX.RowDataBound
            e.Row.BackColor = Drawing.Color.Yellow
        End Sub
    mais je trouve une difficulté d'indexer les cellules car selement e.Row.Cells(0) qui marche mais quand je mets e.Row.Cells(1) ça me donne l'erreur suivante:
    L'argument spécifié n'était pas dans les limites de la plage des valeurs valides.
    Nom du paramètre : index
    Merci.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    voilà,
    ceci me permet de sélectionner les autres colonnes:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    If e.Row.RowType = DataControlRowType.DataRow Then
                e.Row.Cells(1).BackColor = Drawing.Color.Blue
    End If
    Merci.

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    Bonjour,
    Re :-)

    j'essaie de vérifier si les champs d'une colonne sont vides, j'ai mis ce code dans mon GridView1_RowDataBound mais il ne marche pas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    If String.IsNullOrEmpty(Trim(CStr(e.Row.Cells(1).Text))) Then
                    e.Row.Cells(1).BackColor = Drawing.Color.Red
                End If
    une aide?
    Merci par avance.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    Bonjour,
    voilà la solution pour tester si un champ d'une colonne est nul:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    If (e.Row.Cells(1).Text.Replace(" ", "99")) = "99" Then
                    e.Row.Cells(1).BackColor = Drawing.Color.Red
                End If
    et Voilà mon code final de la page aspx:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <asp:FileUpload ID="FileUpload2" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Charger"
                 />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Afficher les noms des colonnes?" />
     
    <asp:RadioButtonList ID="rbHDR" runat="server">
        <asp:ListItem Text ="Oui" Value ="Yes" Selected ="True" >
        </asp:ListItem>
        <asp:ListItem Text = "Non" Value = "No"></asp:ListItem>
    </asp:RadioButtonList>
     
    <asp:GridView ID="GridViewXX" runat="server" CssClass="grid" PageSize="15" OnPageIndexChanging ="PageIndexChanging" AllowPaging ="true">
    </asp:GridView>
    et du code vb:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
     Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
            If FileUpload2.HasFile Then
                Dim FileName As String = Path.GetFileName(FileUpload2.PostedFile.FileName)
                Dim Extension As String = Path.GetExtension(FileUpload2.PostedFile.FileName)
                Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
                Dim FilePath As String = Server.MapPath(FolderPath + FileName)
                FileUpload2.SaveAs(FilePath)
                Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
            End If
        End Sub
     
        Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
            Dim conStr As String = ""
            Select Case Extension
                Case ".xls"
                    'Excel 97-03
                    conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString()
                    Exit Select
                Case ".xlsx"
                    'Excel 07
                    conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString()
                    Exit Select
            End Select
            conStr = String.Format(conStr, FilePath, isHDR)
            Dim connExcel As New OleDbConnection(conStr)
            Dim cmdExcel As New OleDbCommand()
            Dim oda As New OleDbDataAdapter()
            Dim dt As New DataTable()
            cmdExcel.Connection = connExcel
            'Get the name of First Sheet
            connExcel.Open()
            Dim dtExcelSchema As DataTable
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            connExcel.Close()
            'Read Data from First Sheet
            connExcel.Open()
            cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
            oda.SelectCommand = cmdExcel
            oda.Fill(dt)
            connExcel.Close()
            'Bind Data to GridView
            GridViewXX.Caption = Path.GetFileName(FilePath)
            GridViewXX.DataSource = dt
     
            GridViewXX.DataBind()
        End Sub
     
        Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewXX.PageIndexChanging
            Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
            Dim FileName As String = GridViewXX.Caption
            Dim Extension As String = Path.GetExtension(FileName)
            Dim FilePath As String = Server.MapPath(FolderPath + FileName)
            Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
            GridViewXX.PageIndex = e.NewPageIndex
            GridViewXX.DataBind()
        End Sub
     
        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewXX.RowDataBound
            If e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(0).BackColor = Drawing.Color.White
            End If
            If e.Row.RowType = DataControlRowType.DataRow Then
                If Not IsDate(e.Row.Cells(2).Text) Then
                    e.Row.Cells(2).BackColor = Drawing.Color.Red
                End If
     
                'asp/gridview  convert Nulls to &nbsp;
                If (e.Row.Cells(0).Text.Replace("&nbsp;", "99")) = "99" Then
                    e.Row.Cells(0).BackColor = Drawing.Color.Red
                End If
                If (e.Row.Cells(1).Text.Replace("&nbsp;", "99")) = "99" Then
                    e.Row.Cells(1).BackColor = Drawing.Color.Red
                End If
                If (e.Row.Cells(2).Text.Replace("&nbsp;", "99")) = "99" Then
                    e.Row.Cells(2).BackColor = Drawing.Color.Red
                End If
     
            End If
     
        End Sub
    et le Web.Config:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <connectionStrings>
    <add name ="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
    <add name ="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
    </connectionStrings>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <appSettings>
      <add key ="FolderPath" value ="Files/"/>
    </appSettings >
    et je crée un dossier Files dans le même dossier qui rassemble ma page aspx.

    Merci :-)
    pacifiquement

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    Bonjour,

    une question,
    comment puis je référencer les lignes pour pouvoir comparer si j'ai un champ en double?

    Merci par avance.

    Edit
    j'ai mis ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    index = 0
                While index < GridViewXX.Rows.Count
                    GridViewXX.Rows(index).Cells(0).BackColor = Drawing.Color.Azure
                    index = index + 1
                End While
    ça marche sauf que la dernière ligne n'est pas prise en compte
    comment faire ?

    merci

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 58
    Points
    58
    Par défaut
    Bonjour,
    Encore moi :-)
    j'essaie de comparer les lignes d'une colonne, pour ceci j'ai mis:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Dim i As Integer = 1
     While i < GridViewXX.Rows.Count
                    If GridViewXX.Rows(0).Cells(0).Text = GridViewXX.Rows(i).Cells(0).Text Then
                        GridViewXX.Rows(0).Cells(0).BackColor = Drawing.Color.Green
                        GridViewXX.Rows(i).Cells(0).BackColor = Drawing.Color.Green
                    End If
                    i = i + 1
                    'index = index + 1
                End While
    mais ceci n'est pas pris en comptes ni pour la dernière ligne ni pour les autres pages de mon gridview.
    c'est comme si la vérification se fait seulement sur la première page.
    sachant que tout cela je le mets à l'intérieur d'un :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Protected Sub GridViewXX_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewXX.RowDataBound
         End Sub
    une aide ? et merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Afficher des données d'excel dans des txtbox
    Par drutarus dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 17/03/2013, 17h31
  2. Réponses: 10
    Dernier message: 06/06/2007, 18h19
  3. importer des données d'excel dans la base de données
    Par Cifrine dans le forum VBA Access
    Réponses: 2
    Dernier message: 01/06/2007, 14h48
  4. [VBA-E]Transfere des données d'Excel dans une table Access
    Par flo83 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 07/04/2006, 09h22
  5. Importer des données de Excel dans pages .ASP
    Par sperron dans le forum ASP
    Réponses: 8
    Dernier message: 24/03/2006, 16h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo