• 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;
                
             }
         }
    }
  • 相关阅读:
    Spring Security简介与入门Demo
    电商项目之多功能增删改查案例
    linux-用户管理
    zabbix3.4配置第三方邮件报警
    zabbix3.4配置客户端配置
    centos7上安装zabbix3.4的详细步骤与问题处理记录
    MyBatis日记(五):一对一关系,一对多关系
    MyBatis日记(四):MyBatis——insert、update、delete、select
    Python日记(二):Python之禅
    Python日记(一):拜见小主——Python
  • 原文地址:https://www.cnblogs.com/benzhang/p/1458715.html
Copyright © 2020-2023  润新知