简单的csv读卡器

编程入门 行业动态 更新时间:2024-10-26 05:20:06
本文介绍了简单的csv读卡器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

all,

我开始用我以为是一个非常简单的任务。 (将csv转换为wiki格式),但是遇到一些麻烦,通过

我有3个主要问题

1)某些单元格包含\r\\\(所以当逐行阅读时,将每行新行视为新单元格

2)某些行包含,(我尝试切换到\t的已删除文件,但是仍然遇到一个问题,当它在两个之间转移时)

我已经尝试了一些CSV阅读器课程,但他们会碰到上面列出的问题。

我是试图保持这个应用程序尽可能小,所以我也试图避免dll和大型课程,只有一小部分做我想要的。

到目前为止,我有两个尝试不工作

Atempt 1(不会在单元格中处理\\\\)

OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); openFileDialog1.Filter =tab sep文件(* .txt)| * .txt |所有文件(*。*)| *。*; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if(openFileDialog1.ShowDialog()== DialogResult.OK) { if(cb_sortable.Checked) { header ={| class = \wikitable sortable\border = \1\\r\\\ | +可排序表; } StringBuilder sb = new StringBuilder(); 字符串; bool firstline = true; StreamReader sr = new StreamReader(openFileDialog1.FileName); sb.AppendLine(标题); while((line = sr.ReadLine())!= null) { if(line.Replace(\t, ).Length> 1) { string [] hold; string lead =|; if(firstline&& cb_header.Checked == true) { lead =| align = \center\style = \background :#f0f0f0; \|; } hold = line.Split('\t'); sb.AppendLine(table); foreach(保持的字符串行) { sb.AppendLine(lead + row.Replace(\,)); } firstline = false; } } sb.AppendLine(footer); Clipboard.SetText(sb.ToString()); MessageBox.Show(Done!); } } string header ={| class = \wikitable\border = \1\; string footer =|}; string table =| - ; < 2(可以处理\r\\\但可以移动单元格空白单元格)(尚未完成)

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = Environment。 GetFolderPath(Environment.SpecialFolder.Desktop); openFileDialog1.Filter =txt文件(* .txt)| * .txt |所有文件(*。*)| *。*; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if(openFileDialog1.ShowDialog()== DialogResult.OK) { if(cb_sortable.Checked) { header ={| class = \wikitable sortable\border = \1\\r\\\ | +可排序表; } 使用( StreamReader sr = new StreamReader(openFileDialog1.FileName)) { string text = sr.ReadToEnd(); string [] cells = text.Split '\t'); int columnCount = 0; foreach(单元格中的字符串单元格) { if(cell.Contains(\r \\\)) { break; } columnCount ++; } }

基本上我所需要的只是分裂,如果不是在\之间, p>

任何提示或技巧将不胜感激

解决方案

结帐这个项目,而不是滚动您自己的CSV解析器。

all,

I started out with what i thought was going to be a pretty simple task. (convert a csv to "wiki" format) but im hitting a few snags that im having trouble working through

I have 3 main problems

1) some of the cells contain \r\n ( so when reading line by line this treats each new line as a new cell

2) some of the rows contain "," ( i tried switching to \t delemited files but im still running into a problem escaping when its between two "")

3) some rows are completely blank except for the delmiter ("," or "\t") others are incomplete (which is fine i just need to make sure that the cell goes in the correct place)

I've tried a few of the CSV reader classes but they would bump up agenst of teh problems listed above

I'm trying to keep this app as small as possible so i am also trying to avoid dlls and large classes that only a small portion do what i want.

so far i have two "attempts that are not working

Atempt 1 (doesn't handel \r\n in a cell)

OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); openFileDialog1.Filter = "tab sep file (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (cb_sortable.Checked) { header = "{| class=\"wikitable sortable\" border=\"1\" \r\n|+ Sortable table"; } StringBuilder sb = new StringBuilder(); string line; bool firstline = true; StreamReader sr = new StreamReader(openFileDialog1.FileName); sb.AppendLine(header); while ((line = sr.ReadLine()) != null) { if (line.Replace("\t", "").Length > 1) { string[] hold; string lead = "| "; if (firstline && cb_header.Checked == true) { lead = "| align=\"center\" style=\"background:#f0f0f0;\"| "; } hold = line.Split('\t'); sb.AppendLine(table); foreach (string row in hold) { sb.AppendLine(lead + row.Replace("\"", "")); } firstline = false; } } sb.AppendLine(footer); Clipboard.SetText(sb.ToString()); MessageBox.Show("Done!"); } } string header = "{| class=\"wikitable\" border=\"1\" "; string footer = "|}"; string table = "|-";

attempt 2 ( can handle \r\n but shifts cells over blank cells) (its not complete yet)

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); openFileDialog1.Filter = "txt file (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (cb_sortable.Checked) { header = "{| class=\"wikitable sortable\" border=\"1\" \r\n|+ Sortable table"; } using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) { string text = sr.ReadToEnd(); string[] cells = text.Split('\t'); int columnCount = 0; foreach (string cell in cells) { if (cell.Contains("\r\n")) { break; } columnCount++; } }

basically all I needs is a "split if not between \" " but im just at a loss right now

any tips or tricks would be greatly appreciated

解决方案

Checkout this project instead of rolling your own CSV parser.

更多推荐

简单的csv读卡器

本文发布于:2023-05-27 23:10:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/306083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!