[.NET] 將大量的string丟入DataGridView
我必須把篩好的資料以excel方式匯出,有幾個問題一直很不解 大概是觀念不清導致
請板友前輩幫指導一下,謝謝~~~
1. 篩好的資料為string 可以直接丟入datagridview嗎,.net很像不會讓你這麼做,是因為沒有指定row 跟 columns嗎?
2. 若string不可以直接用datagridview接,那就必須建datatable丟入datagridview嗎
3. 丟入datagridview都必須指定行列嗎?
Dim theNotice as String
theNotice = theNotice & theNotice_org & vbCrLf
------------------以theNotice怎麼丟到DataToExcel? ---------
Public Sub DataToExcel(ByVal m_DataView As DataGridView)
Dim kk As New SaveFileDialog()
kk.Title = "儲存EXECL文件"
kk.Filter = "EXECL文件(*.xls) |*.xls |所O有3文件(*.*) |*.*"
kk.FilterIndex = 1
If kk.ShowDialog() = DialogResult.OK Then
Dim FileName As String = kk.FileName ' + ".xls"
If File.Exists(FileName) Then
File.Delete(FileName)
End If
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
Dim strLine As String = ""
objFileStream = New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream, System.Text.Encoding.Unicode)
For i As Integer = 0 To m_DataView.Columns.Count - 1
If m_DataView.Columns(i).Visible = True Then
strLine = strLine + m_DataView.Columns(i).HeaderText.ToString() + Convert.ToChar(9)
End If
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
For i As Integer = 0 To m_DataView.Rows.Count - 1
If m_DataView.Columns(0).Visible = True Then
If m_DataView.Rows(i).Cells(0).Value Is Nothing Then
strLine = (strLine & " ") + Convert.ToChar(9)
Else
strLine = strLine + m_DataView.Rows(i).Cells(0).Value.ToString() + Convert.ToChar(9)
End If
End If
For j As Integer = 1 To m_DataView.Columns.Count - 1
If m_DataView.Columns(j).Visible = True Then
If m_DataView.Rows(i).Cells(j).Value Is Nothing Then
strLine = (strLine & " ") + Convert.ToChar(9)
Else
Dim rowstr As String = ""
rowstr = m_DataView.Rows(i).Cells(j).Value.ToString()
If rowstr.IndexOf(vbCr & vbLf) > 0 Then
rowstr = rowstr.Replace(vbCr & vbLf, " ")
End If
If rowstr.IndexOf(vbLf) > 0 Then
rowstr = rowstr.Replace(vbLf, " ")
End If
If rowstr.IndexOf(vbTab) > 0 Then
rowstr = rowstr.Replace(vbTab, " ")
End If
strLine = strLine + rowstr + Convert.ToChar(9)
End If
End If
'm_DataView.Rows(j).DefaultCellStyle.BackColor = DataGridView1.Rows(j).DefaultCellStyle.BackColor
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
Next
objStreamWriter.Close()
objFileStream.Close()
MessageBox.Show(Me, "儲存EXCEL成功, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 221.120.64.187
→
05/25 22:42, , 1F
05/25 22:42, 1F
※ 編輯: iconograph 來自: 221.120.64.187 (05/26 00:01)
→
05/26 00:07, , 2F
05/26 00:07, 2F