C#如何保存listview中的内容
各位大虾,谁知道如何把listview中的内容按行保存下来到txt,word或者excel文件里?望高手指点一下,最好给段代码示例,谢谢!最好是用保存文件对话框,可以选择...
各位大虾,谁知道如何把listview中的内容按行保存下来到txt,word或者excel文件里?望高手指点一下,最好给段代码示例,谢谢!
最好是用保存文件对话框,可以选择保存文件路径,选择保存文件的类型,输入文件名 展开
最好是用保存文件对话框,可以选择保存文件路径,选择保存文件的类型,输入文件名 展开
2个回答
展开全部
listview可以显示好多的东西,纯文本的话好办,图片的话不好整
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.saveFileDialog1.Filter = "文本文件|*.txt|Excel表格|*.xls";
for (int i = 0; i < 100; i++)
{
this.listView1.Items.Add(i.ToString());
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("姓名" + i);
if (i % 2 == 0)
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("男");
else
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("女");
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("139999999" + i.ToString("00"));
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = this.saveFileDialog1.FileName;
FileInfo fi = new FileInfo(file);
StreamWriter sw = fi.CreateText();
sw.WriteLine("ID\tName\tSex\tTel");
for (int i = 0; i < this.listView1.Items.Count; i++)
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", listView1.Items[i].Text, listView1.Items[i].SubItems[1].Text, listView1.Items[i].SubItems[2].Text, listView1.Items[i].SubItems[3].Text));
}
sw.Close();
System.Diagnostics.Process.Start(file);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.saveFileDialog1.Filter = "文本文件|*.txt|Excel表格|*.xls";
for (int i = 0; i < 100; i++)
{
this.listView1.Items.Add(i.ToString());
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("姓名" + i);
if (i % 2 == 0)
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("男");
else
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("女");
this.listView1.Items[this.listView1.Items.Count - 1].SubItems.Add("139999999" + i.ToString("00"));
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = this.saveFileDialog1.FileName;
FileInfo fi = new FileInfo(file);
StreamWriter sw = fi.CreateText();
sw.WriteLine("ID\tName\tSex\tTel");
for (int i = 0; i < this.listView1.Items.Count; i++)
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", listView1.Items[i].Text, listView1.Items[i].SubItems[1].Text, listView1.Items[i].SubItems[2].Text, listView1.Items[i].SubItems[3].Text));
}
sw.Close();
System.Diagnostics.Process.Start(file);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询