zoukankan      html  css  js  c++  java
  • excel导入导出

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using FS.Extend;
    using System.Data;
    using System.Data.OleDb;
    namespace Tag.Logic
    {
        public static class UploadFileLogic
        {
            /// <summary>
            /// 获取链接字符串
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            private static string GetConectionString(string filePath)
            {
                var strSuffix = "";
                if (filePath.IsNullOrEmpty()) { return ""; }
                strSuffix = filePath.Substring(filePath.LastIndexOf("."));
                var strConectionString = "";
                switch (strSuffix)
                {
                    case ".xls":
                        {
                            strConectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties="Excel 8.0"";
                            break;
                        }
                    case ".xlsx":
                        {
                            strConectionString = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties="Excel 12.0"";
                            break;
                        }
                }
                return strConectionString;
            }
            /// <summary>
            /// 获取表数据
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <param name="sheetName">名称</param>
            /// <returns></returns>
            public static DataTable GetDataTable(string filePath, string sheetName)
            {
                string strConn = GetConectionString(filePath);
                string strSql = "select * from [" + sheetName + "$]";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                DataTable dt = new DataTable();
                OleDbDataAdapter ole = new OleDbDataAdapter(strSql, conn);
                ole.Fill(dt);
                return dt;
            }
        }
    }

     var licenseDT = new DataTable();

    licenseDT = UploadFileLogic.GetDataTable(pathfile, "证书信息");

  • 相关阅读:
    最少必要知识
    pythonGUI编程
    计算机网络基础
    .htaccess总结
    SSRF总结
    jsp学习笔记
    url任意跳转漏洞
    学期总结
    计科一班本学期作业成绩汇总表格
    C语言I博客作业08
  • 原文地址:https://www.cnblogs.com/sll-fuling/p/5033383.html
Copyright © 2011-2022 走看看