49 lines
1.1 KiB
C#
49 lines
1.1 KiB
C#
|
|
using System.Text;
|
||
|
|
|
||
|
|
namespace Hncore.Infrastructure.CSV
|
||
|
|
{
|
||
|
|
public class CsvRow
|
||
|
|
{
|
||
|
|
private string _content = "";
|
||
|
|
|
||
|
|
public CsvRow AddCell(string content)
|
||
|
|
{
|
||
|
|
if (_content != "")
|
||
|
|
{
|
||
|
|
_content += ",";
|
||
|
|
}
|
||
|
|
|
||
|
|
_content += StringToCsvCell(content);
|
||
|
|
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override string ToString()
|
||
|
|
{
|
||
|
|
return _content;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string StringToCsvCell(string str)
|
||
|
|
{
|
||
|
|
bool mustQuote = str.Contains(",") || str.Contains("\"") || str.Contains("\r") || str.Contains("\n");
|
||
|
|
if (mustQuote)
|
||
|
|
{
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
sb.Append("\"");
|
||
|
|
foreach (char nextChar in str)
|
||
|
|
{
|
||
|
|
sb.Append(nextChar);
|
||
|
|
if (nextChar == '"')
|
||
|
|
{
|
||
|
|
sb.Append("\"");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
sb.Append("\"");
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
return str;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|