Exemplo n.º 1
0
        private string GetCsvExportData(frmCsvExport exporter)
        {
            StringBuilder sb = new StringBuilder();
            if (exporter.IncludeHeaders)
            {
                string headerDomain = "Domain";
                string headerResult = "Results";

                if (exporter.ExportAllData)
                {
                    if (exporter.EmbedInDoubleQuotes)
                    {
                        sb.AppendLine(String.Format("\"{0}\"{1}\"{2}\"", headerDomain, exporter.Delimiter, headerResult));
                    }
                    else
                    {
                        sb.AppendLine(String.Format("{0}{1}{2}", headerDomain, exporter.Delimiter, headerResult));
                    }
                }
                else
                {
                    if (exporter.EmbedInDoubleQuotes)
                    {
                        sb.AppendLine(String.Format("\"{0}\"", headerResult));
                    }
                    else
                    {
                        sb.AppendLine(headerResult);
                    }
                }
            }
            foreach (var d in domains)
            {
                if (exporter.ExportAllData)
                {
                    sb.AppendLine(d.GetDomainAndResultAsCSV(exporter.EmbedInDoubleQuotes, exporter.Delimiter));
                }
                else
                {
                    sb.AppendLine(d.GetResultsAsCSV(exporter.EmbedInDoubleQuotes));
                }
            }
            return sb.ToString();
        }
Exemplo n.º 2
0
 private void exportToolStripMenuItem_Click(object sender, EventArgs e)
 {
     frmCsvExport exporter = new frmCsvExport();
     if (exporter.ShowDialog() == DialogResult.OK)
     {
         string data = GetCsvExportData(exporter);
         if (data.Length == 0)
         {
             MessageBox.Show("No data to export!", "Error");
         }
         else
         {
             if (exporter.ExportToClipboard)
             {
                 Clipboard.SetText(GetCsvExportData(exporter));
                 MessageBox.Show("The data is now available in the clipboard.");
             }
             else
             {
                 SaveFileDialog sfd = new SaveFileDialog();
                 sfd.AddExtension = true;
                 sfd.DefaultExt = ".csv";
                 sfd.Filter = "CSV (Comma separated) (*.csv)|*.csv|All files (*.*)|*.*";
                 if (sfd.ShowDialog() == DialogResult.OK)
                 {
                     try
                     {
                         using (StreamWriter sw = new StreamWriter(sfd.OpenFile()))
                         {
                             sw.Write(GetCsvExportData(exporter));
                         }
                     }
                     catch (Exception ex)
                     {
                         MessageBox.Show("Error exporting data!" + Environment.NewLine + ex.Message, "Error");
                     }
                 }
             }
         }
     }
 }