zoukankan      html  css  js  c++  java
  • Excel 导入 DataSet 的类

    using System;
    using System.Data;
    using System.Data.OleDb;

    namespace Legendigital.Swim.Common
    {
        
    /// <summary>
        
    /// Summary description for Excel.
        
    /// </summary>
        public class Excel
         {
            
    private const string STRING_CONECTTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties="Excel 8.0;HDR=Yes;IMEX=1;"";
            
    public Excel()
             {
                
    //
                
    // TODO: Add constructor logic here
                
    //
             }
            
    public static DataTable GetDataTable(string excelFilePath,string sql)
             {
                
    try
                 {
                     DataSet ds
    = new DataSet();
                    
    string connstring = string.Format(STRING_CONECTTIONSTRING,excelFilePath);
                    
    using(OleDbConnection cnn = new OleDbConnection(connstring))
                     {
                         cnn.Open();
                         OleDbDataAdapter dataAdapter
    = new OleDbDataAdapter(sql,cnn);
                         dataAdapter.Fill(ds);
                         cnn.Close();
                        
    return ds.Tables[0];
                     }
                 }
                
    finally
                 {        
                 }
             }
            
    public static DataSet FillDataSet(string excelFilePath,string sql,DataSet ds)
             {
                 DataTable dt
    = GetDataTable(excelFilePath,sql);

                
    foreach(DataRow dr in dt.Rows)
                 {
                    
    try
                     {
                         DataRow newdr
    = ds.Tables[0].NewRow();
                        
    foreach(DataColumn dc in dt.Columns)
                         {
                            
    if(ds.Tables[0].Columns.Contains(dc.ColumnName))
                             {
                                 newdr[dc.ColumnName]
    = dr[dc.ColumnName];                            
                             }
                         }
                         ds.Tables[
    0].Rows.Add(newdr);
                     }
                    
    catch
                     {

                     }
                 }

                 ds.AcceptChanges();

                
    return ds;
                
             }
         }
    }
  • 相关阅读:
    DynamicLibrary动态查询需要的一个CS 文件下载
    指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效
    ACCESS的一个相对好用的数据库连接字符串
    ASP.NET 入门 博客园文章 索引篇
    c# 最小化到系统栏,时钟,随机语句,程序发布 读书笔记本 (三)
    poj 2817 WordStack (状态dp)
    hdu 4380 Farmer Greedy (计算几何 2012 MultiUniversity Training Contest 9 )
    hdu 4353 Finding Mine (计算几何 2012 MultiUniversity Training Contest 6 )
    poj 3735 Training little cats (矩阵快速幂)
    hdu 4374 One hundred layer (dp +单调队列 2012 MultiUniversity Training Contest 8 )
  • 原文地址:https://www.cnblogs.com/benzhang/p/1458715.html
Copyright © 2011-2022 走看看