Click here to Skip to main content
15,916,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert a textbox content of 'n' terms to csv format .

Please help me


I've done code in Console windows I want to get input from Textbox and do the same for converting the text in csv format



C#
string fileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\CSV.csv";

           TextWriter writer = new StreamWriter(fileName,true);

           List<string> stringList = new List<string>();
           Console.Write("Enter the number of elements in the list: ");
           int listSize;
           int.TryParse(Console.ReadLine(),out listSize);
           Console.WriteLine("Enter {0} elements one by one: ",listSize);
           for (int index = 0; index < listSize; index++)
           {
               stringList.Add(Console.ReadLine());
           }
           Console.Write("\nList elements:\n\n");
           foreach (var items in stringList)
           {
               Console.WriteLine(items);
           }
           string csvString = string.Join(",", stringList.ToArray());
           Console.WriteLine("\nCSV Output");
           Console.WriteLine("\n"+csvString);
           writer.WriteLine(csvString);
           writer.Close();
           Console.ReadLine();
Posted
Updated 23-Feb-14 23:44pm
v2
Comments
Kornfeld Eliyahu Peter 24-Feb-14 5:36am    
Have you done anything so far? Show some effort (code or searching)! As is it ain't a question...
KUMAR619 24-Feb-14 6:03am    
@Kornfeld
I have edited my code please help
Kornfeld Eliyahu Peter 24-Feb-14 6:06am    
Am I right that you want to turn this application into UI, instead of console?
KUMAR619 24-Feb-14 6:11am    
@Kornfeld
Yes Offcourse sir.
Can I get the code for it

// Write data to a .csv file
private void WriteToCSV(string data)
{
int length = data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length;

// List of lists containing each line split by part
List<list><string>> dataList = new List<list><string>>();

List<string> valuesA = new List<string>();

// Building the list
foreach (string line in data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
List<string> partsLine = new List<string>();
partsLine.AddRange(line.Split('\t'));
dataList.Add(partsLine);
}

const string separator = ";";

// Writing the list to the .csv file
try
{
using (StreamWriter writer = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\output.xls", false))
{
dataList.ForEach(line =>
{
var lineArray = line.Select(c => c.Contains(separator) ? c.Replace(separator.ToString(), "\\" + separator) : c).ToArray();
writer.WriteLine(string.Join(separator, lineArray));
});
}
}
catch (Exception ex)
{
Console.WriteLine("Error while writing data to .csv file (" + ex.Message + ")");
}
 
Share this answer
 
Comments
KUMAR619 24-Feb-14 6:39am    
@Praks
Thanks for your code
I would do it with a DataGridView:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button_AddColumn_Click(object sender, EventArgs e)
        {
            string colName = string.Format("col_{0}", this.dataGridView1.Columns.Count);
            this.dataGridView1.Columns.Add(colName, colName);
        }

        private void button_SaveAsCsv_Click(object sender, EventArgs e)
        {
            try
            {
                using (StreamWriter w = new StreamWriter(@"d:\tmp\1.csv"))
                {
                    w.Write(this.dataGridView1.ExportToCsv(this.checkBox_IncludeHeaders.Checked));
                }
                MessageBox.Show("saved");
            }
            catch (System.ObjectDisposedException ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
            catch (System.NotSupportedException ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
            catch (System.IO.IOException ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
        }
    }

    public static class Utils
    {
        public static string ExportToCsv(this DataGridView v, bool includeHeaders = false)
        {
            string s = string.Empty;

            if (includeHeaders)
            {
                int c = 0;
                for (; c < v.ColumnCount - 1; c++)
                    s += v.Columns[c].Name + ",";
                s += v.Columns[c].Name + Environment.NewLine;
            }

            for (int r = 0; r < v.Rows.Count; r++)
            {
                int c = 0;
                for (; c < v.Columns.Count - 1; c++)
                {
                    s += (v[c, r].Value ?? "null") + ",";
                }
                s += (v[c, r].Value ?? "null") + Environment.NewLine;
            }
            return s;
        }
    }
}
 
Share this answer
 
Comments
KUMAR619 24-Feb-14 6:56am    
@Vedat
Thanks for your code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900