Example #1
0
        private void btnToCSV_Click(object sender, EventArgs e)
        {
            try
            {
                // Displays a SaveFileDialog so the user can save the Image
                // assigned to Button2.
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "CSV|*.csv";
                saveFileDialog1.Title = "Guardar como csv...";
                saveFileDialog1.ShowDialog();

                if (saveFileDialog1.FileName != "")
                {
                    Cursor = Cursors.WaitCursor;

                    System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
                    fs.Close();

                    CsvFileWriter file = new CsvFileWriter(saveFileDialog1.FileName);

                    CsvRow header = new CsvRow();

                    foreach (DataGridViewColumn col in dgvParte4Final.Columns)
                    {
                        header.Add(col.Name);
                    }

                    file.WriteRow(header);

                    foreach (Source.citParte4Final item in _result)
                    {
                        file.WriteRow(item.ToCsv());
                    }

                    file.Flush();
                    file.Close();

                    MessageBox.Show("Ficheiro guardado.");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro\n" + ex.Message);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
 private static void CerrarArchivo(CsvFileWriter writer)
 {
     writer.Close();
     writer.Dispose();
 }