Exporting datagridview to csv file

后端 未结 8 1806
南方客
南方客 2020-11-29 10:11

I\'m working on a application which will export my DataGridView called scannerDataGridView to a csv file.

Found some example code to do this, but can\'t get it worki

相关标签:
8条回答
  • 2020-11-29 10:57
          Please check this code.its working fine  
    
              try
                   {
                //Build the CSV file data as a Comma separated string.
                string csv = string.Empty;
    
                //Add the Header row for CSV file.
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    csv += column.HeaderText + ',';
                }
                //Add new line.
                csv += "\r\n";
    
                //Adding the Rows
    
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                        {
                            //Add the Data rows.
                            csv += cell.Value.ToString().TrimEnd(',').Replace(",", ";") + ',';
                        }
                        // break;
                    }
                    //Add new line.
                    csv += "\r\n";
                }
    
                //Exporting to CSV.
                string folderPath = "C:\\CSV\\";
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                File.WriteAllText(folderPath + "Invoice.csv", csv);
                MessageBox.Show("");
            }
            catch
            {
                MessageBox.Show("");
            }
    
    0 讨论(0)
  • 2020-11-29 10:59

    I know this is probably already dead but I made a simple code that creates a CSV based on a DataGridView object and asks where you want to save it.

    Its pretty straight forward, just paste de function and use it. It already has some basic exception handling so is pretty safe to use. enjoy.

        private void SaveToCSV(DataGridView DGV)
        {
            string filename = "";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV (*.csv)|*.csv";
            sfd.FileName = "Output.csv";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("Data will be exported and you will be notified when it is ready.");
                if (File.Exists(filename))
                {
                    try
                    {
                        File.Delete(filename);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);
                    }
                }
                int columnCount = DGV.ColumnCount;
                string columnNames = "";
                string[] output = new string[DGV.RowCount + 1];
                for (int i = 0; i < columnCount; i++)
                {
                    columnNames += DGV.Columns[i].Name.ToString() + ",";
                }
                output[0] += columnNames;
                for (int i = 1; (i - 1) < DGV.RowCount; i++)
                {
                    for (int j = 0; j < columnCount; j++)
                    {
                        output[i] += DGV.Rows[i - 1].Cells[j].Value.ToString() + ",";
                    }
                }
                System.IO.File.WriteAllLines(sfd.FileName, output, System.Text.Encoding.UTF8);
                MessageBox.Show("Your file was generated and its ready for use.");
            }
        }
    

    EDIT: changed ';' to ',' since we are talking about CSV files

    0 讨论(0)
提交回复
热议问题