EXCEL常用的类

using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.OleDb;
None.gif
None.gif
namespace  my
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Excel 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Excel
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Excel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static DataSet SelectExcel_AllSheet1(string path,string condition)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strConn;
InBlock.gif            strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
InBlock.gif                
"Data Source="+path+";"+
InBlock.gif                
"Extended Properties=Excel 8.0;";
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(strConn);
InBlock.gif            OleDbDataAdapter myCommand 
= new OleDbDataAdapter("select * FROM [Sheet1$] "+condition, strConn);
InBlock.gif            DataSet myDataSet 
= new DataSet();
InBlock.gif            myCommand.Fill(myDataSet);
InBlock.gif            
return myDataSet;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static DataSet SelectExcel(string path,string sqltext)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strConn;
InBlock.gif            strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
InBlock.gif                
"Data Source="+path+";"+
InBlock.gif                
"Extended Properties=Excel 8.0;";
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(strConn);
InBlock.gif            OleDbDataAdapter myCommand 
= new OleDbDataAdapter(sqltext, strConn);
InBlock.gif            DataSet myDataSet 
= new DataSet();
InBlock.gif            myCommand.Fill(myDataSet);
InBlock.gif            
return myDataSet;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//获取EXCEL文件的表名
InBlock.gif
        public static string[] ObtainTableName(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif            
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";Extended Properties=Excel 8.0;";
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(strConn);
InBlock.gif            conn.Open();
InBlock.gif            DataTable dt 
= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
InBlock.gif            
int i = dt.Rows.Count;
InBlock.gif            
string[] strTableName=new string[i];
InBlock.gif            
for(int s=0;s<i;s++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTableName[s] 
= dt.Rows[s]["TABLE_NAME"].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strTableName;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif        
InBlock.gif        
InBlock.gif        
public static bool DataSetToExcelText(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result=false;
InBlock.gif            
InBlock.gif            
if (ds.Tables[0]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string filename=OpenSaveDialog();
InBlock.gif                
if ((filename!=null&& (filename!=""))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    StreamWriter sw
=new StreamWriter(filename,false,Encoding.Unicode);
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
//                        StreamWriter sw=new StreamWriter(filename,false,Encoding.Unicode);
InBlock.gif
                        int i=0;
InBlock.gif                        
int j=0;
InBlock.gif                        j
=ds.Tables[0].Columns.Count;
InBlock.gif                        
//文件列头
InBlock.gif
                        string s="";
InBlock.gif                        
while (i<j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            s
=s+ds.Tables[0].Columns[i].ColumnName+"\t";
InBlock.gif                            i
=i+1;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        sw.WriteLine(s);
InBlock.gif                        
//写数据
InBlock.gif
                        foreach (DataRow r in ds.Tables[0].Rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            i
=0;
InBlock.gif                            s
="";
InBlock.gif                            
while (i<j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if (r[i].ToString().Trim()=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    s
=s+" "+"\t";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    s
=s+r[i].ToString().Trim()+"\t";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                i
=i+1;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            sw.WriteLine(s);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        result
=true;
InBlock.gif                        MessageBox.Show(
"导出成功!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        result
=false;
InBlock.gif                        MessageBox.Show(
"导出失败!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sw.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif                                   
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool DataSetToExcelText(DataTable dt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result=false;
InBlock.gif            
InBlock.gif            
if (dt!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string filename=OpenSaveDialog();
InBlock.gif                
if ((filename!=null&& (filename!=""))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    StreamWriter sw
=new StreamWriter(filename,false,Encoding.Unicode);
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//                        StreamWriter sw=new StreamWriter(filename,false,Encoding.Unicode);
InBlock.gif
                        int i=0;
InBlock.gif                        
int j=0;
InBlock.gif                        j
=dt.Columns.Count;
InBlock.gif                        
//文件列头
InBlock.gif
                        string s="";
InBlock.gif                        
while (i<j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            s
=s+dt.Columns[i].ColumnName+"\t";
InBlock.gif                            i
=i+1;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        sw.WriteLine(s);
InBlock.gif                        
//写数据
InBlock.gif
                        foreach (DataRow r in dt.Rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            i
=0;
InBlock.gif                            s
="";
InBlock.gif                            
while (i<j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if (r[i].ToString().Trim()=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    s
=s+" "+"\t";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    s
=s+r[i].ToString().Trim()+"\t";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                i
=i+1;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            sw.WriteLine(s);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        result
=true;
InBlock.gif                        MessageBox.Show(
"导出成功!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        result
=false;
InBlock.gif                        MessageBox.Show(
"导出失败!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sw.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif                                   
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static string OpenSaveDialog()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string result="";
InBlock.gif            SaveFileDialog sfd
=new SaveFileDialog();
InBlock.gif            sfd.Filter
="Excel文件(*.xls)|*.xls|文本文件(*.txt)|*.txt";
InBlock.gif            sfd.OverwritePrompt
=true;
InBlock.gif            
if (sfd.ShowDialog()==DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result
=sfd.FileName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
 

转载于:https://www.cnblogs.com/hzuIT/articles/793094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值