### Export TableView to Microsoft Excel

To export the content of a TableView to Microsoft Excel, use the jExcel Library.
Various Export Examples from a TableView.
Private tvExcel As TableView
'Export tableview to excel in simpleformat
`Sub ExportTableViewToExcel1(tv As TableView, folder As String, filename As String)
Log("exportTableViewToExcel1")
Dim FWorkbook1 As WritableWorkbook
FWorkbook1.Initialize(folder, filename)
Dim FSheet1 As WritableSheet
FSheet1 = FWorkbook1.AddSheet("Sheet1", 0)
For i = 0 To tv.Items.Size - 1
tv.SelectedRow = i
Dim Row() As Object = tv.SelectedRowValues
For j = 0 To Row.Length - 1
Dim cell As WritableCell
cell.InitializeText(i, j, Row(j))
'cell.InitializeNumber(i, j, Row(j))
FSheet1.AddCell(cell)
Next
Next
'Must call write and close to save the data.
FWorkbook1.Write
FWorkbook1.Close
lblStatusMessage.Text = "Tableview exported to Excel " & filename
End Sub`
' Export tabelview to excel in simpleformat
`Sub exportTableViewToExcel2(tv As TableView, folder As String, filename As String)
Log("exportTableViewToExcel2")
Dim FWorkbook1 As WritableWorkbook
FWorkbook1.Initialize(folder, filename)
FSheet1 = FWorkbook1.AddSheet("Sheet1", 0)
'add the headers to the sheet
'we create a special format for the headers
Dim cellFormat As WritableCellFormat
cellFormat.Initialize2(cellFormat.FONT_ARIAL, 12, True, False, False, cellFormat.COLOR_GREEN)
cellFormat.HorizontalAlignment = cellFormat.HALIGN_CENTRE
cellFormat.SetBorder(cellFormat.BORDER_ALL, cellFormat.BORDER_STYLE_MEDIUM, cellFormat.COLOR_BLACK)
cellFormat.SetBorder(cellFormat.BORDER_BOTTOM, cellFormat.BORDER_STYLE_THICK, cellFormat.COLOR_BLUE)
cellFormat.VertivalAlignment = cellFormat.VALIGN_CENTRE
cellFormat.BackgroundColor = cellFormat.COLOR_GREY_25_PERCENT
For col = 0 To tv.ColumnsCount - 1
Dim cell As WritableCell
cell.InitializeText(col, 0, tv.GetColumnHeader(col))
Log(tv.GetColumnHeader(col))
cell.SetCellFormat(cellFormat)
FSheet1.AddCell(cell)
FSheet1.SetColumnWidth(col, 15)
Next
' FSheet1.SetColumnWidth(1, 40)
' FSheet1.SetRowHeight(0, 15)
'add the data
Dim rowsFormat As WritableCellFormat
rowsFormat.Initialize
rowsFormat.HorizontalAlignment = rowsFormat.HALIGN_CENTRE
For i = 0 To tv.Items.Size - 1
tv.SelectedRow = i
Dim Row() As Object = tv.SelectedRowValues
For j = 0 To Row.Length - 1
Dim cell As WritableCell
If IsNumber(Row(j)) Then
cell.InitializeNumber(j, (i+1), Row(j))
Else
cell.InitializeText(j, (i+1), Row(j))
End If
cell.SetCellFormat(rowsFormat)
FSheet1.AddCell(cell)
Next
Next
'Must call write and close to save the data.
FWorkbook1.Write
FWorkbook1.Close
lblStatusMessage.Text = "Tableview exported to Excel " & filename
End Sub`
' Export tableview to excel in simpleformat. Uses the jExcel Library
' Parameter Tableview
' Parameter Excel Filename
' Parameter Tabsheet name
`Sub exportTableViewToExcel3(tv As TableView, folder As String, filename As String)
Log("exportTableViewToExcel3")
Dim wb As WritableWorkbook
Dim ws As WritableSheet
wb.Initialize(folder, filename)
ws = wb.AddSheet("Sheet1", 0)
For i = 0 To tv.Items.Size - 1
tv.SelectedRow = i
Dim Row() As Object = tv.SelectedRowValues
For j = 0 To Row.Length - 1
Dim cell As WritableCell
If IsNumber(Row(j)) Then
cell.InitializeNumber(i, j, Row(j))
Else
cell.InitializeText(i, j, Row(j))
End If
ws.AddCell(cell)
Next
Next
'Must call write and close to save the data.
wb.Write
wb.Close
lblStatusMessage.Text = "Tableview exported to Excel " & filename
End Sub`
' Export tableview to csv format. Uses jStringUtils Library.
' Parameter Tableview
' Parameter CSV Filename
`Sub exportTableViewToCSV1(tv As TableView, filename As String)
Dim su As StringUtils
Dim l As List
l.Initialize
For y = 0 To tv.Items.Size - 1
tv.SelectedRow = y
Dim Row() As Object = tv.SelectedRowValues
Dim NRow(Row.Length) As String
For x = 0 To Row.Length - 1
NRow(x) = Row(x)
l.Add(NRow)
Next
Next
Try
su.SaveCSV(File.DirApp, filename, ",", l)
lblStatusMessage.Text = "Tableview exported to CSV " & filename
Catch
Log(LastException.Message)
End Try
End Sub`
' Export tableview to csv format with column header. Uses jStringUtils Library.
' Parameter Tableview
' Parameter CSV Filename
`Sub exportTableViewToCSV2(tv As TableView, filename As String)
Dim su As StringUtils
Dim l As List
l.Initialize
For y = 0 To tv.Items.Size - 1
tv.SelectedRow = y
Dim Row() As Object = tv.SelectedRowValues
Dim NRow(Row.Length) As String
For x = 0 To Row.Length - 1
NRow(x) = Row(x)
l.Add(NRow)
Next
Next
Dim ch As List
ch.Initialize
For i = 0 To tv.ColumnsCount - 1
ch.Add(tv.GetColumnHeader(i))
Next
Try
su.SaveCSV2(File.DirApp, filename, ",", l, ch)
lblStatusMessage.Text = "Tableview exported to CSV " & filename
Catch
Log(LastException.Message)
End Try
End Sub`

Changed: , Category: TableView, File: T6C259M1883.htm, ID: 1883