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读卡器
发布评论