Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace Bankomat
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string path = "";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- try {
- path = openFileDialog1.FileName;
- List<string> rows = File.ReadAllLines(path).ToList();
- DataTable dt = new DataTable();
- string[] header = rows[0].Split(',');
- foreach (var item in header)
- {
- dt.Columns.Add(item);
- }
- rows.RemoveAt(0); // удалим Хедер
- string[] fields = null;
- List<string> lstFields;
- string field;
- string tmp;
- foreach (string csvRownt in rows)
- {
- int InQuote = 0;
- string csvRow = csvRownt.Trim('\"');//.Replace("\"\"", "\"");
- lstFields = new List<string>();
- field = "";
- for (int i = 0; i < csvRow.Length; i++)
- {
- tmp = csvRow.ElementAt(i).ToString();
- if (tmp == "\"") { InQuote = InQuote + 1; }
- else
- {
- switch (InQuote % 2)
- {
- case 1: // в кавычках
- {
- field += tmp;
- }
- break;
- case 0: // не в кавычках
- {
- if (tmp == ",") { lstFields.Add(field); field = ""; }
- else field += tmp;
- }
- break;
- }
- }
- }
- if (!string.IsNullOrEmpty(field))
- {
- lstFields.Add(field);
- field = "";
- }
- fields = lstFields.ToArray();
- dt.Rows.Add(fields);
- }
- dataGridView1.DataSource = dt;
- }
- catch (Exception ex) { MessageBox.Show(ex.Message); }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement