C# Excel读取到DataTable帮助类及测评

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




C#读取Excel的三种方式及测评

1、npoi

2、epplus

3、oledb

注意:npoi和epplus都是第三方组件,oledb是微软自带

Excel帮助类,(NuGet程序包下载并引入npoi和epplus不再赘述)

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace ExcelHelper.cs
{
    public class excelHelp
    {
        // Oledb

        private string strConn;
        private OleDbConnection conn = null;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="path"></param>
        public excelHelp(string path)
        {
            string[] sArray = path.Split('.');
            int count = sArray.Length - 1;
            if (sArray[count] == "xls")
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (sArray[count] == "xlsx")
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            conn = new OleDbConnection(strConn);
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        private void OpenConn()
        {
            try
            {
                if (conn != null && conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        private void CloseConn()
        {
            try
            {
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
        /// <summary>
        /// 构造操作命令
        /// </summary>
        /// <param name="cmdText">带参命令</param>
        /// <param name="param">参数数组</param>
        /// <param name="values">参数值数组</param>
        /// <returns></returns>
        private OleDbCommand CreateCommand(string cmdText, string[] param, object[] values)
        {
            OleDbCommand myCmd = new OleDbCommand(cmdText, conn);
            for (int i = 0; i < param.Length; i++)
            {
                myCmd.Parameters.AddWithValue(param[i], values[i]);
            }
            return myCmd;
        }
        /// <summary>
        /// 根据命令语句返回dt
        /// </summary>
        /// <param name="cmdText">命令语句</param>
        /// <param name="dtName">表名</param>
        /// <param name="param">参数数组,若没有参数可以设置为空</param>
        /// <param name="values">参数值数组,只有当param不为空时有效</param>
        /// <returns></returns>
        public DataTable excelToDtByOledb(string cmdText, string dtName, string[] param, object[] values)
        {
            OpenConn();
            OleDbCommand myCmd;
            if (param != null)
            {
                myCmd = this.CreateCommand(cmdText, param, values);
            }
            else
            {
                myCmd = new OleDbCommand(cmdText, conn);
            }
            OleDbDataAdapter myAdp = new OleDbDataAdapter(myCmd);
            DataTable dt = new DataTable(dtName);
            try
            {
                myAdp.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CloseConn();
            }
        }


        //NPOI

        /// <summary>
        /// 获取单元格类型(xls)
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private static object GetValueTypeForXLS(HSSFCell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank: //BLANK:
                    return null;
                case CellType.Boolean: //BOOLEAN:
                    return cell.BooleanCellValue;
                case CellType.Numeric: //NUMERIC:
                    return cell.NumericCellValue;
                case CellType.String: //STRING:
                    return cell.StringCellValue;
                case CellType.Error: //ERROR:
                    return cell.ErrorCellValue;
                case CellType.Formula: //FORMULA:
                default:
                    return "=" + cell.CellFormula;
            }
        }

        /// <summary>
        /// 获取单元格类型(xlsx)
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private static object GetValueTypeForXLSX(XSSFCell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank: //BLANK:
                    return null;
                case CellType.Boolean: //BOOLEAN:
                    return cell.BooleanCellValue;
                case CellType.Numeric: //NUMERIC:
                    return cell.NumericCellValue;
                case CellType.String: //STRING:
                    return cell.StringCellValue;
                case CellType.Error: //ERROR:
                    return cell.ErrorCellValue;
                case CellType.Formula: //FORMULA:
                default:
                    return "=" + cell.CellFormula;
            }
        }

        /// <summary>
        /// 将Excel文件中的数据读出到DataTable中(xlsx)
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static DataTable excelToDtByNpoiForXlsx(string file)
        {
            DataTable dt = new DataTable();
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs);
                ISheet sheet = xssfworkbook.GetSheetAt(0);

                //表头
                IRow header = sheet.GetRow(sheet.FirstRowNum);
                List<int> columns = new List<int>();
                for (int i = 0; i < header.LastCellNum; i++)
                {
                    object obj = GetValueTypeForXLSX(header.GetCell(i) as XSSFCell);
                    if (obj == null || obj.ToString() == string.Empty)
                    {
                        dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
                        //continue;
                    }
                    else
                        dt.Columns.Add(new DataColumn(obj.ToString()));
                    columns.Add(i);
                }
                //数据
                for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    DataRow dr = dt.NewRow();
                    bool hasValue = false;
                    foreach (int j in columns)
                    {
                        dr[j] = GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) as XSSFCell);
                        if (dr[j] != null && dr[j].ToString() != string.Empty)
                        {
                            hasValue = true;
                        }
                    }
                    if (hasValue)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 将Excel文件中的数据读出到DataTable中(xls)
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static DataTable excelToDtByNpoiForXls(string file)
        {
            DataTable dt = new DataTable();
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
                ISheet sheet = hssfworkbook.GetSheetAt(0);

                //表头
                IRow header = sheet.GetRow(sheet.FirstRowNum);
                List<int> columns = new List<int>();
                for (int i = 0; i < header.LastCellNum; i++)
                {
                    object obj = GetValueTypeForXLS(header.GetCell(i) as HSSFCell);
                    if (obj == null || obj.ToString() == string.Empty)
                    {
                        dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
                        //continue;
                    }
                    else
                        dt.Columns.Add(new DataColumn(obj.ToString()));
                    columns.Add(i);
                }
                //数据
                for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    DataRow dr = dt.NewRow();
                    bool hasValue = false;
                    foreach (int j in columns)
                    {
                        dr[j] = GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) as HSSFCell);
                        if (dr[j] != null && dr[j].ToString() != string.Empty)
                        {
                            hasValue = true;
                        }
                    }
                    if (hasValue)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 读取Excel到Datatable
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static DataTable excelToDtByNpoi(string filepath,string dtName)
        {
            var dt = new DataTable(dtName);
            if (filepath.ToLower().Last() == 's')
            {
                dt = excelToDtByNpoiForXls(filepath);
            }
            else
            {
                dt = excelToDtByNpoiForXlsx(filepath);
            }
            return dt;
        }

        /// <summary>
        /// 将DataTable数据导出到Excel文件中(xls) 不包含文件名
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file"></param>
        public static void dtToExcelForXlsByNpoi(DataTable dt, string strFileName)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            ISheet sheet = hssfworkbook.CreateSheet("Sheet");

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            // 客户端下载
            MemoryStream stream = new MemoryStream();
            hssfworkbook.Write(stream);
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xls", HttpUtility.UrlEncode(strFileName, Encoding.UTF8)));
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.End();
            hssfworkbook = null;
            stream.Close();
            stream.Dispose();
        }

        /// <summary>
        /// 将DataTable数据导出到Excel文件中(xlsx) 不包含文件名
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file"></param>
        public static void dtToExcelForXlsxByNpoi(DataTable dt, string strFileName)
        {
            XSSFWorkbook xssfworkbook = new XSSFWorkbook();
            ISheet sheet = xssfworkbook.CreateSheet("Sheet");

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            // 客户端下载
            MemoryStream stream = new MemoryStream();
            xssfworkbook.Write(stream);
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xlsx", HttpUtility.UrlEncode(strFileName, Encoding.UTF8)));
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.End();
            xssfworkbook = null;
            stream.Close();
            stream.Dispose();
        }


        //EPPlus

        /// <summary>
        /// 使用EPPlus导出Excel(xlsx)
        /// </summary>
        /// <param name="sourceTable">数据源</param>
        /// <param name="strFileName">xlsx文件名(不含后缀名)</param>
        public static void dtToExcelByEpplus(DataTable sourceTable, string strFileName)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                string sheetName = string.IsNullOrEmpty(sourceTable.TableName) ? "Sheet1" : sourceTable.TableName;
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName);

                //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                ws.Cells["A1"].LoadFromDataTable(sourceTable, true);

                //Format the row
                ExcelBorderStyle borderStyle = ExcelBorderStyle.Thin;
                Color borderColor = Color.FromArgb(155, 155, 155);

                using (ExcelRange rng = ws.Cells[1, 1, sourceTable.Rows.Count + 1, sourceTable.Columns.Count])
                {
                    rng.Style.Font.Name = "等线";
                    rng.Style.Font.Size = 11;
                    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 255));

                    rng.Style.Border.Top.Style = borderStyle;
                    rng.Style.Border.Top.Color.SetColor(borderColor);

                    rng.Style.Border.Bottom.Style = borderStyle;
                    rng.Style.Border.Bottom.Color.SetColor(borderColor);

                    rng.Style.Border.Right.Style = borderStyle;
                    rng.Style.Border.Right.Color.SetColor(borderColor);
                }

                //Format the header row
                using (ExcelRange rng = ws.Cells[1, 1, 1, sourceTable.Columns.Count])
                {
                    rng.Style.Font.Bold = true;
                    rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(234, 241, 246));  //Set color to dark blue
                    rng.Style.Font.Color.SetColor(Color.FromArgb(51, 51, 51));
                }

                //Write it back to the client
                HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xlsx", HttpUtility.UrlEncode(strFileName, Encoding.UTF8)));
                HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                HttpContext.Current.Response.BinaryWrite(pck.GetAsByteArray());
                HttpContext.Current.Response.End();
            }
        }

        /// <summary>
        /// 将指定的Excel的文件转换成DataTable
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dtName"></param>
        /// <param name="hasHeader"></param>
        /// <returns></returns>
        public static DataTable excelToDtByEpplus(string path, string dtName, bool hasHeader = true)
        {
            using (var pck = new OfficeOpenXml.ExcelPackage())
            {
                using (var stream = File.OpenRead(path))
                {
                    pck.Load(stream);
                }
                var ws = pck.Workbook.Worksheets.First();
                DataTable dt = new DataTable(dtName);
                foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
                {
                    dt.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
                }
                var startRow = hasHeader ? 2 : 1;
                for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
                {
                    var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                    DataRow row = dt.Rows.Add();
                    foreach (var cell in wsRow)
                    {
                        row[cell.Start.Column - 1] = cell.Text;
                    }
                }
                return dt;
            }
        }


        //NPOI&EPPlus

        /// <summary>
        /// 混合模式 xls用npoi xlsx用epplus
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="dtName"></param>
        /// <returns></returns>
        public static DataTable excelToDt(string filepath, string dtName)
        {
            var dt = new DataTable(dtName);
            if (filepath.ToLower().Last() == 's')
            {
                //NPOI-XLS
                dt = excelToDtByNpoiForXls(filepath);
            }
            else
            {
                //EPPlus-XLSX
                dt = excelToDtByEpplus(filepath,dtName);
            }
            return dt;
        }
    }
}

调用方法:

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExcelHelper.cs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\daobin\Desktop\新建 Microsoft Excel 工作表.xlsx";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DataTable dt = excelHelp.excelToDtByNpoi(path,"excel");
            int count = dt.Rows.Count;
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            Label1.Text = "npoi:" + ts.TotalMilliseconds.ToString();
            Label4.Text = "npoi-dt行数:" + count;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\daobin\Desktop\新建 Microsoft Excel 工作表.xlsx";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DataTable dt = excelHelp.excelToDtByEpplus(path, "excel");
            int count = dt.Rows.Count;
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            Label2.Text = "epplus:" + ts.TotalMilliseconds.ToString();
            Label5.Text = "epplus-dt行数:" + count;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            
            string path = @"C:\Users\daobin\Desktop\新建 Microsoft Excel 工作表.xlsx";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string cmd = "select * from [Sheet1$]";
            excelHelp excel = new excelHelp(path);
            DataTable dt = excel.excelToDtByOledb(cmd, "excel", null, null);
            int count = dt.Rows.Count;
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            Label3.Text = "oledb:" + ts.TotalMilliseconds.ToString();
            Label6.Text = "oledb-dt行数:" + count;
        }
    }
} 

测试环境:

1、npoi – 2.4.0

2、epplus – 4.5.2.1

3、vs2017 – webform

806215条Excel数据读取测试 

Excel读取测试

1048575条Excel数据读取测试

Excel读取测试

注意:npoi无数据是因为电脑卡死,读不出来

结果:

速度快 – epplus>npoi>oledb

内存小 – epplus>oledb>npoi

兼容性 – npoi>oledb>epplus

准确率 – npoi=epplus>oledb

总结:

1、oledb有时候会读出Excel底部空行的情况,且需要服务器支持,不建议考虑使用

2、没有低版本Excel要求的建议使用epplus

3、数据量小npoi,数据量大epplus

原文链接:https://www.idaobin.com/archives/1975.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

− 1 = 2