public static bool ExportForListView(ListView listView, string fileName, bool isShowExcle)
{
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
try
{
string excel = ""; //用于存放要写入的一行文本。
for (int i = 0; i < listView.Columns.Count; i++)
{
excel = excel + listView.Columns[i].Text.ToString().Trim() + Convert.ToChar(9);
}
sw.WriteLine(excel); //写入DataGridView的标题行。
excel = "";
for (int i = 0; i < listView.Items.Count; i++)
{
for (int j = 0; j < listView.Columns.Count; j++)
{
if (listView.Items[i].SubItems[j].Text.ToString() == null)
excel = excel + "" + Convert.ToChar(9); //循环写入每一行
else
excel = excel + listView.Items[i].SubItems[j].Text.ToString() + Convert.ToChar(9);
}
sw.WriteLine(excel);
excel = "";
}
}
catch
{
return false;
}
finally
{
sw.Close();
fs.Close();
if (isShowExcle)
{
System.Diagnostics.Process.Start(fileName);
}
}
return true;
}
这个可能比较慢