[英]How to get rid of time when export datagridview into excel file VB.NET
大家好,我有一個問題,關於如何在使用 vb.net 在 datagridview 中導出數據列表時擺脫日期后的時間
這里圖片
這是 datagridview 中的日期,根本沒有時間
但是在導出數據后變成日期時間..同樣去導出到 pdf..還有時間..
Private Sub tsbtnExcel_Click(sender As Object, e As EventArgs) Handles tsbtnExcel.Click
With SaveFileDialog1
End With
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim filename As String
filename = SaveFileDialog1.FileName
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
'Export Header Names Start
Dim columnsCount As Integer = DataGridView1.Columns.Count
For k As Integer = 1 To columnsCount
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
Next
'Export Header Name End
'Export Each Row Start
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim checkselect As Integer = Convert.ToInt16(row.Cells("chkCheck").Value)
If checkselect = 1 Then
For columnIndex As Integer = 1 To columnsCount - 1
xlWorkSheet.Cells(i + 2, columnIndex + 1).Value = row.Cells(columnIndex).Value.ToString
Next
i += 1
End If
Next
'Export Each Row End
xlWorkSheet.SaveAs(filename + ".csv")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MessageBox.Show("Export excel file successfully",
"Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("Canceled, Export data failed",
"Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
這是將數據導出到 excel 的編碼。我使用復選框 datagridview 以便我可以選擇要導出的那個。所以我希望你們能幫助我擺脫導出到 excel 時的時間
一個快速的解決方案是將您的日期轉換為 gridview 中的字符串。
date.ToString("dd/MM/yyyy")
所以導入會將這兩列視為簡單的字符串
我知道了xlWorkSheet.Cells(i + 2, columnIndex + 1).Value = row.Cells(columnIndex).Value.ToString1
這個編碼假設是xlWorkSheet.Cells(i + 2, columnIndex + 1).Value = row.Cells(columnIndex).Value
... 將 DGV 導出到 excel 時, ToString
需要日期和時間
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.