12/05/2017, 21:43
Vb.net de veritabanı bağlantısı yaptım programda bu veritabanının raporunu ve çıktısını almak istiyorum. Bilen yardım edebilir mi ?
(13/05/2017, 11:19)dumanserdar08 yazdı: [ -> ]Projenizin kod sayfasının kütüphane ekleme alanına Excel ile ilgili aşağıdaki kodları eklemeyi unutmayınız.kodlarını yazabilirmisinizKod:Private Sub Rapor_EXCELE_GÖNDER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rapor_EXCELE_GÖNDER.Click
Try
If DataGridView1.RowCount > 0 Then
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
saveFileDialog1.Title = "Excel Dosya Kaydetme"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
EXCELEGONDER(saveFileDialog1.FileName)
'ToolStripTextBox2.Text = saveFileDialog1.FileName
End If
Else
Exit Sub
End If
Catch ex As OleDbException
MessageBox.Show("ERROR : " & ex.Message.ToString)
End Try
End Sub
Public Sub EXCELEGONDER(ByVal FileName As String)
Dim ExcelApp As Microsoft.Office.Interop.Excel.Application
Dim ExcelBook As Microsoft.Office.Interop.Excel.Workbook
Dim ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet
ExcelApp = New Microsoft.Office.Interop.Excel.Application()
ExcelBook = ExcelApp.Workbooks.Add
ExcelSheet = ExcelApp.Worksheets(1)
'create object of excel
'ExcelApp = CreateObject("Excel.Application")
'ExcelBook = ExcelApp.WorkBooks.Add
'ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelBook
.Sheets(1).Select()
.Sheets(1).Name = "RAPOR"
End With
With ExcelSheet
Dim columnsCount As Integer = DataGridView1.Columns.Count
For Each column In DataGridView1.Columns
.Cells(1, column.Index + 1).Value = column.Name
Next
'Export Header Name End
'Export Each Row Start
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim columnIndex As Integer = 0
Do Until columnIndex = columnsCount
.Cells(i + 2, columnIndex + 1).Value = DataGridView1.Item(columnIndex, i).Value.ToString
columnIndex += 1
ToolStripProgressBar1.Maximum = DataGridView1.Rows.Count
ToolStripProgressBar1.Value = i
If ToolStripProgressBar1.Value >= ToolStripProgressBar1.Maximum Then
ToolStripProgressBar1.Value = 0
End If
Loop
Next
'****************************************************************************************************
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 10
.Cells.Columns.AutoFit()
End With
ExcelApp.Visible = False
'If System.IO.File.Exists(EXCELRAPORUNYERİ) Then
' System.IO.File.Delete(EXCELRAPORUNYERİ)
'End If
ExcelBook.SaveAs(FileName)
ExcelBook.Close(True)
ExcelApp.UserControl = True
ExcelApp.Quit()
MessageBox.Show("Tablo Exele Aktarıldı.", "EXCELE GÖNDER", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Marshal.ReleaseComObject(ExcelSheet)
Marshal.ReleaseComObject(ExcelBook)
Marshal.ReleaseComObject(ExcelApp)
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End Sub