Export a list View to a file

Wednesday, February 18, 2009

Public Function exportListView(ByVal lvw As ListView, Optional ByVal Mtype As String = "") As System.Text.StringBuilder
Dim str As New System.Text.StringBuilder()
Dim st As String() = New String(lvw.Columns.Count - 1) {}
str.Append(vbTab & vbTab & " Viewer" & vbTab & vbTab & vbTab & vbTab & vbCrLf & vbCrLf & vbCrLf & vbCrLf)
For col As Integer = 0 To lvw.Columns.Count - 1
str.Append(vbTab & lvw.Columns(col).Text.ToString())
Next
Dim rowIndex As Integer = 1
Dim row As Integer = 0
Dim st1 As String = ""
For row = 0 To lvw.Items.Count - 1
If rowIndex <= lvw.Items.Count Then
rowIndex += 3
End If
st1 = vbLf
str.Append(vbLf)
For col As Integer = 0 To lvw.Columns.Count - 1
st1 = (st1 & vbTab) + lvw.Items(row).SubItems(col).Text.ToString()
str.Append(vbTab + lvw.Items(row).SubItems(col).Text.ToString())
Next
Next
If Trim(Mtype) = "" Then
Dim SaveFileDialog As New SaveFileDialog
SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
SaveFileDialog.Filter = "Microsoft Word(*.doc)|*.doc"
If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim FileName As String = SaveFileDialog.FileName
Dim sw As New StreamWriter(FileName, False)
sw.AutoFlush = True
sw.Write(str)
sw.Close()
Dim fil As New FileInfo(FileName)
If fil.Exists = True Then
MessageBox.Show("Process Completed", "Export to Word", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Else
Return (str)
End If

End Function

0 comments

Post a Comment