作者iconograph (Amber)
站内Visual_Basic
标题[.NET] 将大量的string丢入DataGridView
时间Wed May 25 21:24:28 2011
我必须把筛好的资料以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
1F:→ sueadolph:缩排一下吧,程式也不是二三行的,这样子别人会想帮忙吗 05/25 22:42
※ 编辑: iconograph 来自: 221.120.64.187 (05/26 00:01)
2F:→ iconograph:sorry,一时疏忽缩排,改正 05/26 00:07