public string Serialize() { StrGen.Builder builder = StrGen.GetBuilder(); int lastRow = 0; DoCell((row, col, cellValue) => { if (row != lastRow) { builder.Append("\n"); lastRow = row; } if (col != 0) { builder.Append(","); } AppendString(builder, cellValue); }); return(builder.GetStringAndRelease()); }
static void AppendString(StrGen.Builder sb, string cellValue) { if (null == cellValue) { cellValue = ""; } else { cellValue = cellValue.Replace("\n", "\\n"); if (cellValue.IndexOfAny(",\"".ToCharArray()) >= 0) { cellValue = cellValue.Replace("\"", "\"\""); sb.AppendFormat("\"{0}\"", cellValue); return; } } sb.Append(cellValue); }