zoukankan      html  css  js  c++  java
  • 读取excel

    public DataTable GetData(string strpath, string strSql,string ExcelEdition)
            {

                DataTable dtgc = null;
                OleDbConnection con = null;
                try
                {
                    string strCon;
                    switch(ExcelEdition)
                    {
                        case "2003":
                            strCon= "provider=microsoft.jet.oledb.4.0;data source=" + strpath + ";extended properties='Excel8.0;HDR=Yes;IMEX=1'";
                            break;
                        case "2007":
                            strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strpath + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
                            break;
                        case "2010":
                            strCon=@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strpath+ ";" +"Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
                            break;
                        case "userdefined":
                            strCon = strconExcel;
                            break;
                            default:
                            strCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strpath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
                            break;
                    }
                     if (strCon=="")
                     {
                         MessageBox.Show("没有设置自定义的连接字符串");
                         return null;
                     }
                      
                


                    con = new OleDbConnection(strCon);

                    OleDbCommand cmd = new OleDbCommand(strSql, con);
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();

                    da.Fill(ds);
                    dtgc = ds.Tables[0];

                    return dtgc;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
                finally
                {
                    con.Close();
                }


            }

  • 相关阅读:
    自定义适用于手机和平板电脑的 Dynamics 365(四):窗体脚本
    自定义适用于手机和平板电脑的 Dynamics 365(三):显示的实体
    自定义适用于手机和平板电脑的 Dynamics 365(二):窗体自定义项
    自定义适用于手机和平板电脑的 Dynamics 365(一):主页
    使用IEDA远程调试
    Apache Roller 5.0.3 XXE漏洞分析
    fastjson 反序列化漏洞笔记,比较乱
    JAVA常见安全问题复现
    Spring Integration Zip不安全解压(CVE-2018-1261)漏洞复现
    php一句话反弹bash shell
  • 原文地址:https://www.cnblogs.com/jinyuttt/p/2213679.html
Copyright © 2011-2022 走看看