Wednesday, June 29, 2011

Generate a Excel file with multiple sheets (Visual Basic)

Dim xlApp As Excel.Application = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkSheet As Excel.Worksheet
Dim xlWorkSheets As Excel.Worksheets = Nothing
Dim xlRange As Excel.Range = Nothing
Dim FileSend As String
Dim str As New Bitacora.Subject
Dim Msg As String

Try
'Creating a new object of the excel application object
xlApp = New Excel.Application
'Hiding the excel application
xlApp.Visible = False
'Hiding all the alert messages occurring during the process
xlApp.DisplayAlerts = False

'Adding a collection of workbooks to the excel object
xlWorkBook = CType(xlApp.Workbooks.Add(), Excel.Workbook)
'Saving the workbook as a normal workbook format
xlWorkBook.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal)

'Gettin the path
FileSend = xlWorkBook.Path & "\" & FileName
'Getting the collection of workbooks in an object
xlWorkBooks = xlApp.Workbooks
'Get the reference to the first sheet in the workbook collection in a variable
xlWorkSheet = CType(xlWorkBooks(1).Worksheets.Item(1), Excel.Worksheet)

'The name of the worksheet
xlWorkSheet.Name = "Pendientes"

'Get the cells collection of the sheet in a variable to write the data
xlRange = xlWorkSheet.Cells
'Calling the function to write the datatable data in the cells of the first sheet
dt = fun.PendientesGenerados(datos.nAgente, datos.nAgenteBck, Fecha)
str.PGenerados = dt.Rows.Count
WriteData(dt, xlRange)

'Setting the width of the specified range of cells so as to absolutely fit the written data
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'2nd Sheet
xlWorkSheet = xlWorkBook.Worksheets.Add
xlWorkSheet.Name = "TicketsActualizados"
xlRange = xlWorkSheet.Cells
dt = fun.TicketsActualizados(datos.nAgente, datos.nAgenteBck, Fecha)
str.TModificados = dt.Rows.Count
WriteData(dt, xlRange)
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'3rd Sheet
xlWorkSheet = xlWorkBook.Worksheets.Add
xlWorkSheet.Name = "TicketsRecibidos"
xlRange = xlWorkSheet.Cells
dt = fun.TicketsRecibidos(datos.nAgente, datos.nAgenteBck, Fecha)
str.TRecibidos = dt.Rows.Count
WriteData(dt, xlRange)
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'4th Sheet
xlWorkSheet = xlWorkBook.Worksheets.Add
xlWorkSheet.Name = "TicketsGenerados"
xlRange = xlWorkSheet.Cells
dt = fun.TicketsGenerados(datos.nAgente, datos.nAgenteBck, Fecha)
str.TGenerados = dt.Rows.Count
WriteData(dt, xlRange)
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'5th Sheet
xlWorkSheet = xlWorkBook.Worksheets.Add
xlWorkSheet.Name = "TransaccionesIngresadas"
xlRange = xlWorkSheet.Cells
dt = fun.TransaccionesIngresadas(datos.nAgente, datos.nAgenteBck, Fecha)
str.TIngresadas = dt.Rows.Count
WriteData(dt, xlRange)
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'6th Sheet
xlWorkSheet = xlWorkBook.Worksheets.Add
xlWorkSheet.Name = "ClientesContactados"
xlRange = xlWorkSheet.Cells
dt = fun.ClientesContactados(datos.nAgente, datos.nAgenteBck, Fecha)
str.CContactados = dt.Rows.Count
WriteData(dt, xlRange)
xlWorkSheet.Range("A1", "Z1").EntireColumn.AutoFit()

'Delete Sheets
For Each sheet As Excel.Worksheet In xlWorkBook.Sheets
If sheet.Name = "Sheet2" Or sheet.Name = "Sheet3" Then
sheet.Delete()
End If

Next
xlWorkSheet.SaveAs(FileName)
xlWorkBook.Close()
xlWorkBook = Nothing

Show a ToolTip on a Control

ToolTip buttonToolTip = new ToolTip();
//Title of tooltip window
buttonToolTip.ToolTipTitle = "Ivan Osorio De anda";
//Represents weather a fade effect should be used when displaying the tooltip
buttonToolTip.UseFading = true;
buttonToolTip.UseAnimation = true;
//Gets or sets a value indicating whether the ToolTip should use a balloon window
buttonToolTip.IsBalloon = true;
buttonToolTip.ToolTipIcon = ToolTipIcon.Info;
//Displays if tooltip is displayed even the parent control is not active.
buttonToolTip.ShowAlways = true;
               
//The period of time the ToolTip remains visible if the pointer is stationary on a control with specified ToolTip text.
buttonToolTip.AutoPopDelay = 5000;
//Gets or sets the time that passes before the ToolTip appears.
buttonToolTip.InitialDelay = 20;
//Gets or sets the length of time that must transpire before subsequent ToolTip windows appear as the pointer moves from one control to another.
buttonToolTip.ReshowDelay = 25;
//call SetToolTip method and pass a control and text
buttonToolTip.SetToolTip(DataGridViewTest, "Message!!");