Imports System.Text
Imports System.IO
''' <summary>
''' 快速写入数据到CSV文件
''' </summary>
Public Class CsvWriter
#Region "Constants"
''' <summary>
''' Defines the default delimiter character separating each field.
''' </summary>
Public Const DefaultDelimiter As Char = ","c
''' <summary>
''' Defines the default quote character wrapping every field.
''' </summary>
Public Const DefaultQuote As Char = """"c
''' <summary>
''' Defines the default escape character letting insert quotation characters inside a quoted field.
''' </summary>
Public Const DefaultEscape As Char = """"c
''' <summary>
''' Defines the default comment character indicating that a line is commented out.
''' </summary>
Public Const DefaultComment As Char = "#"c
#End Region
#Region "Constructors"
Sub New()
Me.New(DefaultDelimiter, DefaultQuote, DefaultEscape, DefaultComment)
End Sub
Sub New(ByVal delimiter As Char)
Me.New(delimiter, DefaultQuote, DefaultEscape, DefaultComment)
End Sub
''' <summary>
''' Initializes a new instance of the CsvWriter class.
''' </summary>
''' <param name="delimiter">The delimiter character separating each field (default is ',').(分隔符号默认",")</param>
''' <param name="quote">The quotation character wrapping every field (default is ''').(需要转义的字符串 重复出现2次)</param>
''' <param name="escape">
''' The escape character letting insert quotation characters inside a quoted field (default is '\').
''' If no escape character, set to '\0' to gain some performance.(包装用)
''' </param>
''' <param name="comment">The comment character indicating that a line is commented out (default is '#').</param>
''' <exception cref="T:ArgumentNullException">
''' <paramref name="reader"/> is a <see langword="null"/>.
''' </exception>
''' <exception cref="ArgumentOutOfRangeException">
''' <paramref name="bufferSize"/> must be 1 or more.
''' </exception>
Sub New(ByVal delimiter As Char, ByVal quote As Char, ByVal escape As Char, ByVal comment As Char)
_delimiter = delimiter
_quote = quote
_escape = escape
_comment = comment
End Sub
#End Region
#Region "Write"
Public Sub Write(ByVal writer As TextWriter, ByVal table As DataTable)
Dim cols = From c As DataColumn In table.Columns Select ObjToString(c.ColumnName)
writer.WriteLine(String.Join(_delimiter, cols))
Write(writer, table.Rows)
End Sub
Public Sub Write(ByVal writer As TextWriter, ByVal rows As IList(Of String()))
For Each row In rows
Dim items() As String = (From r In row Select ObjToString(r)).ToArray
Dim str As String = String.Join(_delimiter, items)
writer.WriteLine(str)
Next
End Sub
Public Sub Write(ByVal writer As TextWriter, ByVal rows() As DataRow)
For Each row As DataRow In rows
Dim items() As String = (From o As Object In row.ItemArray Select ObjToString(o)).ToArray
Dim str As String = String.Join(_delimiter, items)
writer.WriteLine(str)
Next
End Sub
Public Sub Write(ByVal writer As TextWriter, ByVal rows As DataRowCollection)
For Each row As DataRow In rows
Dim items() As String = (From o As Object In row.ItemArray Select ObjToString(o)).ToArray
Dim str As String = String.Join(_delimiter, items)
writer.WriteLine(str)
Next
End Sub
Public Function ObjToString(ByVal obj As Object) As String
If obj Is Nothing Then Return String.Empty
Dim str As String = obj.ToString
'If str.Length > 30000 Then MsgBox(str)
If str.Contains(_delimiter) OrElse str.Contains(_quote) OrElse str.Contains(vbCr) OrElse str.Contains(vbLf) Then
Dim strb As New StringBuilder(_escape)
For Each c In str
If c = _quote Then strb.Append(_quote)
strb.Append(c)
Next
strb.Append(_escape)
Return strb.ToString
End If
Return str
End Function
#End Region
#Region "Fields"
''' <summary>
''' Contains the <see cref="T:TextReader"/> export to the CSV file.
''' </summary>
Private _writer As TextWriter
''' <summary>
''' Contains the comment character indicating that a line is commented out.
''' </summary>
Private _comment As Char
''' <summary>
''' Contains the escape character letting insert quotation characters inside a quoted field.
''' </summary>
Private _escape As Char
''' <summary>
''' Contains the delimiter character separating each field.
''' </summary>
Private _delimiter As Char
''' <summary>
''' Contains the quotation character wrapping every field.
''' </summary>
Private _quote As Char
''' <summary>
''' Indicates if the reader supports multiline.
''' </summary>
Private _supportsMultiline As Boolean
''' <summary>
''' Indicates if the reader will skip empty lines.
''' </summary>
Private _skipEmptyLines As Boolean
#End Region
#Region "Properties"
''' <summary>
''' Gets the comment character indicating that a line is commented out.
''' </summary>
''' <value>The comment character indicating that a line is commented out.</value>
Public ReadOnly Property Comment As Char
Get
Return _comment
End Get
End Property
''' <summary>
''' Gets the escape character letting insert quotation characters inside a quoted field.
''' </summary>
''' <value>The escape character letting insert quotation characters inside a quoted field.</value>
Public ReadOnly Property Escape As Char
Get
Return _escape
End Get
End Property
''' <summary>
''' Gets the delimiter character separating each field.
''' </summary>
''' <value>The delimiter character separating each field.</value>
Public ReadOnly Property Delimiter As Char
Get
Return _delimiter
End Get
End Property
''' <summary>
''' Gets the quotation character wrapping every field.
''' </summary>
''' <value>The quotation character wrapping every field.</value>
Public ReadOnly Property Quote As Char
Get
Return _quote
End Get
End Property
#End Region
End Class
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace CsvIO.Csv
{
/// <summary>
/// 快速写入数据到CSV文件
/// </summary>
public static class CsvWriter
{
#region Constants
/// <summary>
/// Defines the default delimiter character separating each field.
/// </summary>
public const char DefaultDelimiter = ',';
/// <summary>
/// Defines the default quote character wrapping every field.
/// </summary>
public const char DefaultQuote = '"';
/// <summary>
/// Defines the default escape character letting insert quotation characters inside a quoted field.
/// </summary>
public const char DefaultEscape = '"';
/// <summary>
/// The space character.
/// </summary>
private const char SPACE = ' ';
/// <summary>
/// The carriage return character. Escape code is <c>\r</c>.
/// </summary>
private const char CR = (char)0x0d;
/// <summary>
/// The line-feed character. Escape code is <c>\n</c>.
/// </summary>
private const char LF = (char)0x0a;
#endregion
#region Write
public static void Write(TextWriter writer, IEnumerable<string> row)
{
Write(writer, row, DefaultDelimiter);
}
public static void Write(TextWriter writer, IEnumerable<string> row, char delimiter)
{
bool start = false;
foreach (string str in row)
{
if (start == true)
writer.Write(delimiter);
else
start = true;
writer.Write(CsvString(str, delimiter));
}
writer.WriteLine();
}
private static string CsvString(string str, char delimiter)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
bool delimit = false;
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if (c == delimiter || c == CR || c == LF)
{
delimit = true;
}
else if (c == DefaultQuote)
{
sb.Append(DefaultQuote);
delimit = true;
}
sb.Append(c);
}
if (delimit)
{
sb.Insert(0, DefaultEscape);
sb.Append(DefaultEscape);
}
return sb.ToString();
}
#endregion
}
}