Question

I'm trying to fill an existing xls file with EPPlus, but i never use it before. I have to search my data in a DB.

After insert data, the user could save the file on his computer, I doesn't know anything about EPPlus so, i made this :

Dim ExistFile = Server.MapPath("~/Vues/tableau_qualif1.xlsx")

Dim File = New FileInfo(ExistFile)
Dim Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Formation_2014ConnectionString").ConnectionString)
Dim i = 3
Dim Query = "SELECT * FROM personnes"

Using package As New ExcelPackage(File)
    package.Load(New FileStream(ExistFile, FileMode.Open))

    Dim workSheet As ExcelWorksheet = package.Workbook.Worksheets("Feuil1")

    Try
        'Ouverture de la connexion
        Connection.Open()
        'Définition de la commande et de ses paramètres
        Dim Commande As New SqlCommand(Query, Connection)

        'Création du SqlDataAdapter et du DataSet (En fonction de la Commande)
        Dim Adaptateur As New SqlDataAdapter(Commande)
        Dim MonDataSet As New DataSet
        Try
            'Définition de l'adaptateur
            Adaptateur.Fill(MonDataSet, "Personnes")

            For Each Ligne As DataRow In MonDataSet.Tables("Personnes").Rows()
                workSheet.Cells("A" & i).Value = Ligne("Prenom_personne").ToString() & " " & Ligne("Nom_personne").ToString()
                i = i + 1
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    'Fermeture de la connexion
    Connection.Close()

    package.Save()


    Response.Clear()
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

    Response.BinaryWrite(package.GetAsByteArray())

    Response.End()

End Using

This code crash at

Response.BinaryWrite(package.GetAsByteArray())

Anyone could help me with this ? Thanks a lot !

Was it helpful?

Solution

My line

package.save()

cause my issues, I just remove this line and all work fine !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top