前缀头到我的csv文件 [英] prepend headers to my csv file

查看:140
本文介绍了前缀头到我的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将标头加到我的CSV档案中,让资料反映标题。我怎么会这样,而不必每次写入文件添加它?含义我只想要在每次导出时添加一次标题。当导出到相同的文件名时,它不应该创建相同标题的重复。这里是我的代码下面写入文件:

  private void button6_Click_2(object sender,EventArgs e)
{

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

MessageBox.Show(请稍候... + comboBox5.Text +表正在导出..);
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\\\
;
}
System.IO.File.WriteAllText(C:\\Users\\jdavis\\Desktop\\+ comboBox5.Text +。csv,textBox8 。文本);
MessageBox.Show(Export of+ comboBox5.Text +table is complete!);
textBox8.Clear();
}

更新的尝试:

  private void button6_Click_2(object sender,EventArgs e)
{

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

MessageBox.Show(请稍候... + comboBox5.Text +表正在导出..);

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

string rxHeader =Code+,+ ++,+UOM+ Environment.NewLine;+,+,+,+NDC+,+供应商代码
+,


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\\\
;
}
System.IO.File.WriteAllText(C:\\Users\\jdavis\\Desktop\\+ comboBox5.Text +.csv, textBox8.Text);
MessageBox.Show(Export of+ comboBox5.Text +table is complete!);
textBox8.Clear();
}

}

我真的想尝试,它没有SteamWriter,我在哪里错了?

  private void button6_Click_2(object sender,EventArgs e)
{

int count_row = dataGridView1.RowCount;
int count_cell = dataGridView1.Rows [0] .Cells.Count;
string path =C:\\Users\\jdavis\\Desktop\\+ comboBox5.Text +.csv;
string rxHeader =Code+,+说明+,+NDC+,+供应商代码
+, ,+包大小+,+UOM;


MessageBox.Show(请稍候... + comboBox5.Text +表正在导出...);

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\\\
;

if(!File.Exists(path))
{
System.IO.File.WriteAllText(path,rxHeader);
System.IO.File.WriteAllText(path,textBox8.Text);
}
else
{
System.IO.File.AppendAllText(path,textBox8.Text);
MessageBox.Show(Export of+ comboBox5.Text +table is complete!);
textBox8.Clear();
}
}
}


解决方案>

通过使用此代码片段编辑我的代码,得到答案:

  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();
}


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();
        }

    }

I really want to try and achieve it without SteamWriter, where am I going wrong?

private void button6_Click_2(object sender, EventArgs e)
    {

        int count_row = dataGridView1.RowCount;
        int count_cell = dataGridView1.Rows[0].Cells.Count;
        string path = "C:\\Users\\jdavis\\Desktop\\" + comboBox5.Text + ".csv";
        string rxHeader = "Code" + "," + "Description" + "," + "NDC" + "," + "Supplier Code"
        + "," + "Supplier Description" + "," + "Pack Size" + "," + "UOM";


        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";

            if (!File.Exists(path))
            {
                System.IO.File.WriteAllText(path, rxHeader);
                System.IO.File.WriteAllText(path, textBox8.Text);
            }
            else
            {    
                System.IO.File.AppendAllText(path, textBox8.Text);
                MessageBox.Show("Export  of " + comboBox5.Text + " table is complete!");
                textBox8.Clear();
            }
        }
    }

解决方案

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();
    }

这篇关于前缀头到我的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆