面代码演示了如何将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, " ;
}
{
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, " ;
}