首页 > 代码库 > [.Net] C# Excel操作类 ExcelHelper

[.Net] C# Excel操作类 ExcelHelper

实现C#与Excel文件的交互操作,实现以下功能:

1、DataTable 导出到 Excel文件

2、Model数据实体导出到 Excel文件[List<Model>]

3、导出数据到模版

4、多DataTable导入到同一Excel文件

对于第四项功能,常见的用途为:最后一行显示的是百分比,为字符型,而这列其它类型为数据型,如果放在同一个DataTable中,显然不合适。此操作类可实现

5、将Excel文件导入到数据库表中

可以指定每个字段的数据类型

此帮助类的代码量不算少,但注释比较详细,导出数据集中在一个方法,导入数据集中在一个方法。只看完整的两个方法就可以了,其它方法为便于使用所写的辅助简化方法,看看就清楚了:

using System;using System.Collections.Generic;using System.Text;using System.Configuration;using Microsoft.Office.Interop.Excel;using System.Data;using System.Reflection;using System.Data.OleDb; namespace SOSP.Common{    /// <summary>    /// C#与Excel交互类    /// </summary>    public class ExcelHelper    {        #region 导出到Excel        #region ExportExcelForDataTable        /// <summary>        /// 从DataTable导出Excel,指定列别名,指定要排除的列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="colName">各列的列名List string </param>        /// <param name="excludeColumn">要显示/排除的列</param>        /// <param name="excludeType">显示/排除列方式 0为所有列 1指定的为要显示的列 2指定的为要排除的列</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 例:tp.xlsx 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForDataTable(System.Data.DataTable dt, string excelPathName, string pathType, List<string> colName, List<string> excludeColumn, string excludeType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            try            {                if (dt == null || dt.Rows.Count == 0) return false;                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();                if (xlApp == null)                {                    return false;                }                System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;                 Microsoft.Office.Interop.Excel.Workbook workbook = null;                if (TemplatePath == "")                {                    workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);                }                else                {                    workbook = workbooks.Add(TemplatePath); //加载模板                }                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];                Microsoft.Office.Interop.Excel.Range range;                 long totalCount = dt.Rows.Count;                if (exDataTableList != null && exDataTableList.Count > 0)                {                    foreach (System.Data.DataTable item in exDataTableList)                    {                        totalCount += item.Rows.Count;                    }                }                long rowRead = 0;                float percent = 0;                string exclStr = "";//要排除的列临时项                object exclType;//DataTable 列的类型,用于做                int colPosition = 0;//列位置                if (sheetName != null && sheetName != "")                {                    worksheet.Name = sheetName;                }                #region 列别名判定                if (TemplatePath == "")                {                    if (colName != null && colName.Count > 0)                    {                        #region 指定了列别名                        for (int i = 0; i < colName.Count; i++)                        {                            worksheet.Cells[1, i + 1] = colName[i];                            range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];                            range.Interior.ColorIndex = 15;                            range.Font.Bold = true;                            exclType = dt.Columns[i].DataType.Name;                            if (exclType.ToString() != "DateTime")                            {                                //range.EntireColumn.AutoFit();//全局自动调整列宽,不能再使用单独设置                                ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.AutoFit();                            }                            else                            {                                //规定列宽                                ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.ColumnWidth = 20;                            }                            //((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.ColumnWidth = 20;                        }                        #endregion                    }                    else                    {                        #region 未指定别名                        for (int i = 0; i < dt.Columns.Count; i++)                        {                            worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;                            range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];                            range.Interior.ColorIndex = 15;                            range.Font.Bold = true;                            exclType = dt.Columns[i].DataType.Name;                            if (exclType.ToString() != "DateTime")                            {                                //range.EntireColumn.AutoFit();//全局自动调整列宽,不能再使用单独设置                                ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.AutoFit();                            }                            else                            {                                //规定列宽                                ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.ColumnWidth = 20;                            }                            //((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Columns.ColumnWidth = 20;                        }                        #endregion                    }                }                else                {                    //用了模版,不加载标题                }                #endregion                #region 显示/排除列判定                if (excludeColumn != null && excludeColumn.Count > 0)                {                    switch (excludeType)                    {                        case "0":                            {                                #region 0为显示所有列                                #region 常规项                                int r = 0;                                for (r = 0; r < dt.Rows.Count; r++)                                {                                    colPosition = 0;                                    for (int i = 0; i < dt.Columns.Count; i++)                                    {                                        if (TemplatePath == "")                                        {                                            worksheet.Cells[r + 2, colPosition + 1] = dt.Rows[r][i].ToString();                                        }                                        else                                        {                                            worksheet.Cells[r + TemplateRow, colPosition + 1] = dt.Rows[r][i].ToString();                                        }                                        colPosition++;                                    }                                    rowRead++;                                    percent = ((float)(100 * rowRead)) / totalCount;                                }                                #endregion                                #region 扩展项                                if (exDataTableList != null && exDataTableList.Count > 0)                                {                                    foreach (System.Data.DataTable item in exDataTableList)                                    {                                        for (int k = 0; k < item.Rows.Count; r++, k++)                                        {                                            colPosition = 0;                                            //生成扩展 DataTable 每行数据                                            for (int t = 0; t < item.Columns.Count; t++)                                            {                                                if (TemplatePath == "")                                                {                                                    worksheet.Cells[r + 2, colPosition + 1] = item.Rows[k][t].ToString();                                                }                                                else                                                {                                                    worksheet.Cells[r + TemplateRow, colPosition + 1] = item.Rows[k][t].ToString();                                                }                                                colPosition++;                                            }                                            rowRead++;                                            percent = ((float)(100 * rowRead)) / totalCount;                                        }                                    }                                }                                #endregion                                #endregion                            }; break;                        case "1":                            {                                #region 1指定的为要显示的列                                #region 常规项                                int r = 0;                                for (r = 0; r < dt.Rows.Count; r++)                                {                                    colPosition = 0;                                    for (int i = 0; i < dt.Columns.Count; i++)                                    {                                        exclStr = dt.Columns[i].ColumnName;                                        if (excludeColumn.Contains(exclStr))                                        {                                            if (TemplatePath == "")                                            {                                                worksheet.Cells[r + 2, colPosition + 1] = dt.Rows[r][i].ToString();                                            }                                            else                                            {                                                worksheet.Cells[r + TemplateRow, colPosition + 1] = dt.Rows[r][i].ToString();                                            }                                            colPosition++;                                        }                                        else                                        {                                         }                                    }                                    rowRead++;                                    percent = ((float)(100 * rowRead)) / totalCount;                                }                                #endregion                                #region 扩展项                                if (exDataTableList != null && exDataTableList.Count > 0)                                {                                    foreach (System.Data.DataTable item in exDataTableList)                                    {                                        for (int k = 0; k < item.Rows.Count; r++, k++)                                        {                                            colPosition = 0;                                            //生成扩展 DataTable 每行数据                                            for (int t = 0; t < item.Columns.Count; t++)                                            {                                                exclStr = dt.Columns[t].ColumnName;                                                if (excludeColumn.Contains(exclStr))                                                {                                                    if (TemplatePath == "")                                                    {                                                        worksheet.Cells[r + 2, colPosition + 1] = item.Rows[k][t].ToString();                                                    }                                                    else                                                    {                                                        worksheet.Cells[r + TemplateRow, colPosition + 1] = item.Rows[k][t].ToString();                                                    }                                                    colPosition++;                                                }                                                else                                                {                                                 }                                            }                                            rowRead++;                                            percent = ((float)(100 * rowRead)) / totalCount;                                        }                                    }                                }                                #endregion                                #endregion                            }; break;                        case "2":                            {                                #region 2指定的为要排除的列                                #region 常规项                                int r = 0;                                for (r = 0; r < dt.Rows.Count; r++)                                {                                    colPosition = 0;                                    for (int i = 0; i < dt.Columns.Count; i++)                                    {                                        exclStr = dt.Columns[i].ColumnName;                                        if (excludeColumn.Contains(exclStr))                                        {                                         }                                        else                                        {                                            if (TemplatePath == "")                                            {                                                worksheet.Cells[r + 2, colPosition + 1] = dt.Rows[r][i].ToString();                                            }                                            else                                            {                                                worksheet.Cells[r + TemplateRow, colPosition + 1] = dt.Rows[r][i].ToString();                                            }                                            colPosition++;                                        }                                    }                                    rowRead++;                                    percent = ((float)(100 * rowRead)) / totalCount;                                }                                #endregion                                #region 扩展项                                if (exDataTableList != null && exDataTableList.Count > 0)                                {                                    foreach (System.Data.DataTable item in exDataTableList)                                    {                                        for (int k = 0; k < item.Rows.Count; r++, k++)                                        {                                            colPosition = 0;                                            //生成扩展 DataTable 每行数据                                            for (int t = 0; t < item.Columns.Count; t++)                                            {                                                exclStr = dt.Columns[t].ColumnName;                                                if (excludeColumn.Contains(exclStr))                                                {                                                 }                                                else                                                {                                                    if (TemplatePath == "")                                                    {                                                        worksheet.Cells[r + 2, colPosition + 1] = item.Rows[k][t].ToString();                                                    }                                                    else                                                    {                                                        worksheet.Cells[r + TemplateRow, colPosition + 1] = item.Rows[k][t].ToString();                                                    }                                                    colPosition++;                                                }                                            }                                            rowRead++;                                            percent = ((float)(100 * rowRead)) / totalCount;                                        }                                    }                                }                                #endregion                                #endregion                            }; break;                        default:                            break;                    }                 }                else                {                    //生成每行数据                    int r = 0;                    for (r = 0; r < dt.Rows.Count; r++)                    {                        //生成每列数据                        if (TemplatePath == "")                        {                            for (int i = 0; i < dt.Columns.Count; i++)                            {                                worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString();                            }                        }                        else                        {                            for (int i = 0; i < dt.Columns.Count; i++)                            {                                worksheet.Cells[r + 1 + TemplateRow, i + 1] = dt.Rows[r][i].ToString();                            }                        }                        rowRead++;                        percent = ((float)(100 * rowRead)) / totalCount;                    }                }                #endregion                switch (pathType)                {                    case "0": { workbook.Saved = false; }; break;                    case "1": { workbook.Saved = true; workbook.SaveCopyAs(excelPathName); }; break;                    default:                        return false;                }                xlApp.Visible = false;//是否在服务器打开                workbook.Close(true, Type.Missing, Type.Missing);                workbook = null;                xlApp.Quit();                xlApp = null;                return true;            }            catch (Exception)            {                return false;            }        }         /// <summary>        /// 从DataTable导出Excel,指定列别名        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="colName">各列的列名List string </param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForDataTableC(System.Data.DataTable dt, string excelPathName, string pathType, List<string> colName, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> excludeColumn = new List<string>();            string excludeType = "0";            return ToExcelForDataTable(dt, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }         /// <summary>        /// 从DataTable导出Excel,指定要排除的列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="excludeColumn">要显示/排除的列</param>        /// <param name="excludeType">显示/排除列方式 0为所有列 1指定的为要显示的列 2指定的为要排除的列</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForDataTableE(System.Data.DataTable dt, string excelPathName, string pathType, List<string> excludeColumn, string excludeType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> colName = new List<string>();            return ToExcelForDataTable(dt, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }        /// <summary>        /// 从DataTable导出Excel,使用默认列名,不排除导出任何列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForDataTableZ(System.Data.DataTable dt, string excelPathName, string pathType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> colName = new List<string>();            List<string> excludeColumn = new List<string>();            string excludeType = "0";            return ToExcelForDataTable(dt, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }        #endregion         #region ExportExcelForModelList        /// <summary>        /// 从DataTable导出Excel,指定列别名,指定要排除的列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="colName">各列的列名List string </param>        /// <<param name="excludeColumn">要显示/排除的列</param>        /// <param name="excludeType">显示/排除列方式 0为所有列 1指定的为要显示的列 2指定的为要排除的列</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForModelList<T>(List<T> md, string excelPathName, string pathType, List<string> colName, List<string> excludeColumn, string excludeType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            System.Data.DataTable dt = ModelListToDataTable(md);            return ToExcelForDataTable(dt, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }         /// <summary>        /// 从DataTable导出Excel,指定列别名        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="colName">各列的列名List string </param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForModelListC<T>(List<T> md, string excelPathName, string pathType, List<string> colName, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> excludeColumn = new List<string>();            string excludeType = "0";            return ToExcelForModelList(md, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }         /// <summary>        /// 从DataTable导出Excel,指定要排除的列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="excludeColumn">要显示/排除的列</param>        /// <param name="excludeType">显示/排除列方式 0为所有列 1指定的为要显示的列 2指定的为要排除的列</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForModelListE<T>(List<T> md, string excelPathName, string pathType, List<string> excludeColumn, string excludeType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> colName = new List<string>();            return ToExcelForModelList(md, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }         /// <summary>        /// 从DataTable导出Excel,使用默认列名,不排除导出任何列        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="excelPathName">含Excel名称的保存路径 在pathType=1时有效,其它请赋值空字符串</param>        /// <param name="pathType">路径类型。只能取值:0客户自定义路径;1服务端定义路径,标识文件保存路径是服务端指定还是客户自定义路径及文件名</param>        /// <param name="sheetName">sheet1的名称 为空字符串时保持默认名称</param>        /// <param name="TemplatePath">模版在项目服务器中路径 为空字符串时表示无模版</param>        /// <param name="TemplateRow">模版中已存在数据的行数,无模版时请传入参数 0</param>        /// <param name="exDataTableList">扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又都在同一列时使用,要求格式与参数第一个 DataTable的列名字段名一至,仅字段类型可不同</param>        /// <returns>bool</returns>        public static bool ToExcelForModelListZ<T>(List<T> md, string excelPathName, string pathType, string sheetName, string TemplatePath, int TemplateRow, List<System.Data.DataTable> exDataTableList)        {            List<string> colName = new List<string>();            List<string> excludeColumn = new List<string>();            string excludeType = "0";            return ToExcelForModelList(md, excelPathName, pathType, colName, excludeColumn, excludeType, sheetName, TemplatePath, TemplateRow, exDataTableList);        }        #endregion         #region 从DataTable导出Excel; ToExcelModel实体传参        /// <summary>        /// 从DataTable导出Excel; ToExcelModel实体传参        /// </summary>        /// <param name="tem">ExcelHelper.ToExcelModel</param>        /// <returns></returns>        public static bool ToExcelForDataTable(ToExcelModel tem)        {            if (tem != null)            {                return ToExcelForDataTable(tem.DataTable, tem.excelPathName, tem.pathType, tem.colNameList, tem.excludeColumn, tem.excludeType, tem.sheetName, tem.TemplatePath, tem.TemplateRow, tem.exDataTableList);            }            else            {                return false;            }        }        #endregion         #region Model To DataTable        /// <summary>        /// 实体类转换成DataTable        /// </summary>        /// <param name="modelList">实体类列表</param>        /// <returns></returns>        public static System.Data.DataTable ModelListToDataTable<T>(List<T> modelList)        {            System.Data.DataTable dtReturn = new System.Data.DataTable();             // column names            PropertyInfo[] oProps = null;             if (modelList == null) return dtReturn;             foreach (T rec in modelList)            {                if (oProps == null)                {                    oProps = ((Type)rec.GetType()).GetProperties();                    foreach (PropertyInfo pi in oProps)                    {                        Type colType = pi.PropertyType;                         if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()                        == typeof(Nullable<>)))                        {                            colType = colType.GetGenericArguments()[0];                        }                         dtReturn.Columns.Add(new DataColumn(pi.Name, colType));                    }                }                 DataRow dr = dtReturn.NewRow();                 foreach (PropertyInfo pi in oProps)                {                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue                    (rec, null);                }                 dtReturn.Rows.Add(dr);            }            return dtReturn;        }        #endregion         #region 说明 如何使用        /*         * 功能:         *      1、将System.Data.DataTable数据导出到Excel文件         *      2、将Model(Entity)数据实体导出到Excel文件         * 完整调用:         *      1、ExcelHelper.ToExcelForDataTable(DataTable,excelPathName,pathType,colName,excludeColumn,excludeType,sheetName,TemplatePath,TemplateRow,exDataTableList);         *      2、ExcelHelper.ToExcelForModelList(Model,excelPathName,pathType,colName,excludeColumn,excludeType,sheetName,TemplatePath,TemplateRow,exDataTableList);         * 参数说明:         *      1、DataTable:DataSet.DataTable[0];数据表         *      2、Model:Model.Users users = new Model.Users(){...};数据实体         *      3、excelPathName:含Excel名称的保存路径 在pathType=1时有效。用户自定义保存路径时请赋值空字符串 ""。格式:"E://456.xlsx"         *      4、pathType:路径类型。只能取值:0用户自定义路径,弹出用户选择路径对话框;1服务端定义路径。标识文件保存路径是服务端指定还是客户自定义路径及文件名,与excelPathName参数合用         *      5、colName:各列的列别名List string,比如:字段名为userName,此处可指定为"用户名",并以此显示         *      6、excludeColumn:要显示/排除的列,指定这些列用于显示,或指定这些列用于不显示。倒低这些列是显示还是不显示,由excludeType参数决定         *      7、excludeType:显示/排除列方式。 0为显示所有列 1指定的是要显示的列 2指定的是要排除的列,与excludeColumn合用         *      8、sheetName:sheet1的名称,要使期保持默认名称请指定为空字符串 ""         *      9、TemplatePath:模版在项目服务器中路径 例:tp.xlsx 。当为空字符串 "" 时表示无模版         *      10、TemplateRow:模版中已存在数据的行数,与TemplatePath合用,无模版时请传入参数 0         *      11、exDataTableList:扩展 DataTable List 用于当上下两个及以上DataTable数据类型不一至,但又

调用示例:

导出---

/// <summary>        /// 导出至Excel        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        protected void Button1_Click(object sender, EventArgs e)        {            try            {                BLL.COMMON_UserInfo cu = new BLL.COMMON_UserInfo();                List<Model.COMMON_UserInfo> cuiList = cu.GetModelList("");                BLL.manager_log bll = new BLL.manager_log();                DataSet ds = bll.GetList(10, "", "id");                System.Data.DataTable dt = ds.Tables[0];                List<string> colName = new List<string>() {                 "用户ID",                "用户名",                "备注",                "用户IP",                "操作时间",                "操作时间1",                "操作时间2"                };                List<string> tt = new List<string>() { "action_type" };                DataSet dss = bll.GetList(10, "", "id");                List<System.Data.DataTable> dtss = new List<System.Data.DataTable>();                dtss.Add(dss.Tables[0]);                dtss.Add(dss.Tables[0]);                dtss.Add(dss.Tables[0]);                ExcelHelper.ToExcelForDataTable(dt, Server.MapPath("~").ToString() + "456.xlsx", "0", colName, tt, "2", "", "", 0, dtss);//指定了列别名,指定了要排除的列                ToExcelModel tem = new ToExcelModel()                {                    DataTable = dt,                    excelPathName = "",                    pathType = "0",                    colNameList = colName,                    excludeColumn = tt,                    excludeType = "0",                    sheetName = "成功",                    TemplatePath = "",                    TemplateRow = 0,                    exDataTableList = dtss                };                ExcelHelper.ToExcelForDataTable(tem);            }            catch (Exception ex)            {                FileLog.Log(ex.Message, "ExportExcelByDataTable");            }        }

导入---

/// <summary>        /// 导入数据到数据库        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        protected void Button2_Click(object sender, EventArgs e)        {            string excelFile = "G://123.xls";            string sheetName = "Sheet1";            string DbTableName = "test_new_table";// "test_new_table";            List<string> columnType = new List<string>() {                 "int",                "nvarchar(100)",                "decimal(18,2)",                "nvarchar(100)",                "datetime"            };            string connectionString = "server=.;database=Test1;uid=sa;pwd=zhangquan;";            ExcelHelper.FromExcel(excelFile, sheetName, DbTableName, columnType, connectionString);        }

[.Net] C# Excel操作类 ExcelHelper