也是转载的,不知道哪个是原作者,原作者若看到请联系本人注明
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace USB_N40A3
{
class DataTableCSV
{
/// <summary>
/// 将DataTable中数据写入到CSV文件中
/// </summary>
/// <param name="dt">提供保存数据的DataTable</param>
/// <param name="fileName">CSV的文件路径</param>
public static bool SaveCSV(DataTable dt, string fullPath)
{
try
{
FileInfo fi = new FileInfo(fullPath);
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
string data = "";
//写出列名称
for (int i = 0; i < dt.Columns.Count; i++)
{
data += "\"" + dt.Columns[i].ColumnName.ToString() + "\"";
if (i < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
//写出各行数据
for (int i = 0; i < dt.Rows.Count; i++)
{
data = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
string str = dt.Rows[i][j].ToString();
str = string.Format("\"{0}\"", str);
data += str;
if (j < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
}
sw.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
public static bool SaveCSVAppend(DataTable dt, string fullPath)
{
try
{
FileInfo fi = new FileInfo(fullPath);
StreamWriter sw;
FileStream fs;
if (!File.Exists(fullPath))
{
fi.Directory.Create();
fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.Default);
}
else
{
fs = new FileStream(fullPath, FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.Default);
}
string data = "";
//写出列名称
for (int i = 0; i < dt.Columns.Count; i++)
{
data += "\"" + dt.Columns[i].ColumnName.ToString() + "\"";
if (i < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
for (int i = 0; i < dt.Rows.Count; i++)
{
string data1 = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
string str = dt.Rows[i][j].ToString();
str = string.Format("\"{0}\"", str);
data1 += str;
if (j < dt.Columns.Count - 1)
{
data1 += ",";
}
}
sw.WriteLine(data1);
}
sw.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取CSV文件到DataTable中
/// </summary>
/// <param name="filePath">CSV的文件路径</param>
/// <returns></returns>
public static DataTable ReadCSV(string filePath)
{
DataTable dt = new DataTable();
int lineNumber = 0;
using (CsvFileReader reader = new CsvFileReader(filePath, Encoding.Default))
{
CsvRow row = new CsvRow();
while (reader.ReadRow(row))
{
if (0 == lineNumber)
{
foreach (string s in row)
{
dt.Columns.Add(s.Replace("\"", ""));
}
}
else
{
int index = 0;
DataRow dr = dt.NewRow();
foreach (string s in row)
{
dr[index] = s.Replace("\"", "");
index++;
}
dt.Rows.Add(dr);
}
lineNumber++;
}
}
return dt;
}
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename, Encoding encoding)
: base(filename, encoding)
{
}
/// <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);
}
}
public static DataTable GetDgvToTable(DataGridView dgv)
{
DataTable dt = new DataTable();
// 列强制转换
for (int count = 0; count < dgv.Columns.Count; count++)
{
DataColumn dc = new DataColumn(dgv.Columns[count].Name .ToString());
dt.Columns.Add(dc);
}
// 循环行
for (int count = 0; count < dgv.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
{
dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
}
dt.Rows.Add(dr);
}
return dt;
}
}
}
标签:Count,LineText,++,pos,保存,dt,CSV,DataTable,row
From: https://www.cnblogs.com/yuanqi99977/p/16911519.html