Namespace ExportDatatableToExcel
Public Class _Default
Inherits System.Web.UI.Page
#Region "Export Excel"
Private FunctionInserHeaderInExcel() As String
Dim sExportToExcel AsString = ""
sExportToExcel &= "<table border='0' cellpadding='0' cellspacing='0' width='99%' align='center'>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td colspan='3' style='font-size:14pt;'><b>THỦ THUẬT LẬP TRÌNH</b></td>"& vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td colspan='3'><b>Website:</b> http://thuthuatlaptrinh.blogspot.com</td>" & vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td colspan='3'><b>Email:</b> kenhphanmemviet@gmail.com</td>" & vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "</table>"& vbCrLf
Return sExportToExcel
End Function
Private FunctionExportAccountToExcel(ByVal objBind As DataTable)
Dim sExportToExcel AsString = ""
Dim sActivitiesNumber AsString = ""
Dim sParentName As String = ""
Dim ListInfoContact AsString = ""
sExportToExcel &= "<div id='div1'>" & vbCrLf
'=============Insert Header============
sExportToExcel &= InserHeaderInExcel()
'======================================
sExportToExcel &= "<table border='0' cellpadding='0' cellspacing='0' width='99%' align='center'>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td colspan='9'> </td>" & vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td align='center' colspan='9' style='font-size:12pt;'><b>LIST ACCOUNT</b></td>" & vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td colspan='9'> </td>" & vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
sExportToExcel &= "</table>"& vbCrLf
sExportToExcel &= "<table border='1' cellpadding='0' cellspacing='0' width='99%' align='center'>"& vbCrLf
sExportToExcel &= "<tr>"& vbCrLf
sExportToExcel &= "<td align='center'><b>AccountCode</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>AccountName</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Phone</b></td>"& vbCrLf
sExportToExcel &= "<td align='center'><b>FAX</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Email</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Website</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Description</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Created Date</b></td>" & vbCrLf
sExportToExcel &= "<td align='center'><b>Modified Date</b></td>"& vbCrLf
sExportToExcel &= "</tr>"& vbCrLf
'End Header
For Each row As DataRow In objBind.Rows
If Notrow Is Nothing Then
sExportToExcel &= "<tr>"& vbCrLf
If NotIsDBNull(row("AccountCode")) Then
sExportToExcel &= "<td>" & row("AccountCode").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
IfNot IsDBNull(row("AccName")) Then
sExportToExcel &= "<td>" & row("AccName").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("AccPhone")) Then
sExportToExcel &= "<td>" & row("AccPhone").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
EndIf
If NotIsDBNull(row("AccFAX")) Then
sExportToExcel &= "<td>" & row("AccFAX").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("AccEmail")) Then
sExportToExcel &= "<td>" & row("AccEmail").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("AccWebsite")) Then
sExportToExcel &= "<td>" & row("AccWebsite").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("AccDesc")) Then
sExportToExcel &= "<td>" & row("AccDesc").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("CreatedDate")) Then
sExportToExcel &= "<td>" & row("CreatedDate").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
If NotIsDBNull(row("ModifiedDate")) Then
sExportToExcel &= "<td>" & row("ModifiedDate").ToString & "</td>" & vbCrLf
Else
sExportToExcel &= "<td></td>" & vbCrLf
End If
sExportToExcel &= "</tr>"& vbCrLf
End If
Next
sExportToExcel &= "</table></div>"& vbCrLf
Return sExportToExcel
End Function
Private SubExportToExcel(ByVal FileName As String)
Dim objBind As New DataTable
Dim sb As New System.Text.StringBuilder
Dim sExportToExcel AsString = ""
Dim style As String = "<style>.text{mso-number-format:\@;}</style>"
objBind = BindData()
If objBind.Rows.Count > 0 Then
sExportToExcel = ExportAccountToExcel(objBind)
sb.Append(sExportToExcel)
lblExport.Text = sb.ToString()
sb.Remove(0, sb.Length)
Response.Clear()
Response.Charset = ""
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/ms-excel.xls"
Response.AddHeader("content-disposition", "attachment;filename=" & FileName & ".xls")
Response.ContentEncoding = Encoding.Unicode
Response.BinaryWrite(Encoding.Unicode.GetPreamble())
Dim sw AsNew System.IO.StringWriter
Dim htw AsNew HtmlTextWriter(sw)
lblExport.RenderControl(htw)
Response.Output.Write(style & sw.ToString().Replace("td", "td class='text'"))
Response.Flush()
Response.End()
End If
End Sub
#End Region
#Region "Bind Data"
Private SubBindAccount()
Dim objBind As New DataTable
objBind = BindData()
If Not objBind Is Nothing Then
If objBind.Rows.Count > 0 Then
grvObject.DataSource = objBind
grvObject.DataBind()
trMessage.Visible = False
grvObject.Visible = True
Else
trMessage.Visible = True
grvObject.Visible = False
End If
updatePanel.Update()
End If
End Sub
Private FunctionBindData() As DataTable
Dim objSQL As New SqlDataProvider
Dim objBind As DataTable = objSQL.FillTable("Pro_Accounts_List", New ObjectPara("@Keyword", txtSearch.Text.Trim), _
New ObjectPara("@SortField", "CreatedDate"), _
New ObjectPara("@SortType", "DESC"))
Return objBind
End Function
#End Region
#Region "GridView Methods"
Private SubgrvObject_RowCommand(ByVal sender As Object, ByVal e AsSystem.Web.UI.WebControls.GridViewCommandEventArgs) Handles grvObject.RowCommand
Dim ItemID As Integer = Integer.Parse(e.CommandArgument)
Select Casee.CommandName.ToLower
Case "view"
With CType(ucViewRecord, ExportDatatableToExcel.UserControls.Popup_ViewRecord)
.ItemID = ItemID
.ShowPopup(ItemID)
End With
End Select
End Sub
Private SubgrvObject_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) HandlesgrvObject.RowDeleting
Dim ItemID As Integer = CType(grvObject.DataKeys(e.RowIndex).Value, Integer)
Dim ItemName As String = ""
If ItemID <> -1 Then
With CType(ucDeleteItem, ExportDatatableToExcel.UserControls.Popup_ConfirmDelete)
.ItemID = ItemID
.ShowPopup(ItemID, "")
End With
End If
End Sub
Private SubgrvObject_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) HandlesgrvObject.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
'Delete
Dim cmdDelete AsImageButton = DirectCast(e.Row.FindControl("cmdDelete"), ImageButton)
If NotcmdDelete Is NothingThen
cmdDelete.ToolTip = "Delete Account"
End If
End If
End Sub
Private SubgrvObject_PageIndexChanging(ByVal sender As Object, ByVal e AsSystem.Web.UI.WebControls.GridViewPageEventArgs) Handles grvObject.PageIndexChanging
grvObject.PageIndex = e.NewPageIndex
BindAccount()
End Sub
#End Region
#Region "Popup"
Private SubMySelDelete_OnSelectedRow(ByVal sender As Object, ByVal e AsExportDatatableToExcel.MyEventArgs)
Dim ItemName As String = ""
With e
If e.Id <> "" Then
BindAccount()
End If
End With
End Sub
#End Region
#Region "Event Handles"
Protected SubPage_Load(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Me.Load
Try
AddHandler CType(ucDeleteItem, ExportDatatableToExcel.UserControls.Popup_ConfirmDelete).OnSelectedRow, AddressOf MySelDelete_OnSelectedRow
If Page.IsPostBack = False Then
'Default Submit Button
Page.Form.DefaultButton = cmdQuickSearch.UniqueID
BindAccount()
End If
Catch ex As Exception
End Try
End Sub
Private SubcmdQuickSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlescmdQuickSearch.Click
BindAccount()
End Sub
Private SubcmdExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlescmdExport.Click
ExportToExcel("List-Account.xls")
End Sub
#End Region
End Class
End Namespace