Home > Net >  [VB.NET] DataGridView export to Excel for help
[VB.NET] DataGridView export to Excel for help

Time:11-14


Here, I want to have the data in the DataGridView export to Excel, but export and found the following two questions, please help solve, thank you,
[a] id a column, a scientific notation, "2.10503 e+17" as shown in the figure below, how to display the normal id number?

[ii] the modification time, time of filling the default display "# # # # # # #", as shown in the figure below, how to display normal cell content?


Attachment: export to Excel code:
 
Private Sub Button1_Click (sender As Object, As EventArgs e) Handles for. Click
Dim kk As New SaveFileDialog ()
Kk. Title="save EXECL file
"Kk. Filter="EXECL file (*. XLS) | *. XLS | all files (*. *) | *. *", "
Kk. FilterIndex=1

If kk. ShowDialog ()=DialogResult. OK Then
Dim FileName As String=kk. FileName '+ ". XLS "
. If the File Exists (FileName) Then
File. The Delete (FileName)
End the If
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
Dim strLine As String=""
ObjFileStream=New FileStream (FileName, FileMode OpenOrCreate, FileAccess. Write)
ObjStreamWriter=New StreamWriter (Unicode) objFileStream, System. Text. Encoding.
For I As Integer=0 To DataGridView1. Columns. The Count - 1
If DataGridView1. Columns (I). The Visible=True Then
StrLine=strLine + DataGridView1. Columns (I). The HeaderText. The ToString () + the Convert. ToChar (9)
End the If
Next
ObjStreamWriter. WriteLine (strLine)
StrLine=""

For I As Integer=0 To DataGridView1. Rows. The Count - 1
If DataGridView1. Columns (0). The Visible=True Then
If DataGridView1. Rows (I) Cells (0). The Value Is Nothing Then
StrLine=(strLine & amp; "") + the Convert. ToChar (9)
The Else
StrLine=strLine + DataGridView1. Rows (I) Cells (0) Value. The ToString () + the Convert. ToChar (9)
End the If
End the If
For j As Integer=1 To DataGridView1 Columns. The Count - 1
If DataGridView1. Columns (j) Visible=True Then
If DataGridView1. Rows (I) Cells (j). The Value Is Nothing Then
StrLine=(strLine & amp; "") + the Convert. ToChar (9)
The Else
Dim rowstr As String=""
Rowstr=DataGridView1. Rows (I) Cells (j) Value. The ToString ()
If rowstr. IndexOf (vbCr & amp; VbLf) & gt; 0 Then
Rowstr=rowstr. Replace (vbCr & amp; VbLf, "")
End the If
If rowstr. IndexOf (vbTab) & gt; 0 Then
Rowstr=rowstr. Replace (vbTab, "")
End the If
StrLine=strLine + rowstr + Convert. ToChar (9)
End the If
End the If
Next
ObjStreamWriter. WriteLine (strLine)
StrLine=""
Next
ObjStreamWriter. Close ()
ObjFileStream. Close ()
MessageBox. Show (Me, "save the EXCEL success", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information)
End the If
End Sub

CodePudding user response:

With what? Npoi?
Problem a to d column set to text format,
Question 2 to set up the column width,
  • Related