[.Net] C# Excel操作类 ExcelHelper
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[.Net] C# Excel操作类 ExcelHelper,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含22671字,纯文字阅读大概需要33分钟。
内容图文
![[.Net] C# Excel操作类 ExcelHelper](/upload/InfoBanner/zyjiaocheng/1048/7b52791406f54b5daadf008b5f483000.jpg)
实现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>publicstaticbool 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) returnfalse; Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { returnfalse; } 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; } } #endregionswitch (pathType) { case"0": { workbook.Saved = false; }; break; case"1": { workbook.Saved = true; workbook.SaveCopyAs(excelPathName); }; break; default: returnfalse; } xlApp.Visible = false;//是否在服务器打开 workbook.Close(true, Type.Missing, Type.Missing); workbook = null; xlApp.Quit(); xlApp = null; returntrue; } catch (Exception) { returnfalse; } } ///<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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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>publicstaticbool 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 { returnfalse; } } #endregion#region Model To DataTable ///<summary>/// 实体类转换成DataTable ///</summary>///<param name="modelList">实体类列表</param>///<returns></returns>publicstatic 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); }
原文:http://www.cnblogs.com/jqmtony/p/4110933.html
内容总结
以上是互联网集市为您收集整理的[.Net] C# Excel操作类 ExcelHelper全部内容,希望文章能够帮你解决[.Net] C# Excel操作类 ExcelHelper所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。