public class Csv {
protected List<List<String>> data = new List<List<string>>();
protected int maxcells = 0;
public Csv() {
}
/// <summary>
/// 得到值
/// </summary>
/// <param name="x">列</param>
/// <param name="y">行</param>
/// <returns></returns>
public string getValue(int x, int y) {
if (x < 1 || y < 1) throw new Exception("out index");
if (data.Count < y || data[y-1].Count < x)
return "";
return data[y - 1][x - 1];
}
/// <summary>
/// 设置值
/// </summary>
/// <param name="x">列</param>
/// <param name="y">行</param>
/// <param name="v">值</param>
public void setValue(int x, int y, String v) {
if (x < 1 || y < 1) throw new Exception("out index");
if (data.Count < y || data[y - 1].Count < x) {
if (!string.IsNullOrEmpty(v)) {
while (data.Count < y) {
data.Add(new List<string>());
}
while (data[y - 1].Count < x) {
data[y - 1].Add("");
}
data[y - 1][x - 1] = v;
if (x > maxcells)
maxcells = x;
}
}
else {
data[y - 1][x - 1] = v == null ? "" : v;
}
}
/// <summary>
/// 最大行数
/// </summary>
public int MaxRows {
get { return data.Count; }
}
/// <summary>
/// 最大列数
/// </summary>
public int MaxCells {
get { return maxcells; }
}
/// <summary>
/// 得到cvs
/// </summary>
/// <param name="strTx"></param>
/// <returns></returns>
public static Csv Parse(string strTx) {
Csv csv = new Csv();
List<String> link = new List<String>();
int post = ParseLink(csv.data, strTx,ref link, 0);
while (post != 0) {
//下一行
if (post > 0) {
if (csv.data.Count > 0 && csv.data[csv.data.Count - 1].Count > csv.maxcells) {
csv.maxcells = csv.data[csv.data.Count - 1].Count;
}
link = new List<String>();
post = ParseLink(csv.data, strTx,ref link, post);
}
else {
//同一行的下一个
post = ParseLink(csv.data, strTx,ref link, -post);
}
}
if (csv.data.Count > 0 && csv.data[csv.data.Count - 1].Count > csv.maxcells) {
csv.maxcells = csv.data[csv.data.Count - 1].Count;
}
return csv;
}
private static int ParseLink(List<List<String>> data, String strTx,ref List<String> link, int post) {
Cell c = ParseCell(data, strTx, post);
link.Add(c.str);
if (post + c.len >= strTx.Length - 1) {
//全结束
data.Add(link);
return 0;
}
else if (strTx[post + c.len] == '\n') {
//行结束
data.Add(link);
//ParseLink(ref strTx, new List<String>(), post + c.len + 1);
return post + c.len + 1;
}
else if (strTx[post + c.len] == ',') {
//单元结束
//return ParseLink(ref strTx, link, post + c.len + 1);
return -(post + c.len + 1);
}
return 0;
}
private static Cell ParseCell(List<List<String>> data, String strTx, int s) {
if (strTx[s] == '"') {
bool count = true;
for (int i = s + 1; i < strTx.Length; i++) {
if (count) {
if (strTx[i] == '"') {
if (i == strTx.Length - 1 || strTx[i + 1] == ',' || strTx[i + 1] == '\n' || strTx[i + 1] == '\r') {
//找到
return new Cell() { str = strTx.Substring(s + 1, i - s - 1).Replace("\"\"", "\""), len = i - s + 1 };
}
}
}
if (strTx[i] == '"') {
count = !count;
}
}
}
else if (strTx[s] == ',') {
//找到
return new Cell() { str = "", len = 0 };
}
else if (strTx[s] == '\n' || strTx[s] == '\r') {
//空行
return new Cell() { str = "", len = 0 };
}
else {
bool count = true;
for (int i = s + 1; i < strTx.Length; i++) {
if (count) {
if (strTx[i] == ',' || strTx[i] == '\n' || strTx[i] == '\r') {
//找到
return new Cell() { str = strTx.Substring(s, i - s), len = i - s };
}
}
if (strTx[i] == '"') {
count = !count;
}
}
}
throw new Exception("encode is error");
}
public override string ToString() {
StringBuilder str = new StringBuilder();
foreach (List<string> link in data) {
for (int i = 0; i < link.Count; i++) {
str.Append("\"").Append(link[i].Replace("\"", "\"\"")).Append("\"");
if (i < link.Count - 1) {
str.Append(",");
}
}
//补空的,
for (int i = link.Count; i < maxcells - 1; i++) {
str.Append(",");
}
str.Append("\n");
}
return str.ToString();
}
public void SaveFile(string file) {
using (StreamWriter writer = File.CreateText(file)) {
foreach (List<string> link in data) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < link.Count; i++) {
str.Append("\"").Append(link[i].Replace("\"", "\"\"")).Append("\"");
if (i < link.Count - 1) {
str.Append(",");
}
}
//补空的,
for (int i = link.Count; i < maxcells - 1; i++) {
str.Append(",");
}
writer.WriteLine(str.ToString());
}
writer.Flush();
}
}
}
class Cell {
public String str;
public int len;
}
csv读写 备忘
最新推荐文章于 2023-05-25 23:47:48 发布