Jevon Jevon - 12 days ago 11
C# Question

prepend headers to my csv file

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 only want the headers added once on each export. When am exporting to the same file name it should not create duplicates of the same headers. Here is my code below which writes to file:

private void button6_Click_2(object sender, EventArgs e)
{

int count_row = dataGridView1.RowCount;
int count_cell = dataGridView1.Rows[0].Cells.Count;

MessageBox.Show("Please wait while " +comboBox5.Text+ " table is being exported..");
for (int row_index = 0; row_index <= count_row - 2; row_index++)
{

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() + ",";

}
textBox8.Text = textBox8.Text + "\r\n";
}
System.IO.File.WriteAllText("C:\\Users\\jdavis\\Desktop\\"+comboBox5.Text+".csv", textBox8.Text);
MessageBox.Show("Export of " +comboBox5.Text+ " table is complete!");
textBox8.Clear();
}


Updated attempt:

private void button6_Click_2(object sender, EventArgs e)
{

int count_row = dataGridView1.RowCount;
int count_cell = dataGridView1.Rows[0].Cells.Count;

MessageBox.Show("Please wait while " + comboBox5.Text + " table is being exported..");

if (!File.Exists(comboBox5.Text))
{

string rxHeader = "Code" + "," + "Description" + "," + "NDC" + "," + "Supplier Code"
+ "," + "Supplier Description" + "," + "Pack Size" + "," + "UOM" + Environment.NewLine;


for (int row_index = 0; row_index <= count_row - 2; row_index++)
{

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() + ",";

}
textBox8.Text = textBox8.Text + "\r\n";
}
System.IO.File.WriteAllText("C:\\Users\\jdavis\\Desktop\\" + comboBox5.Text + ".csv", textBox8.Text);
MessageBox.Show("Export of " + comboBox5.Text + " table is complete!");
textBox8.Clear();
}

}

Answer

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!
        }
    }
Comments