【发布时间】:2017-04-05 04:38:15
【问题描述】:
我想将标题添加到我的 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("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();
}
更新尝试:
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();
}
}
我真的很想尝试在没有 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" + "," + "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();
}
}
}
【问题讨论】:
-
关于 WriteAllText 的 MSDN :如果目标文件已经存在,则将其覆盖。 所以上面的代码总是重写所有标题或其他内容。
-
其实没有。我导出到文件并将其附加到现有数据中,因此您实际上对此有误。隐藏的文本框是 @Steve 的解决方法
-
@RezaAghaei 如果我按照我在原始问题的更新尝试中的建议检查它是否存在,该怎么办
-
史蒂夫没有错,他实际上是直接从文档中复制/粘贴的。 WriteAllText 覆盖文件。当然,您正在通过使用一串文本框来保留附加文本来解决这个问题,但最后,当您调用该方法时,您正在替换整个文件,而不是附加到它。
-
显然你并没有意识到这一点,否则你不会写这个“实际上不,它没有”来回应史蒂夫的评论。在解决这个问题方面,我可能会提供一些东西,但必须指出这一点,因为当那个人实际上完全正确时,你公然告诉某人是错的。