Private Sub DosPrint() Try Dim table As New DataTable Dim i As Integer Dim j As Integer Dim Collist(dgvreport.ColumnCount) As String Dim TotalLen As Integer = 0 Dim LineCount As Integer = 0 Dim PageCount As Integer = 1 Dim StrSeprator As String = "" ''========== Check total len from Each Rows =============== For Each dc As DataGridViewColumn In dgvreport.Columns If dc.Visible = True Then Dim temp As Integer = dc.HeaderText.Length For Each dr As DataGridViewRow In dgvreport.Rows Dim drlen As Integer = dr.Cells(dc.Name).Value.ToString.Length If temp < drlen Then temp = drlen End If Next Collist(dc.Index) = temp TotalLen = TotalLen + temp End If Next Using Writer As New System.IO.StreamWriter("C:\Testfile.txt") ''=================== Write Reprot Header ================ Writer.WriteLine("") Writer.WriteLine(Space(20) + Chr(27) + Chr(71) + Chr(14) + _ Chr(27) + Chr(45) + Chr(1) + "Company Name" + _ Chr(27) + Chr(72) + Chr(20) + _ Chr(27) + Chr(45) + Chr(0)) '' For Bold and Enlarge Writer.WriteLine(lblHeader.Text) Writer.WriteLine("") For k As Integer = 0 To TotalLen Writer.Write("=") Next Dim strCOl As String = "" Dim strRow As String = "" ''============= Write Col Header for First Page ====================================== Dim iC As Integer = 1 Dim iR As Integer = 1 For j = 0 To dgvreport.ColumnCount - 1 If dgvreport.Columns(j).Visible = True Then strCOl = strCOl + " " + dgvreport.Columns(j).HeaderText.ToString() + Space(CInt(Collist(j)) - CInt(dgvreport.Columns(j).HeaderText.Length)) iC = iC + 1 End If Next Writer.WriteLine("") Writer.WriteLine(Chr(27) + Chr(27) + strCOl + Chr(27) + Chr(72)) For k As Integer = 0 To TotalLen StrSeprator = StrSeprator + "=" Next Writer.WriteLine(StrSeprator) Writer.WriteLine("") ''======== End First Col ======================== For i = 0 To dgvreport.RowCount - 1 If LineCount >= 60 Then '' Check Lines per page LineCount = 0 '' Ending First Page Writer.WriteLine(StrSeprator) Writer.WriteLine("Page: " + PageCount.ToString) ''Write Page Number PageCount = PageCount + 1 Writer.WriteLine("") Writer.Write(Chr(13)) '' Line Break ''============= Write Col Header for Other Page ====================================== Writer.WriteLine("") Writer.WriteLine(StrSeprator) Writer.WriteLine(Chr(27) + Chr(27) + strCOl + Chr(27) + Chr(72)) Writer.WriteLine(StrSeprator) Writer.WriteLine("") ''======== End Other Col header ======================== End If If dgvreport.Rows(i).Visible = True Then iR = iR + 1 iC = 1 For j = 0 To dgvreport.ColumnCount - 1 If dgvreport.Columns(j).Visible = True Then strRow = strRow + " " + dgvreport(j, i).Value.ToString() + Space(CInt(Collist(j)) - CInt(dgvreport(j, i).Value.ToString.Length)) iC = iC + 1 End If Next End If Writer.WriteLine(strRow) strRow = "" LineCount = LineCount + 1 Next Writer.WriteLine(StrSeprator) Writer.Close() End Using Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub
Saturday, July 2, 2011
Dos Print from Datagridview in VB.Net
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment