My environment : VS2010- ASP.net coding - VB.net
Sir, I am looking for a code which can create dynamic Excel file and further send it on a click.
Following is the Code I am trying following code.
Protected Sub excel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles excel.Click
Dim constr As String = ConfigurationManager.ConnectionStrings("ACCOUNTSCONNECTIONSTRING").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT * FROM EXPHEAD")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Using wb As New XLWorkbook()
wb.Worksheets.Add(dt, "PENSION")
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx")
Using MyMemoryStream As New MemoryStream()
wb.SaveAs(MyMemoryStream)
MyMemoryStream.WriteTo(Response.OutputStream)
Dim msg As String
Dim MTHD As String
Using mm As New MailMessage("sender@gmail.com", "receiver@gmail.com") '
mm.Subject = "PENSION - BOSER (AUTOMATED) "
msg = "P F A "
'+ " <br/> <br/> </b> <br/> <br/> " + txtquery.Text + " <br/> <br/> </b> <br/> <br/> " + "Pl. use Bill Filling Portal to reply for objection Clearing " + "http://117.211.8.73:84/center21/default.aspx"
' txtmsg0.Text = msgS
mm.Body = msg
Dim File = "SQLEXPORT.XLSX"
mm.Attachments.Add(New Attachment(File))
'End If
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New NetworkCredential("sender@gmail.com", "ntkdedublnrzkqin") ''
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
ClientScript.RegisterStartupScript(Me.GetType, "alert", "alert('Email sent.');", True)
MTHD = "EMAIL"
'updatedata(MTHD)
End Using
Response.Flush()
Response.End()
End Using
End Using
End Using
End Using
End Using
End Using
End Sub