CSV-Editor

Benötigte Controlls

  • Button „btn_oeffnen“
  • Button „btn_speichern“
  • dataGridView „dataGridView1“
  • openFileDialog „openFileDialog1“
  • saveFileDialog „saveFileDialog1“

Quellcode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CSV_Editor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Clear_all()
        {
            dataGridView1.Columns.Clear();
        }

        void ReadCSV(String file)
        {
            Clear_all();
            String rowValue;
            string[] cellValue;

            if(System.IO.File.Exists (file))
            {
                StreamReader streamReader = new StreamReader (file);
                rowValue = streamReader.ReadLine();
                cellValue = rowValue.Split(';');

                for (int i = 0; i <= cellValue.Count() - 1; i++)
                {
                    DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

                    column.Name = cellValue[i];
                    column.HeaderText = cellValue[i];
                    dataGridView1.Columns.Add(column);
                }

                while (streamReader.Peek() != -1)
                {
                    rowValue = streamReader.ReadLine();
                    cellValue = rowValue.Split(';');
                    dataGridView1.Rows.Add(cellValue);
                }

                streamReader.Close();
            }
        }

        void SaveCSV(String file)
        {
            StreamWriter streamWriter = new StreamWriter(file);
            string strHeader = "";

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                strHeader += dataGridView1.Columns[i].HeaderText + ";";
            }

            strHeader = strHeader.TrimEnd(';');

            streamWriter.WriteLine(strHeader);

            for (int m = 0; m < dataGridView1.Rows.Count - 1; m++)
            {
                string strRowValue = "";

                for (int n = 0; n < dataGridView1.Columns.Count; n++)
                {
                    strRowValue += dataGridView1.Rows[m].Cells[n].Value + ";";
                }
                strRowValue = strRowValue.TrimEnd(';');
                streamWriter.WriteLine(strRowValue);
            }
            streamWriter.Close();
        }

        private void btn_oeffnen_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            ReadCSV(openFileDialog1.FileName);
        }

        private void btn_speichern_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            SaveCSV(saveFileDialog1.FileName);
        }

    }
}

Beispielinhalt einer .csv-Datei

Name;Vorname;Postleitzahl;Hausnummer
Name1;Vorname1;12345;32
Name2;Vorname2;17496;8

Kommentar verfassen

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close