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>
        publicclass Excel
         {
            
    privateconststring 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
                
    //
             }
            
    publicstatic 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
                 {        
                 }
             }
            
    publicstatic 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;
                
             }
         }
    }

  • 相关阅读:
    TextView 显示内容时出现 ArrayIndexOutOfBoundsException 的解决方法(Android 4.1)
    Android Activity 启动模式和任务栈
    Android 坐标系和 MotionEvent 分析、滑动
    Android 控件架构及View、ViewGroup的测量
    Android Studio 3.0 及以上版本使用技巧总结
    移动端开发网络优化建议
    Netty系列之Netty高性能之道
    并发框架Disruptor译文
    Python轻量Web框架Flask使用
    修改MySQL的时区,涉及参数time_zone
  • 原文地址:https://www.cnblogs.com/jcomet/p/1242782.html
Copyright © 2011-2022 走看看