Tuesday, June 24, 2008

Export to Excel

Click event of button to export contact to Excel or spreadsheet.

Protected Sub excelvCard_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles excelvCard.Click
Try
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim ml_directoryPath As String = Server.MapPath("~/DesktopModules/Employee/ExportEmployee/")
If Directory.Exists(ml_directoryPath) Then
Directory.Delete(ml_directoryPath, True)
End If
Directory.CreateDirectory(ml_directoryPath)
ExportContactsToExcel(ml_directoryPath)
Response.Clear()
Response.ContentType = "application/Excel"
Response.AppendHeader("content-disposition", "attachment;filename=Contacts.csv")
Response.TransmitFile((ml_directoryPath + "Employees.csv"))
Response.Flush()
Response.End()
Catch ex As Exception
End Try
End Sub

////Function for Export contact to Excel///

Public Shared Function ExportContactsToExcel(ByVal destinationFilepath As String) As String
Dim emplist As New ArrayList
Dim ExportList As New List(Of Modules.Employee.EmployeeInfo)
ExportList = Modules.Employee.EmployeeController.GetAllEmployeeForExport()
Dim ml_filePath As String = destinationFilepath
ml_filePath = (ml_filePath + "Employees.csv")
Dim o_fsCsvFile As FileStream = New FileStream(ml_filePath, FileMode.Create)
Dim o_swCsvFile As StreamWriter = New StreamWriter(o_fsCsvFile)
o_swCsvFile.WriteLine("FirstName,LastName,DateOfBirth,Parmanent Address,Local Address,Home Phone,Cell Phone,Emer. Contact Name,Emer. Contact Relation,Emer.Contact Home Phone,Emer.Contact Cell Phone,Physically Disabled,Blood Group,Manager Name,Current Designation,Pan Number,Passport Number,Hire Date,Termination Date")
Dim o_sbContact As System.Text.StringBuilder = New System.Text.StringBuilder
For Each exportEmp As EmployeeInfo In ExportList
o_sbContact.Append((exportEmp.Name + ","))
o_sbContact.Append((exportEmp.LastName + ","))
o_sbContact.Append((exportEmp.DateOfBirth + ","))
o_sbContact.Append((exportEmp.PermanentAddress + ","))
o_sbContact.Append((exportEmp.LocalAddress + ","))
o_sbContact.Append((exportEmp.PhoneNumber + ","))
o_sbContact.Append((exportEmp.MobileNumber + ","))
o_sbContact.Append((exportEmp.EmgContactName + ","))
o_sbContact.Append((exportEmp.Email + ","))
o_sbContact.Append((exportEmp.Relation + ","))
o_sbContact.Append((exportEmp.Emg_ContactPhoneNumber + ","))
o_sbContact.Append((exportEmp.Emg_ContactMobileNumber + ","))
'o_sbContact.Append((exportEmp.Cast.ToString() + ","))
o_sbContact.Append((exportEmp.PhysicallyDisabled + ","))
o_sbContact.Append((exportEmp.BloodGroup + ","))
o_sbContact.Append((exportEmp.ManagerName + ","))
o_sbContact.Append((exportEmp.CurrentPosition + ","))
o_sbContact.Append((exportEmp.PanNumber + ","))
o_sbContact.Append((exportEmp.PassPortNumber + ","))
o_sbContact.Append((exportEmp.HireDate + ","))
o_sbContact.Append((exportEmp.TerminationDate + ","))
o_swCsvFile.WriteLine(o_sbContact.ToString())
o_sbContact.Remove(0, o_sbContact.Length)
Next
o_swCsvFile.Close()
Return ml_filePath
End Function

No comments: