读取ExcelCSV 文件到数据库

读取ExcelCSV 文件到数据库,第1张

概述读取Excel / CSV 文件数据库

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

    /// <summary>    /// Reads the contents of an excel spreadsheet (.xlsx || .xls)     /// or a comma separated value file (.csv) into a data table.     /// (Works with Excel 2003 and Excel 2007)    /// </summary>    /// <param name="path">Path to file on server,type System.string</param>    /// <param name="has@R_403_5548@">True or false to indicate if first row is a @R_403_5548@ row or not</param>    /// <param name="sheet">name of the spreadsheet to select data from. Use null for .csv file</param>    /// <returns>System.Data.Datatable</returns>    protected Datatable GetfileContent(string path,bool has@R_403_5548@,string sheet)    {        #region Data ProvIDers        /*Office 2007*/        string ace = "Microsoft.ACE.olEDB.12.0";        /*Office 97 - 2003*/        string jet = "Microsoft.Jet.olEDB.4.0";        #endregion        #region Excel PropertIEs        string xl2007 = "Excel 12.0 Xml";        string xl2003 = "Excel 8.0";        string imex = "IMEX=1";        #endregion        #region CSV PropertIEs        string text = "text";        string fmt = "FMT=delimited";        #endregion        string hdr = has@R_403_5548@ ? "Yes" : "No";        string conn = "ProvIDer={0};Data Source={1};Extended PropertIEs=\"{2};HDR={3};{4}\";";        string select = "SELECT * FROM {0}";        string ext = Path.GetExtension(path);        oleDbDataAdapter oda;        Datatable dt = new Datatable("data");        switch (ext.Tolower())        {            case ".xlsx":                conn = String.Format(conn,ace,Path.GetFullPath(path),xl2007,has@R_403_5548@,imex);                                break;            case ".xls":                conn = String.Format(conn,jet,xl2003,imex);                break;            case ".csv":                conn = String.Format(conn,Path.GetDirectoryname(path),text,fmt);                sheet = Path.Getfilename(path);                break;            default:                throw new Exception("file Not Supported!");        }        select = String.Format(select,sheet);        oda = new oleDbDataAdapter(select,conn);        oda.Fill(dt);        return dt;     }

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的读取Excel / CSV 文件到数据库全部内容,希望文章能够帮你解决读取Excel / CSV 文件到数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1274833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存