如何将ListView中的数据导出到Excel
来源:互联网 发布:node.js restful 框架 编辑:程序博客网 时间:2024/05/21 17:48
下面代码演示了如何将ListView中的数据导出到Excel的方法,例子代码中还包括了一些编程中的其它小方法,比如:
1)文件的拷贝复制方法
2)文件属性的修改
3)ListView控件Columns的遍历
4)ListView控件Items的遍历
5)foreach语句的使用
6)SaveFileDialog的使用
public void DealExcelOut()
...{
String path = Application.StartupPath;
String path1 = path + "/tmp.xls";
String path2 = path + "/tmp2.xls";
File.Copy(path1, path2, true);
File.SetAttributes(path2, FileAttributes.Normal);
String strconn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" + path2 + "; Extended Properties = Excel 8.0";
OleDbConnection cn = new OleDbConnection(strconn);
OleDbCommand cmd = new OleDbCommand();
String cmdstr;
//根据ListView创建VPN表
try
...{
cn.Open();
cmd.Connection = cn;
cmdstr = "Create Table VPN (";
foreach (ColumnHeader ch in listView_Main.Columns)
...{
cmdstr += ch.Text + " TEXT,";
}
cmdstr = cmdstr.Remove(cmdstr.Length - 1);
cmdstr += ")";
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
catch (Exception)
...{
MessageBox.Show("读取Excel模板文件错误!");
}
//导出数据
try
...{
long cols = listView_Main.Columns.Count;
foreach (ListViewItem lvi in listView_Main.Items)
...{
cmdstr = "Insert Into VPN Values (";
for (long i = 0; i < cols; i++)
cmdstr += "'" + lvi.SubItems[(Int32)i].Text + "',";
cmdstr = cmdstr.Remove(cmdstr.Length - 1);
cmdstr += ")";
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
cn.Close();
//显示FileSave对话框,复制临时文件到指定文件
DialogResult r = saveFileDialog_Excel.ShowDialog();
if (r == DialogResult.OK)
...{
File.Copy(path2, saveFileDialog_Excel.FileName, true);
}
File.Delete(path2);
}
catch (Exception)
...{
MessageBox.Show("访问Excel文件错误!");
}
}
...{
String path = Application.StartupPath;
String path1 = path + "/tmp.xls";
String path2 = path + "/tmp2.xls";
File.Copy(path1, path2, true);
File.SetAttributes(path2, FileAttributes.Normal);
String strconn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" + path2 + "; Extended Properties = Excel 8.0";
OleDbConnection cn = new OleDbConnection(strconn);
OleDbCommand cmd = new OleDbCommand();
String cmdstr;
//根据ListView创建VPN表
try
...{
cn.Open();
cmd.Connection = cn;
cmdstr = "Create Table VPN (";
foreach (ColumnHeader ch in listView_Main.Columns)
...{
cmdstr += ch.Text + " TEXT,";
}
cmdstr = cmdstr.Remove(cmdstr.Length - 1);
cmdstr += ")";
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
catch (Exception)
...{
MessageBox.Show("读取Excel模板文件错误!");
}
//导出数据
try
...{
long cols = listView_Main.Columns.Count;
foreach (ListViewItem lvi in listView_Main.Items)
...{
cmdstr = "Insert Into VPN Values (";
for (long i = 0; i < cols; i++)
cmdstr += "'" + lvi.SubItems[(Int32)i].Text + "',";
cmdstr = cmdstr.Remove(cmdstr.Length - 1);
cmdstr += ")";
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
cn.Close();
//显示FileSave对话框,复制临时文件到指定文件
DialogResult r = saveFileDialog_Excel.ShowDialog();
if (r == DialogResult.OK)
...{
File.Copy(path2, saveFileDialog_Excel.FileName, true);
}
File.Delete(path2);
}
catch (Exception)
...{
MessageBox.Show("访问Excel文件错误!");
}
}
- 如何将ListView中的数据导出到Excel
- C#如何将ListView中的数据导出到Excel中
- 将ListView控件中的数据导出到Excel文
- C# 将listview 中的数据导出到excel 文件
- 将ListView中的数据导出到Excel中
- 如何将DataGrid中的数据导出到Excel文件中
- 如何将DataGrid中的数据导出到Excel文件中
- 如何将datagridview中的数据导出到Excel中(转)
- 如何将datagridview中的数据导出到Excel中
- C#中如何将DataTable中的数据导出到Excel
- SQLyog如何将mysql中的表数据导出到Excel
- 将DataGridView中的数据导出到Excel
- C# 将ListView数据导出到Excel文本之方法
- 通过OLE将ListView数据导出到Excel
- C# 将ListView数据导出到Excel文本之方法
- C# 将ListView数据导出到Excel文本之方法
- winform将listview数据导出到excel中
- 将ListView中的内容导出到Word和Excel
- 利用脚本加入域和退出域
- MyEclipse使用快捷键
- CSDN下载频道积分规则2.1
- MyEclipse 6 Java EE 开发中文手册
- 在C#中运用SQLDMO备份和恢复SQLServer数据库
- 如何将ListView中的数据导出到Excel
- 演说之禅:演说中的 1-7-7法则
- 一个可以开机后自动和你打招呼的小程序(希望以后可以成为智能的系统)
- Environment Object
- ListView的Column排序方法,按列排序
- ubuntu 分辨率
- cuteEditor 编码问题(使用前必看)
- C#调用API的一些基础
- FasTime.Data.dll V2.6.5.1 数据库操作组件