helloyesyes 发表于 2013-2-1 09:51:43

C# 将ListView数据导出到Excel文本之方法

下面代码演示了如何将ListView中的数据导出到Excel的方法,例子代码中还包括了一些编程中的其它小方法,比如:
1)文件的拷贝复制方法
2)文件属性的修改
3)ListView控件Columns的遍历
4)ListView控件Items的遍历
5)foreach语句的使用
6)SaveFileDialog的使用
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublicvoidDealExcelOut()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringpath=Application.StartupPath;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringpath1=path+"\tmp.xls";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringpath2=path+"\tmp2.xls";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifFile.Copy(path1,path2,true);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifFile.SetAttributes(path2,FileAttributes.Normal);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringstrconn="Provider=Microsoft.jet.OLEDB.4.0;DataSource="+path2+";ExtendedProperties=Excel8.0";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifOleDbConnectioncn=newOleDbConnection(strconn);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifOleDbCommandcmd=newOleDbCommand();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringcmdstr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//根据ListView创建VPN表
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftry
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcn.Open();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmd.Connection=cn;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr="CreateTableVPN(";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifforeach(ColumnHeaderchinlistView_Main.Columns)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr+=ch.Text+"TEXT,";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr=cmdstr.Remove(cmdstr.Length-1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr+=")";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmd.CommandText=cmdstr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmd.ExecuteNonQuery();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcatch(Exception)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifMessageBox.Show("读取Excel模板文件错误!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//导出数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftry
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflongcols=listView_Main.Columns.Count;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifforeach(ListViewItemlviinlistView_Main.Items)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr="InsertIntoVPNValues(";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(longi=0;i<cols;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr+="'"+lvi.SubItems[(Int32)i].Text+"',";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr=cmdstr.Remove(cmdstr.Length-1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmdstr+=")";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmd.CommandText=cmdstr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcmd.ExecuteNonQuery();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcn.Close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//显示FileSave对话框,复制临时文件到指定文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifDialogResultr=saveFileDialog_Excel.ShowDialog();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(r==DialogResult.OK)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifFile.Copy(path2,saveFileDialog_Excel.FileName,true);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifFile.Delete(path2);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcatch(Exception)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifMessageBox.Show("访问Excel文件错误!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: C# 将ListView数据导出到Excel文本之方法