这就是我想出的。这个对我有用。由于注释和添加的新行,它看起来很长,因此在保存时它在文本文件中看起来不错。
// Save File Button
private void save_button_Click(object sender, EventArgs e)
{
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine + Environment.NewLine);
// Columns NAMES
for (int c = 0; c < timeTable_listView.Columns.Count; c++)
{
System.IO.File.AppendAllText(@"C:\bbbb.txt", timeTable_listView.Columns[c].Text+"\t");
}
// Add new Line int the txt file
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine + Environment.NewLine);
// Get all Items "loop All items"
for (int i = 0; i < timeTable_listView.Items.Count; i++)
{
// Loop all Sub Items of the Current Item "[i]"
for (int a = 0; a < timeTable_listView.Items[i].SubItems.Count; a++)
{
// Write the sub item "[a]" to the file with "tab betwen each item"
System.IO.File.AppendAllText(@"C:\bbbb.txt", timeTable_listView.Items[i].SubItems[a].Text + "\t");
}
// Add new Line "the new Row" starts at new line
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine);
}
}
图片:表格
图片:TXT 文件
仅代码:------------------------------------------------------------ -----------------
private void save_button_Click(object sender, EventArgs e)
{
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine + Environment.NewLine);
for (int c = 0; c < timeTable_listView.Columns.Count; c++)
{
System.IO.File.AppendAllText(@"C:\bbbb.txt", timeTable_listView.Columns[c].Text+"\t");
}
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine + Environment.NewLine);
for (int i = 0; i < timeTable_listView.Items.Count; i++)
{
for (int a = 0; a < timeTable_listView.Items[i].SubItems.Count; a++)
{
System.IO.File.AppendAllText(@"C:\bbbb.txt", timeTable_listView.Items[i].SubItems[a].Text + "\t");
}
System.IO.File.AppendAllText(@"C:\bbbb.txt", Environment.NewLine);
}
}