• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#开发的高性能EXCEL导入、导出工具DataPie(支持MSSQL、ORACLE、ACCESS,附源码下载 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

         作为财务数据核算人员,面对大量的业务与财务数据,借助于传统的EXCEL表格,已经力不从心。最近几个月,利用周末及下班的空闲时间,写了一个数据库导入导出工具,以方便业务逻辑密集型的数据处理。目前,DataPie支持百万级别的数据导出,对于几十万的数据导入,也轻松应付。

 

源码及安装包下载地址:https://github.com/yfl8910/DataPie

 .NET 6 版本:https://github.com/yfl8910/DataPie2

 

先看看界面,登录界面:

 

主界面:

 

 

 

主要代码:

1.excel文件读到DataTable

 

        ///<summary>

        ///根据excel路径和sheet名称,返回excelDataTable

        ///</summary>

        public static DataTable GetExcelDataTable(string path, string tname)

        {

            /*Office 2007*/

            string ace = "Microsoft.ACE.OLEDB.12.0";

            /*Office 97 - 2003*/

            string jet = "Microsoft.Jet.OLEDB.4.0";

            string xl2007 = "Excel 12.0 Xml";

            string xl2003 = "Excel 8.0";

            string imex = "IMEX=1";

            /* csv */

            string text = "text";

            string fmt = "FMT=Delimited";

            string hdr = "Yes";

            string conn = "Provider={0};Data Source={1};Extended Properties=\"{2};HDR={3};{4}\";";

            string select = string.Format("SELECT * FROM [{0}$]", tname);

            //string select = sql;

            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, hdr, imex);

                    break;

                case ".xls":

                    conn = String.Format(conn, jet, Path.GetFullPath(path), xl2003, hdr, imex);

                    break;

                case ".csv":

                    conn = String.Format(conn, jet, Path.GetDirectoryName(path), text, hdr, fmt);

                    //sheet = Path.GetFileName(path);

                    break;

                default:

                    throw new Exception("File Not Supported!");

            }

            OleDbConnection con = new OleDbConnection(conn);

            con.Open();

            //select = string.Format(select, sql);

            oda = new OleDbDataAdapter(select, con);

            oda.Fill(dt);

            con.Close();

            return dt;

        }

2.批量把数据导入到数据库

1SQL SERVER版本

    public bool SqlBulkCopyImport(IList<string> maplist, string TableName, DataTable dt)

        {

            using (SqlConnection connection = new SqlConnection(connectionString))

            {

                connection.Open();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))

                {

                    bulkCopy.DestinationTableName = TableName;

                    foreach (string a in maplist)

                    {

                        bulkCopy.ColumnMappings.Add(a, a);

                    }

                    try

                    {

                        bulkCopy.WriteToServer(dt);

                        return true;

                    }

                    catch (Exception e)

                    {

                        throw e;

                    }

                }

            }

        }

2oracle版本 

public bool SqlBulkCopyImport(IList<string> maplist, string TableName, DataTable dt)

        {

 

            using (OracleConnection connection = new OracleConnection(connectionString))

            {

 

                connection.Open();

 

                using (OracleBulkCopy bulkCopy = new OracleBulkCopy(connection))

                {

 

                    bulkCopy.DestinationTableName = TableName;

 

                    foreach (string a in maplist)

                    {

 

                        bulkCopy.ColumnMappings.Add(a, a);

 

                    }

 

                    try

                    {

 

                        bulkCopy.WriteToServer(dt);

 

                        return true;

 

                    }

 

                    catch (Exception e)

                    {

                        throw e;

 

                    }

                }

            }

        }

 

3ACCESS版本

public bool SqlBulkCopyImport(IList<string> maplist, string TableName, DataTable dt)

        {

            try

            {

                using (OleDbConnection connection = new OleDbConnection(connectionString))

                {

                    connection.Open();

                    OleDbDataAdapter adapter = new OleDbDataAdapter("select * from " + TableName + "  where 1=0", connection);

                    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

                    int rowcount = dt.Rows.Count;

                    for (int n = 0; n < rowcount; n++)

                    {

                        dt.Rows[n].SetAdded();

                    }

                    //adapter.UpdateBatchSize = 1000;

                    adapter.Update(dt);

                }

                return true;

            }

            catch (Exception e)

            {

                throw e;

            }

       

       

        }

 

 

3.导出EXCEL文件

///<summary>

        ///保存excel文件,覆盖相同文件名的文件

        ///</summary>

        public static bool SaveExcel(string SheetName, DataTable dt, ExcelPackage package)

        {

 

            try

            {              

                ExcelWorksheet ws = package.Workbook.Worksheets.Add(SheetName);

                ws.Cells["A1"].LoadFromDataTable(dt, true);

                return true;

            }

            catch (Exception ex)

            {


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#如何打开一个窗体,同时关闭该窗体发布时间:2022-07-13
下一篇:
计算机二级-C语言-程序修改题-190108记录-字符串处理发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap