this is my excel upload using vb.net
you can use a convertor to conver it to c# if you are unsure of how to do it
<asp:Panel ID="PanelUpload" runat="server" Visibile="False">
<asp:FileUpload ID="FileUploadExcel" runat="server" />
<br />
Please select an Excel File to upload<br />
<asp:Label ID="LabelUpload" runat="server" Text="">
</asp:Label>
</asp:Panel>
code behind...
Protected Sub ButtonUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ButtonUploadFile.Click
PanelUpload.Visible = True
btnNo.Visible = False
btnYes.Visible = False
btnSave.Visible = False
'check to see if the SiteTemplate file already exists. if so delete it
If FileUploadExcel.HasFile Then
Try
'alter path for your project
FileUploadExcel.SaveAs(Server.MapPath("~/SiteTemplate.xls"))
FileUploadExcel.Dispose()
LabelUpload.Text = "Upload File Name: " & _
FileUploadExcel.PostedFile.FileName & "<br>" & _
"Type: " & _
FileUploadExcel.PostedFile.ContentType & _
" File Size: " & _
FileUploadExcel.PostedFile.ContentLength & " kb<br>"
ButtonUploadFile.Enabled = False
ButtonView.Enabled = True
'save the file to a different location with the time and date on the end of the file at upload
Dim sFileDir As String = "C:\xxxx\xxxx\My Documents\"
Dim sFileName As String = System.IO.Path.GetFileName(FileUploadExcel.PostedFile.FileName)
Dim TLDateTime As String
Dim TLDay As String
Dim TLMonth As Integer
Dim TLYear As Integer
Dim TLHour As Integer
Dim TLMinute As Integer
Dim TLDate As String
Dim TLTime As String
TLDay = DateTime.Now.Day
TLMonth = DateTime.Now.Month
TLYear = DateTime.Now.Year
TLHour = DateTime.Now.Hour
TLMinute = DateTime.Now.Minute
TLDate = TLDay.ToString + "-" + TLMonth.ToString + "-" + TLYear.ToString
TLTime = TLHour.ToString + "-" + TLMinute.ToString
TLDateTime = TLDate + "_" + TLTime
'delete the extension on the file name so you do not have two "." in your file
sFileName = Left([sFileName], InStrRev([sFileName], ".") - 1)
FileUploadExcel.PostedFile.SaveAs(sFileDir + sFileName + "_" + TLDateTime + ".xls")
FileUploadExcel.Dispose()
Catch ex As Exception
LabelUpload.Text = "Error: " & ex.Message.ToString
End Try
Else
LabelUpload.Text = "Please select a file to upload."
End If
End Sub
the next bit is for uploading a pdf file but you can alter for excel
If FileUpload1.HasFile = False And FileUpload2.HasFile = False Then
Label1.Text = "Please select file to upload"
Else
If FileUpload1.HasFile Then
If (FileUpload1.PostedFile.ContentLength < 3145728) Then
If Not Request.QueryString("siteDataID") Is Nothing Then
Dim fileLength As Integer = FileUpload1.PostedFile.ContentLength
Dim bytfile(fileLength) As Byte
FileUpload1.PostedFile.InputStream.Read(bytfile, 0, fileLength)
Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionsstring").ConnectionString)
Try
connection.Open()
Dim cmd As New SqlCommand("UPDATE tblxxx " & _
" SET [xxxPDF1]=@file, PDF1Name=@filename" & _
" WHERE xxxdatid=@xxxdataid", connection)
cmd.Parameters.AddWithValue("@file", bytfile)
cmd.Parameters.AddWithValue("@xxxdataID", Request.QueryString("siteDataID"))
cmd.Parameters.AddWithValue("@filename", FileUpload1.PostedFile.FileName)
cmd.ExecuteNonQuery()
Label1.Text = FileUpload1.PostedFile.FileName + " : Uploaded Successfully"
Catch ex As Exception
Label1.Text = "File Not Uploaded" & ex.ToString
Finally
connection.Close()
End Try
End If
Else
Label3.Visible = True
Label3.Text = "ERROR: File " & FileUpload1.PostedFile.FileName & " not uploaded. Size must not exceed 3MB."
End If
End If