本文介绍了简单的csv读卡器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 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 =| - ; 但可以移动单元格空白单元格)(尚未完成) 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 throughI have 3 main problems1) some of the cells contain \r\n ( so when reading line by line this treats each new line as a new cell2) 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 aboveI'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 workingAtempt 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 nowany tips or tricks would be greatly appreciated 解决方案 Checkout this project instead of rolling your own CSV parser. 这篇关于简单的csv读卡器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 03:08