TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Naidu AMMAN
NA
37
35.3k
code for export datagrid to pdf
Jul 31 2014 3:13 AM
hi i write code for export data grid to excel its working successfully and how to write for export data grid to PDF in same format. Please help me as soon as possible.
---------------------------------------------------------------------------------------------
Public Class frmStillPendingVehicles
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
Dim strsql As String = ""
strsql = "select trasactionID,VehicleNumber,VehicleType,format(INDATE,'dd/MM/yyyy') as VehicleINDATE ,format(INDATE,'HH:mm:ss') as VehicleINTIME from tblcollection where OUTDATE is NULL" ' and INDATE>=#" & DtFrom.Value.Date & "# and INDATE<#" & DTto.Value.AddDays(1).Date & "# "
Dim m_ds As New DataSet
Dim m_db As New CDataAccess
m_ds = m_db.getdata(strsql, strconnection)
If Not m_ds Is Nothing Then
DataGridView1.DataSource = m_ds.Tables(0)
Else
MsgBox("No Records found", MsgBoxStyle.Information)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim m_excel As String = ""
If SfdlgDownload.ShowDialog() = Windows.Forms.DialogResult.OK Then
ExportDataToExcel(SfdlgDownload.FileName, DataGridView1)
End If
End Sub
Public Sub ExportDataToExcel(ByVal FilePath As String, ByVal DataGrid As DataGridView)
Dim _mFileStream As New IO.StreamWriter(FilePath, False)
Try
_mFileStream.WriteLine("<?xml version=""1.0""?>")
_mFileStream.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
_mFileStream.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
_mFileStream.WriteLine(" <ss:Styles>")
_mFileStream.WriteLine(" <ss:Style ss:ID=""1"">")
_mFileStream.WriteLine(" <ss:Font ss:Bold=""1""/>")
_mFileStream.WriteLine(" <ss:FontName=""Courier New""/>")
_mFileStream.WriteLine(" </ss:Style>")
_mFileStream.WriteLine(" </ss:Styles>")
_mFileStream.WriteLine(" <ss:Worksheet ss:Name=""Sheet1"">")
_mFileStream.WriteLine(" <ss:Table>")
For x As Integer = 0 To DataGrid.Columns.Count - 1
_mFileStream.WriteLine(" <ss:Column ss:Width=""{0}""/>", DataGrid.Columns.Item(x).Width)
Next
_mFileStream.WriteLine(" <ss:Row ss:StyleID=""1"">")
For i As Integer = 0 To DataGrid.Columns.Count - 1
_mFileStream.WriteLine(" <ss:Cell>")
_mFileStream.WriteLine(String.Format(" <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Columns.Item(i).HeaderText))
_mFileStream.WriteLine("</ss:Cell>")
Next
_mFileStream.WriteLine(" </ss:Row>")
For intRow As Integer = 0 To DataGrid.RowCount - 2
_mFileStream.WriteLine(String.Format(" <ss:Row ss:Height =""{0}"">", DataGrid.Rows(intRow).Height))
For intCol As Integer = 0 To DataGrid.Columns.Count - 1
_mFileStream.WriteLine(" <ss:Cell>")
_mFileStream.WriteLine(String.Format(" <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Item(intCol, intRow).Value.ToString))
_mFileStream.WriteLine(" </ss:Cell>")
Next
_mFileStream.WriteLine(" </ss:Row>")
Next
_mFileStream.WriteLine(" </ss:Table>")
_mFileStream.WriteLine(" </ss:Worksheet>")
_mFileStream.WriteLine("</ss:Workbook>")
_mFileStream.Close()
_mFileStream.Dispose()
_mFileStream = Nothing
Catch ex As Exception
_mFileStream.Close()
_mFileStream.Dispose()
_mFileStream = Nothing
MessageBox.Show("Error While Exporting Data To Excel. Error : " & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
----------------------------------------------------------------------------------------
Best Regards
Gyana sumanth gondu
Reply
Answers (
3
)
How to delete selected rows in datagrid view using button
equivalent of push from javascript to c#