/// <summary> /// Generates a CSV file from the passed datatable. /// </summary> /// <param name="dt">The datatable to convert to the CSV file. Data column names will be in the first row. Note: Currently, newline characters will be replaced by spaces.</param> /// <param name="outputFile">The full path to the output file.</param> /// <param name="overwrite">If true, the current file will be erased if it exists. Otherwise, it will be appended.</param> /// <param name="maxRows">The maximum number of rows to output from the DataTable.</param> /// <param name="maxCols">The maximum number of columns to output from the DataTable.</param> public static bool DataTableToCSV(this DataTable dt, string outputFile, bool overwrite, int maxRows, int maxCols) { if (dt == null || String.IsNullOrEmpty(outputFile)) { throw new ArgumentException("The datatable cannot be null and outputFile must be a valid string."); } bool rtrn = false; if (overwrite && File.Exists(outputFile)) { File.Delete(outputFile); } try { using (CSVWriter csv = new CSVWriter(outputFile)) { int colCount = 0; //Get header row CSVRow header = new CSVRow(); foreach (DataColumn dc in dt.Columns) { //Check if we've reached the max col count colCount++; if (colCount > maxCols) { break; } //Add the column name header.Add(dc.ColumnName.Replace("\r", "").Replace("\n", " ")); } csv.WriteRow(header); //Insert the data int cntr = 0; foreach (DataRow dr in dt.Rows) { CSVRow row = new CSVRow(); for (int cind = 0; cind < dt.Columns.Count; cind++) { row.Add(dr[cind].ToString().Replace("\r", "").Replace("\n", " ")); } csv.WriteRow(row); cntr++; if (cntr >= maxRows) { break; } } rtrn = true; } } catch { throw; } return(rtrn); }
/// <summary> /// Reads a row of data from a CSV file /// </summary> /// <param name="row"></param> /// <returns></returns> public bool ReadRow(CSVRow row) { row.LineText = ReadLine(); if (String.IsNullOrEmpty(row.LineText)) { return(false); } int pos = 0; int rows = 0; while (pos < row.LineText.Length) { string value; // Special handling for quoted field if (row.LineText[pos] == '"') { // Skip initial quote pos++; // Parse quoted value int start = pos; while (pos < row.LineText.Length) { // Test for quote character if (row.LineText[pos] == '"') { // Found one pos++; // If two quotes together, keep one // Otherwise, indicates end of value if (pos >= row.LineText.Length || row.LineText[pos] != '"') { pos--; break; } } pos++; } value = row.LineText.Substring(start, pos - start); value = value.Replace("\"\"", "\""); } else { // Parse unquoted value int start = pos; while (pos < row.LineText.Length && row.LineText[pos] != ',') { pos++; } value = row.LineText.Substring(start, pos - start); } // Add field to list if (rows < row.Count) { row[rows] = value; } else { row.Add(value); } rows++; // Eat up to and including next comma while (pos < row.LineText.Length && row.LineText[pos] != ',') { pos++; } if (pos < row.LineText.Length) { pos++; } } // Delete any unused items while (row.Count > rows) { row.RemoveAt(rows); } // Return true if any columns read return(row.Count > 0); }