我有一个将DataGridView导出为CSV的方法。但我有一个场景,我有一个像2-3表init的DataSet。我想将其导出到单个CSV文件中,每个DataSet表位于单独的CSV工作表中。
这是我将DataGridView转换为CSV文件的方法(注意,如果可以使用在线工具将其转换为Vb,我也会接受C#代码):
Private Sub subExportDGVToCSV(ByVal strExportFileName As String, ByVal DataGridView As DataGridView, Optional ByVal blnWriteColumnHeaderNames As Boolean = False, Optional ByVal strDelimiterType As String = ",")
Dim sr As StreamWriter = File.CreateText(strExportFileName)
Dim strDelimiter As String = strDelimiterType
Dim intColumnCount As Integer = DataGridView.Columns.Count - 1
Dim strRowData As String = ""
If blnWriteColumnHeaderNames Then
For intX As Integer = 0 To intColumnCount
strRowData += Replace(DataGridView.Columns(intX).Name, strDelimiter, "") & IIf(intX < intColumnCount, strDelimiter, "")
Next intX
sr.WriteLine(strRowData)
End If
For intX As Integer = 0 To DataGridView.Rows.Count - 1
strRowData = ""
For intRowData As Integer = 0 To intColumnCount
strRowData += Replace(DataGridView.Rows(intX).Cells(intRowData).Value, strDelimiter, "") & IIf(intRowData < intColumnCount, strDelimiter, "") '''''''''highlights this row
Next intRowData
sr.WriteLine(strRowData)
Next intX
sr.Close()
End Sub
答案 0 :(得分:0)
您可以像xml映射一样分隔工作表,并在想要使用时解析它们
CSV:
<book>
<sheet>
//Sheet 1
.....
</sheet>
<sheet>
//Sheet 2
.....
</sheet>
</book>