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;
                
             }
         }
    }
  • 相关阅读:
    C# TransactionScope 使用
    .Net 4.5 的async 和await 的简单理解使用
    图片的等比缩放
    IIS 8 下使用 WCF
    SQL Server 中字符串中包含字符串变量的表示方法
    jsTree 的简单用法--异步加载和刷新数据
    webService 部署以后参数输入框不能显示
    js 节点属性
    js 数组排序
    js 时间格式化 -- 时间加减实现
  • 原文地址:https://www.cnblogs.com/benzhang/p/1458715.html
Copyright © 2011-2022 走看看