1 Pluspunkt 0 Minuspunkte

Wie kann ich den Inhalt eines C# DataGridView in eine CSV Datei exportieren?

private void button1_Click(object sender, EventArgs e)
{
    string CsvFpath = @"C:\scanner\CSV-EXPORT.csv";
    try
    {
        System.IO.StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
        string columnHeaderText = "";
        int countColumn = scannerDataGridView.ColumnCount - 1;
        if (countColumn >= 0)
        {
            columnHeaderText = scannerDataGridView.Columns[0].HeaderText;
        }
        for (int i = 1; i <= countColumn; i++)
        {
            columnHeaderText = columnHeaderText + ',' + scannerDataGridView.Columns[i].HeaderText;
        }
        csvFileWriter.WriteLine(columnHeaderText);
        foreach (DataGridViewRow dataRowObject in scannerDataGridView.Rows)
        {
            if (!dataRowObject.IsNewRow)
            {
                string dataFromGrid = "";
                dataFromGrid = dataRowObject.Cells[0].Value.ToString();
                for (int i = 1; i <= countColumn; i++)
                {
                    dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();
                    csvFileWriter.WriteLine(dataFromGrid);
                }
            }
        }
        csvFileWriter.Flush();
        csvFileWriter.Close();
    }
    catch (Exception exceptionObject)
    {
        MessageBox.Show(exceptionObject.ToString());
    }
}

Dieser Code fügt leere Zeilen ein und ich weiß nicht wieso.

von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Mit diesem Code sollte der Export in eine SCV Datei gelingen.

private void exportToCsvButton_Click(object sender, EventArgs e)
{
    if (dataGridView1.Rows.Count > 0)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "CSV (*.csv)|*.csv";
        sfd.FileName = "Output.csv";
        bool fileError = false;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            if (File.Exists(sfd.FileName))
            {
                try
                {
                    File.Delete(sfd.FileName);
                }
                catch (IOException ex)
                {
                    fileError = true;
                    MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);
                }
            }
            if (!fileError)
            {
                try
                {
                    int columnCount = dataGridView1.Columns.Count;
                    string columnNames = "";
                    string[] outputCsv = new string[dataGridView1.Rows.Count + 1];
                    for (int i = 0; i < columnCount; i++)
                    {
                        columnNames += dataGridView1.Columns[i].HeaderText.ToString() + ";";
                    }
                    outputCsv[0] += columnNames;
                    for (int i = 1; (i - 1) < dataGridView1.Rows.Count; i++)
                    {
                        for (int j = 0; j < columnCount; j++)
                        {
                            outputCsv[i] += dataGridView1.Rows[i - 1].Cells[j].Value.ToString() + ";";
                        }
                    }
                    File.WriteAllLines(sfd.FileName, outputCsv, Encoding.UTF8);
                    MessageBox.Show("Data Exported Successfully !!!", "Info");

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error :" + ex.Message);
                }
            }
        }
    }
    else
    {
        MessageBox.Show("No Record To Export !!!", "Info");
    }
}
von (410 Punkte)