[.NET] 將大量的string丟入DataGridView

看板Visual_Basic作者 (Amber)時間14年前 (2011/05/25 21:24), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
我必須把篩好的資料以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
sorry,一時疏忽縮排,改正
05/26 00:07, 2F
文章代碼(AID): #1DtGCE7U (Visual_Basic)