/// <summary> /// Returns a string representation of the CSV file, using the specified <paramref name="separator"/>. /// </summary> /// <param name="separator">The separator to be used.</param> /// <returns>A string representation of the CSV file.</returns> public string ToString(CsvSeparator separator) { StringBuilder sb = new StringBuilder(); // Get the separator as a "char" char sep; switch (separator) { case CsvSeparator.Comma: sep = ','; break; case CsvSeparator.Colon: sep = ':'; break; case CsvSeparator.SemiColon: sep = ';'; break; case CsvSeparator.Space: sep = ' '; break; case CsvSeparator.Tab: sep = '\t'; break; default: sep = ';'; break; } // Append the first line with the column headers for (int i = 0; i < Columns.Length; i++) { if (i > 0) { sb.Append(sep); } sb.Append(Escape(Columns[i].Name, sep)); } foreach (CsvRow row in Rows) { sb.AppendLine(); for (int i = 0; i < Columns.Length; i++) { if (i > 0) { sb.Append(sep); } CsvCell cell = i < row.Cells.Length ? row.Cells[i] : null; sb.Append(Escape(cell == null ? "" : cell.Value, sep)); } } return(sb.ToString()); }
/// <summary> /// Gets the string value of the cell at the specified <paramref name="index"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="index">The index of the cell.</param> /// <returns>The string value of the cell.</returns> public T GetCellValue <T>(int index) { CsvCell cell = Cells[index]; return(cell == null ? default(T) : (T)Convert.ChangeType(cell.Value, typeof(T))); }
/// <summary> /// Gets the string value of the cell at the specified <paramref name="index"/>. /// </summary> /// <param name="index">The index of the cell.</param> /// <returns>The string value of the cell.</returns> public string GetCellValue(int index) { CsvCell cell = Cells[index]; return(cell == null ? null : cell.Value); }
/// <summary> /// Gets the string value of the cell with the specified <paramref name="columnName"/>, and converts it using /// <paramref name="callback"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="columnName">The name of the cell.</param> /// <param name="callback">The callback function to be used for converting the value.</param> /// <returns>An instance of <typeparamref name="T"/> representing the value of the cell.</returns> public T GetCellValue <T>(string columnName, Func <string, T> callback) { CsvCell cell = this[columnName]; return(cell == null ? default(T) : callback(cell.Value)); }
/// <summary> /// Gets the string value of the cell with the specified <paramref name="columnName"/>, and converts it to the /// type of <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="columnName">The name of the cell.</param> /// <returns>An instance of <typeparamref name="T"/> representing the value of the cell.</returns> public T GetCellValue <T>(string columnName) { CsvCell cell = this[columnName]; return(cell == null ? default(T) : (T)Convert.ChangeType(cell.Value, typeof(T))); }
/// <summary> /// Gets the string value of the cell with the specified <paramref name="columnName"/>. If multiple columns /// match <paramref name="columnName"/>, only the value of the first cell will be returned. /// </summary> /// <param name="columnName">The name of the cell.</param> /// <returns>The string value of the cell.</returns> public string GetCellValue(string columnName) { CsvCell cell = this[columnName]; return(cell == null ? null : cell.Value); }
/// <summary> /// Gets the string value of the cell at the specified <paramref name="index"/>, and converts it using /// <paramref name="callback"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="index">The index of the cell.</param> /// <param name="callback">The callback function to be used for converting the value.</param> /// <returns>An instance of <typeparamref name="T"/> representing the value of the cell.</returns> public T GetCellValue <T>(int index, Func <string, T> callback) { CsvCell cell = Cells[index]; return(cell == null ? default(T) : callback(cell.Value)); }