Buenas tarde.. su apoyo por favor..
al momento de exportar los datos del datagridview a excel . me sale que la fecha que debes de ser 01/09/2021(correcto) sale al contrario 09/01/2021(incorrecto) y los numero por ejemplo 00284514 (correcto) sale 284514, me elimina los ceros de la izquierda.. porfavor ayuda...adjunto el codigo que copie de aqui.. el codigo enviado es el que estoy trabajando, al estar vacio el espacio de las sal error, como modificar para que pase por alto los registros vacios, al momento de exportar
muy agradecido de antemano
gracias
Translated:
Good afternoon. Your support please.
When exporting the data from the DataGridView to Excel. I get that the date that you must be 01/09/2021 (correct) comes out the opposite 09/01/2021 (incorrect) and I number them for example 00284514 (correct) comes out 284514, it eliminates the zeros on the left .. please help... I attach the code that I copy from here. The code sent is the one I am working on, being empty the space of the salt error, how to modify so that it ignores the empty records, at the time of exporting
very grateful in advance
Thank you
What I have tried:
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
MsgBox("datagrid vacio")
End If
'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
Next
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
dr1(j) = DataGridView1.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim pexcel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = pexcel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
pexcel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
pexcel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
MsgBox("Espere la consulta esta siendo exportada")
MsgBox("Cierre los Archivos de Excel de lo contrario no se Guardara", MsgBoxStyle.OkOnly)
wSheet.Columns.AutoFit()
Dim strFileName As String = "D:\Filtro.xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
pexcel.Workbooks.Open(strFileName)
pexcel.Visible = True
DataGridView1.DataSource = Nothing
Button8.Enabled = False
End Sub