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

简介:

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

先看看界面。

登录界面:

 

 

主界面:

 

 

 

主要代码:

1.excel文件读到DataTable

 

        /// <summary>

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

        /// </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;

 

                    }

                }

            }

        }

 

3)ACCESS版本

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)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 多个表格导出到一个excel工作簿

        /// </summary>

        public static void export(IList<string> SheetNames, string filename, DBConfig db, IList<string> sqls)

        {

            DataTable dt = new DataTable();

            FileInfo newFile = new FileInfo(filename);

            if (newFile.Exists)

            {

                newFile.Delete();

                newFile = new FileInfo(filename);

            }

            using (ExcelPackage package = new ExcelPackage(newFile))

            {

                for (int i = 0; i < sqls.Count; i++)

                {

                    dt = db.DB.ReturnDataTable(sqls[i]);

                    SaveExcel(SheetNames[i], dt, package);

                }

                package.Save();

            }

        }

 

        /// <summary>

        /// 单个表格导出到一个excel工作簿

        /// </summary>

        public static void export(string SheetName, string filename, DBConfig db, string sql)

        {

            DataTable dt = new DataTable();

            FileInfo newFile = new FileInfo(filename);

            if (newFile.Exists)

            {

                newFile.Delete();

                newFile = new FileInfo(filename);

            }

            using (ExcelPackage package = new ExcelPackage(newFile))

            {

                dt = db.DB.ReturnDataTable(sql);

                SaveExcel(SheetName, dt, package);

                package.Save();

            }

        }

 

        /// <summary>

        /// 单个表导出到多个excel工作簿(分页)

        /// </summary>

        public static void export(string SheetName, string filename, DBConfig db, string sql, int num, int pagesize)

        {

            DataTable dt = new DataTable();

            FileInfo newFile = new FileInfo(filename);

            int numtb = num / pagesize + 1;

            for (int i = 1; i <= numtb; i++)

            {

                string s = filename.Substring(0, filename.LastIndexOf("."));

                StringBuilder newfileName = new StringBuilder(s);

                newfileName.Append(i + ".xlsx");

                newFile = new FileInfo(newfileName.ToString());

                if (newFile.Exists)

                {

                    newFile.Delete();

                    newFile = new FileInfo(newfileName.ToString());

                }

                using (ExcelPackage package = new ExcelPackage(newFile))

                {

                    dt = db.DB.ReturnDataTable(sql, pagesize * (i - 1), pagesize);

                    SaveExcel(SheetName, dt, package);

                    package.Save();

                }

            }

        }

 

4.DataPie下载地址

 

http://datapie.codeplex.com/releases/view/88081

 

分类:  ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2012/05/20/2510845.html ,如需转载请自行联系原作者
相关文章
|
3月前
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
4月前
|
文字识别 C# Python
使用C#将几个Excel文件合并去重分类
使用C#将几个Excel文件合并去重分类
33 3
|
4月前
|
开发框架 算法 .NET
C#使用MiniExcel导入导出数据到Excel/CSV文件
C#使用MiniExcel导入导出数据到Excel/CSV文件
86 0
|
6月前
|
C#
【C#】C#读写Excel文件
【C#】C#读写Excel文件
133 1
|
SQL Oracle 关系型数据库
Oracle 补丁体系(PSR/PSU/CPU) 及 opatch 工具 介绍
原文:http://blog.csdn.net/tianlesoftware/article/details/5809526 一. CPU(Critical Patch Update)     一个CPU内包含了对多个安全漏洞的修复,并且也包括相应必需的非安全漏洞的补丁。
1507 0
|
2月前
|
存储 Oracle 关系型数据库
Oracle数据库的应用场景有哪些?
【10月更文挑战第15天】Oracle数据库的应用场景有哪些?
179 64
|
1月前
|
SQL Oracle 关系型数据库
Oracle数据库优化方法
【10月更文挑战第25天】Oracle数据库优化方法
40 7
|
1月前
|
Oracle 关系型数据库 数据库
oracle数据库技巧
【10月更文挑战第25天】oracle数据库技巧
28 6
|
1月前
|
存储 Oracle 关系型数据库
Oracle数据库优化策略
【10月更文挑战第25天】Oracle数据库优化策略
25 5

推荐镜像

更多