ik plaats de gegevens van een Datagridview in een excel sheet
hoe kan ik nu bepalen in welke sheet het geplaatst wordt
en hoe wijzig in de naam van de sheet
ik stuur de gegevens
Public Function GridAExcel(ByVal grd As DataGridView)
ProgressBar1.Visible = True
Dim progres As Integer = 0
Dim x As Integer
Dim fichero As New SaveFileDialog()
fichero.Filter = "Excel (*.xls)|*.xls"
Dim aplicacion As Microsoft.Office.Interop.Excel.Application
Dim libros_trabajo As Microsoft.Office.Interop.Excel.Workbook
Dim hoja_trabajo As Microsoft.Office.Interop.Excel.Worksheet
aplicacion = New Microsoft.Office.Interop.Excel.Application()
libros_trabajo = aplicacion.Workbooks.Add()
Dim Visibles As Integer = 0
For x = 0 To grd.Columns.Count - 1
If grd.Columns(x).Visible = True Then
Visibles = Visibles + 1
End If
Next
Dim NCol As Integer = grd.Columns.Count
Dim NRow As Integer = grd.RowCount
hoja_trabajo = DirectCast(libros_trabajo.Worksheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim Count = 1
For i As Integer = 1 To NCol
If grd.Columns(i - 1).Visible = True Then
hoja_trabajo.Cells.Item(1, Count) = grd.Columns(i - 1).Name
Count = Count + 1
End If
progres = progres + 1
If progres > ProgressBar1.Maximum Then
progres = ProgressBar1.Maximum
End If
ProgressBar1.Value = progres
Next
hoja_trabajo.Rows.Item(1).Font.Bold = 1
hoja_trabajo.Rows.Item(1).HorizontalAlignment = 3
hoja_trabajo.Columns.AutoFit()
For i As Integer = 0 To grd.Rows.Count - 1
Dim cant As Integer = 1
For j As Integer = 0 To grd.Columns.Count - 1
If grd.Columns(j).Visible = True Then
hoja_trabajo.Cells(i + 2, cant) = grd.Rows(i).Cells(j).Value
cant = cant + 1
End If
Next
progres = progres + 1
If progres > ProgressBar1.Maximum Then
progres = ProgressBar1.Maximum
End If
ProgressBar1.Value = progres
Next
ProgressBar1.Visible = False
aplicacion.Application.Visible = True
Return True
End Function
hoe kan ik nu bepalen in welke sheet het geplaatst wordt
en hoe wijzig in de naam van de sheet
ik stuur de gegevens
Public Function GridAExcel(ByVal grd As DataGridView)
ProgressBar1.Visible = True
Dim progres As Integer = 0
Dim x As Integer
Dim fichero As New SaveFileDialog()
fichero.Filter = "Excel (*.xls)|*.xls"
Dim aplicacion As Microsoft.Office.Interop.Excel.Application
Dim libros_trabajo As Microsoft.Office.Interop.Excel.Workbook
Dim hoja_trabajo As Microsoft.Office.Interop.Excel.Worksheet
aplicacion = New Microsoft.Office.Interop.Excel.Application()
libros_trabajo = aplicacion.Workbooks.Add()
Dim Visibles As Integer = 0
For x = 0 To grd.Columns.Count - 1
If grd.Columns(x).Visible = True Then
Visibles = Visibles + 1
End If
Next
Dim NCol As Integer = grd.Columns.Count
Dim NRow As Integer = grd.RowCount
hoja_trabajo = DirectCast(libros_trabajo.Worksheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim Count = 1
For i As Integer = 1 To NCol
If grd.Columns(i - 1).Visible = True Then
hoja_trabajo.Cells.Item(1, Count) = grd.Columns(i - 1).Name
Count = Count + 1
End If
progres = progres + 1
If progres > ProgressBar1.Maximum Then
progres = ProgressBar1.Maximum
End If
ProgressBar1.Value = progres
Next
hoja_trabajo.Rows.Item(1).Font.Bold = 1
hoja_trabajo.Rows.Item(1).HorizontalAlignment = 3
hoja_trabajo.Columns.AutoFit()
For i As Integer = 0 To grd.Rows.Count - 1
Dim cant As Integer = 1
For j As Integer = 0 To grd.Columns.Count - 1
If grd.Columns(j).Visible = True Then
hoja_trabajo.Cells(i + 2, cant) = grd.Rows(i).Cells(j).Value
cant = cant + 1
End If
Next
progres = progres + 1
If progres > ProgressBar1.Maximum Then
progres = ProgressBar1.Maximum
End If
ProgressBar1.Value = progres
Next
ProgressBar1.Visible = False
aplicacion.Application.Visible = True
Return True
End Function