1

I've to write a csv for a third-party upload. They're saying they can't read the csv file created because it has two extra lines of code and doesn't end (when opened in Notepad) on the last character of the last line. That's true - but can anyone tell me why?

Dim _csvLine As New System.IO.StreamWriter(Server.MapPath("~/folder/_" & rptType.SelectedItem.Value.ToString & ".csv"), False)
Dim tb As New StringBuilder
Dim x As Integer = ds.Tables("csv").Columns.Count, y As Integer = 1
For Each row As Data.DataRow In ds.Tables("csv").Rows
    For Each col As Data.DataColumn In ds.Tables("csv").Columns
        If y <> x Then
            tb.Append(Trim(System.Text.RegularExpressions.Regex.Replace(row.Item(col).ToString, "\s+", " ", RegexOptions.IgnoreCase Or RegexOptions.Multiline)) & ",")
            y = y + 1
        Else
            tb.Append(Trim(System.Text.RegularExpressions.Regex.Replace(row.Item(col).ToString, "\s+", " ", RegexOptions.IgnoreCase Or RegexOptions.Multiline)))
            tb.AppendLine()
            y = 1
        End If
    Next
Next
_csvLine.WriteLine(Left(tb.ToString, Len(tb.ToString) - 2))
_csvLine.Flush()
_csvLine.Close()
_csvLine.Dispose()
4

2 回答 2

2

One line is appended by _csvLine.WriteLine, use Write() instead. tb.AppendLine() adds the second line, try avoiding it on the last line.

于 2012-11-02T19:20:00.467 回答
0

您正在执行_csvLine.WriteLine(的操作是在文件末尾添加换行符。

于 2012-11-02T19:18:46.033 回答