ExcelHelper代码

ExcelHelper代码
ExcelHelper代码
using  System.Data.OleDb;
ExcelHelper代码
ExcelHelper代码
using  System; 
ExcelHelper代码
using  System.IO; 
ExcelHelper代码
using  System.Data; 
ExcelHelper代码
using  System.Text; 
ExcelHelper代码
using  System.Collections; 
ExcelHelper代码
ExcelHelper代码
namespace  Wxy.Util
ExcelHelper代码
{
ExcelHelper代码    
/// <summary>
ExcelHelper代码    
/// ExcelHelper 的摘要说明。
ExcelHelper代码    
/// </summary>

ExcelHelper代码    public class ExcelHelper
ExcelHelper代码    
{
ExcelHelper代码        
public ExcelHelper()
ExcelHelper代码        
{
ExcelHelper代码            
//
ExcelHelper代码            
// TODO: 在此处添加构造函数逻辑
ExcelHelper代码            
//
ExcelHelper代码
        }

ExcelHelper代码
ExcelHelper代码        
/// <summary>
ExcelHelper代码        
/// 读取Excel文档
ExcelHelper代码        
/// </summary>
ExcelHelper代码        
/// <param name="Path">文件名称</param>
ExcelHelper代码        
/// <returns>返回一个数据集</returns>

ExcelHelper代码        public static DataSet ExcelToDS(string Path)
ExcelHelper代码        
{
ExcelHelper代码            
try
ExcelHelper代码            
{
ExcelHelper代码                
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
ExcelHelper代码                OleDbConnection conn 
= new OleDbConnection(strConn);
ExcelHelper代码                conn.Open();  
ExcelHelper代码                
string strExcel = "";   
ExcelHelper代码                OleDbDataAdapter myCommand 
= null;
ExcelHelper代码                DataSet ds 
= null;
ExcelHelper代码                strExcel
="select * from [Sheet1$]";
ExcelHelper代码                myCommand 
= new OleDbDataAdapter(strExcel, strConn);
ExcelHelper代码                ds 
= new DataSet();
ExcelHelper代码                myCommand.Fill(ds,
"table1");   
ExcelHelper代码                
return ds;
ExcelHelper代码            }

ExcelHelper代码            
catch(System.Data.OleDb.OleDbException ex)
ExcelHelper代码            
{
ExcelHelper代码                System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ex.Message );
ExcelHelper代码                
return null;
ExcelHelper代码            }

ExcelHelper代码        }

ExcelHelper代码
ExcelHelper代码        
public static void DataTableToExcel(System.Data.DataView dataview,string Path,Hashtable NameMap)
ExcelHelper代码        
{
ExcelHelper代码            
try
ExcelHelper代码            
{
ExcelHelper代码                
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
ExcelHelper代码                OleDbConnection conn 
= new OleDbConnection(strConn);
ExcelHelper代码                conn.Open();  
ExcelHelper代码                System.Data.OleDb.OleDbCommand cmd
=new OleDbCommand ();
ExcelHelper代码                cmd.Connection 
= conn;
ExcelHelper代码
ExcelHelper代码                
string strSql = string.Empty ,strSql1 = string.Empty ;
ExcelHelper代码                
int i , j ;
ExcelHelper代码
ExcelHelper代码                
for(  i = 0 ; i < dataview.Count ; i++ )
ExcelHelper代码                
{
ExcelHelper代码                    
ExcelHelper代码                    strSql 
= "INSERT INTO [sheet1$] (";
ExcelHelper代码                    strSql1 
= ") values(";
ExcelHelper代码                    
for(  j = 0 ; j < dataview.Table.Columns.Count ; j++)
ExcelHelper代码                    
{
ExcelHelper代码                        
if( NameMap.ContainsKey( dataview.Table.Columns[j].ColumnName ) )
ExcelHelper代码                        
{
ExcelHelper代码                            strSql 
+= NameMap[dataview.Table.Columns[j].ColumnName] +"," ;  //2414210
ExcelHelper代码
                            strSql1 += "'" +dataview[i][j].ToString() + "',";
ExcelHelper代码                        }

ExcelHelper代码                        
ExcelHelper代码                    }

ExcelHelper代码                    
ExcelHelper代码                    
try
ExcelHelper代码                    
{
ExcelHelper代码                        
if( strSql.EndsWith(",") ) 
ExcelHelper代码                            strSql 
= strSql.Substring(0,strSql.Length - 1 ) ;
ExcelHelper代码                        
if( strSql1.EndsWith(",") ) 
ExcelHelper代码                            strSql1 
= strSql1.Substring(0,strSql1.Length - 1 ) ;
ExcelHelper代码
ExcelHelper代码                        strSql1 
= strSql1 + ")";
ExcelHelper代码                        strSql 
= strSql + strSql1 ;
ExcelHelper代码
ExcelHelper代码                        cmd.CommandText 
= strSql ;
ExcelHelper代码                        cmd.ExecuteNonQuery();
ExcelHelper代码                    }

ExcelHelper代码                    
catch(Exception ex)
ExcelHelper代码                    
{
ExcelHelper代码                        System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ strSql + strSql1 + ex.Message );
ExcelHelper代码                        
throw new Exception(strSql + ex.Message);
ExcelHelper代码                    }

ExcelHelper代码                }

ExcelHelper代码                conn.Close ();
ExcelHelper代码            }

ExcelHelper代码            
catch(System.Data.OleDb.OleDbException ex)
ExcelHelper代码            
{
ExcelHelper代码                System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ex.Message );
ExcelHelper代码            }

ExcelHelper代码        }

ExcelHelper代码
ExcelHelper代码        
public static void DataTableToExcel(System.Data.DataView dataview,string Path)
ExcelHelper代码        
{
ExcelHelper代码            
try
ExcelHelper代码            
{
ExcelHelper代码                
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
ExcelHelper代码                OleDbConnection conn 
= new OleDbConnection(strConn);
ExcelHelper代码                conn.Open();  
ExcelHelper代码                System.Data.OleDb.OleDbCommand cmd
=new OleDbCommand ();
ExcelHelper代码                cmd.Connection 
= conn;
ExcelHelper代码
ExcelHelper代码                
string strSql = string.Empty ,strSql1 = string.Empty ;
ExcelHelper代码                
int i , j ;
ExcelHelper代码
ExcelHelper代码                
for(  i = 0 ; i < dataview.Count ; i++ )
ExcelHelper代码                
{
ExcelHelper代码                    
ExcelHelper代码                    strSql 
= "INSERT INTO [sheet1$] (";
ExcelHelper代码                    strSql1 
= ") values(";
ExcelHelper代码                    
for(  j = 0 ; j < dataview.Table.Columns.Count ; j++)
ExcelHelper代码                    
{
ExcelHelper代码                        strSql 
+= dataview.Table.Columns[j].ColumnName +"," ;  
ExcelHelper代码                        strSql1 
+= "'" +dataview[i][j].ToString() + "',";
ExcelHelper代码                    }

ExcelHelper代码            
//        
ExcelHelper代码
                    try
ExcelHelper代码                    
{
ExcelHelper代码                        
if( strSql.EndsWith(",") ) 
ExcelHelper代码                            strSql 
= strSql.Substring(0,strSql.Length - 1 ) ;
ExcelHelper代码                        
if( strSql1.EndsWith(",") ) 
ExcelHelper代码                            strSql1 
= strSql1.Substring(0,strSql1.Length - 1 ) ;
ExcelHelper代码                        strSql1 
= strSql1 + ")";
ExcelHelper代码                        strSql 
= strSql + strSql1 ;
ExcelHelper代码                        cmd.CommandText 
= strSql  ;
ExcelHelper代码                        cmd.ExecuteNonQuery();
ExcelHelper代码                    }

ExcelHelper代码                    
catch(Exception ex)
ExcelHelper代码                    
{
ExcelHelper代码                        System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ strSql + ex.Message );
ExcelHelper代码                        
throw new Exception(strSql + ex.Message);
ExcelHelper代码                    }

ExcelHelper代码                }

ExcelHelper代码                conn.Close ();
ExcelHelper代码            }

ExcelHelper代码            
catch(System.Data.OleDb.OleDbException ex)
ExcelHelper代码            
{
ExcelHelper代码                System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ex.Message );
ExcelHelper代码            }

ExcelHelper代码        }

ExcelHelper代码
ExcelHelper代码        
/// <summary>
ExcelHelper代码        
/// 写入Excel文档
ExcelHelper代码        
/// </summary>
ExcelHelper代码        
/// <param name="Path">文件名称</param>

ExcelHelper代码        public bool SaveFP2toExcel(string Path)
ExcelHelper代码        
{
ExcelHelper代码            
try
ExcelHelper代码            
{
ExcelHelper代码                
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
ExcelHelper代码                OleDbConnection conn 
= new OleDbConnection(strConn);
ExcelHelper代码                conn.Open();  
ExcelHelper代码                System.Data.OleDb.OleDbCommand cmd
=new OleDbCommand ();
ExcelHelper代码                cmd.Connection 
=conn;
ExcelHelper代码                cmd.CommandText 
="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
ExcelHelper代码                cmd.ExecuteNonQuery ();
ExcelHelper代码    
/*            for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
ExcelHelper代码                {
ExcelHelper代码                    if(fp2.Sheets [0].Cells[i,0].Text!="")
ExcelHelper代码                    {
ExcelHelper代码                        cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
ExcelHelper代码                            fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
ExcelHelper代码                            "','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
ExcelHelper代码                        cmd.ExecuteNonQuery ();
ExcelHelper代码                    }
ExcelHelper代码                }
ExcelHelper代码                
*/

ExcelHelper代码                conn.Close ();
ExcelHelper代码                
return true;
ExcelHelper代码            }

ExcelHelper代码            
catch(System.Data.OleDb.OleDbException ex)
ExcelHelper代码            
{
ExcelHelper代码                System.Diagnostics.Debug.WriteLine (
"写入Excel发生错误:"+ex.Message );
ExcelHelper代码            }

ExcelHelper代码            
return false;
ExcelHelper代码        }

ExcelHelper代码
ExcelHelper代码    }

ExcelHelper代码}

以下文章中用到的工具类 发布一个Excel导入数据到GridView的类

你可能感兴趣的:(Excel)