prepend headers to my csv file

前端 未结 4 436
长发绾君心
长发绾君心 2021-01-29 07:18

I want to prepend headers to my CSV File as to let the data reflect the headings. How would I go about this without having to add it each time writing to the file? Meaning I onl

相关标签:
4条回答
  • 2021-01-29 07:54
    for (int row_index = 0; row_index <= count_row - 2; row_index++)
    {
        textBox8.Text = textBox8.Text + "\r\n";
    
        for (int cell_index = 1; cell_index <= count_cell - 1; cell_index++)
        {
            textBox8.Text = textBox8.Text + dataGridView1.Rows[row_index].Cells[cell_index].Value.ToString() + ",";
    
        }
    }
    

    Just go to the property pannel of textBox8. Text and add column header with comma and set the textbox8 visibility to hidden.

    0 讨论(0)
  • 2021-01-29 07:58

    This is my suggested solution. My suggestion is to first check if the file exists or not, to then decide if you need to write the header or not.

    private void DoTheWork(int fileIDtoUpdate)
        {
            //this is just my representation of what probably already exist in your project
            string textInTheTextBox = "blah blah blah blah\nI love text\nI love code\nI love to Code\ndon't you just love to code!";
            string filePath1 = @"M:\StackOverflowQuestionsAndAnswers\40726017\File1.txt";
            string filePath2 = @"M:\StackOverflowQuestionsAndAnswers\40726017\File2.txt";
            string filePath3 = @"M:\StackOverflowQuestionsAndAnswers\40726017\File3.txt";
            string filePath4 = @"M:\StackOverflowQuestionsAndAnswers\40726017\File4.txt";
            string fileToWorkWith = string.Empty;
            //decide which file to work with
            switch (fileIDtoUpdate)
            {
                case 1:
                    fileToWorkWith = filePath1;
                    break;
                case 2:
                    fileToWorkWith = filePath2;
                    break;
                case 3:
                    fileToWorkWith = filePath3;
                    break;
                case 4:
                    fileToWorkWith = filePath4;
                    break;
                default:
                    break;
            }
            //check if the file existed
            bool fileExisted = File.Exists(fileToWorkWith);
            using (StreamWriter sw = new StreamWriter(fileToWorkWith, true))
            {
                if (!fileExisted)
                {
                    //if the file did not exist, then you need to put your header line!
                    sw.WriteLine("Write your Header Line here");
                }
                sw.WriteLine(textInTheTextBox);//down here... who cares if the file existed or not, you need to append this text to it no matter what!
            }
        }
    
    0 讨论(0)
  • 2021-01-29 08:11

    Got the answer by simply editing my code with this snippet:

     if (!File.Exists(path))
        {
            System.IO.File.WriteAllText(path, rxHeader + textBox8.Text);
        }
        else
        {    
            System.IO.File.AppendAllText(path, textBox8.Text);
            MessageBox.Show("Export  of " + comboBox5.Text + " table is complete!");
            textBox8.Clear();
        }
    
    0 讨论(0)
  • 2021-01-29 08:12

    Here is the short version that will even properly handle values that contain , and ":

    dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
    dataGridView1.RowHeadersVisible = false;  // the row headers column is copied too if visible
    dataGridView1.SelectAll();                // only the selected cells are used (the Windows Clipboard is not used)
    
    DataObject dataObject = dataGridView1.GetClipboardContent();      // 4 Data Formats: Text,Csv,HTML Format,UnicodeText
    File.WriteAllText("1.csv", dataObject.GetData("Csv") as string);  // DataFormats.CommaSeparatedValue = "Csv"
    
    //string html = Encoding.ASCII.GetString((dataObject.GetData("HTML Format") as MemoryStream).ToArray()); // just the HTML Clipboard Format is in a MemoryStream
    
    0 讨论(0)
提交回复
热议问题