【推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出

项目需引用NPOI的NuGet包:

A-2:EXCEL数据导出--Web项目--C#代码导出:

/// <summary>
/// EXCEL帮助类
/// </summary>
/// <typeparam name="T">泛型类</typeparam>
/// <typeparam name="TCollection">泛型类集合</typeparam>
public class ExcelHelp<T, TCollection> where T : new() where TCollection : List<T>, new()
{
//http请求Request对象
public static HttpRequest baseRequest = HttpContext.Current.Request;
//http请求Response对象
public static HttpResponse baseResponse = HttpContext.Current.Response;/// <summary>
/// 将数据导出EXCEL
/// </summary>
/// <param name="columnNameAndShowNameDic">列名+显示名</param>
/// <param name="tColl">数据集(tColl里的类属性名必须和字典中的列名一致)</param>
public static void ExportExcelData(Dictionary<string, string> columnNameAndShowNameDic, TCollection tColl)
{
WorkBook workbook = WorkBook.CreateNew();
WorkSheet worksheet = workbook.Sheets["sheet1"]; List<string> columnNameList = columnNameAndShowNameDic.Keys.ToList();
List<string> showNameList = columnNameAndShowNameDic.Values.ToList();
//暂定26行
string[] zm = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
//设置首列显示
for (var i = 0; i < columnNameList.Count; i++)
{
worksheet.Cells[zm[i] + "1"].Value = showNameList[i];
//加粗
worksheet.Cells[zm[i] + "1"].Style.Font.Bold = true;
}
for (int i = 0; i < tColl.Count; i++)
{
for (int j = 0; j < columnNameList.Count; j++)
{
worksheet.Cells[zm[j] + (i + 2)].Value = getPropertyValue(tColl[i], columnNameList[j]);
}
}
byte[] buffer = workbook.SaveAsBytes();
baseResponse.Clear();
baseResponse.Buffer = true;
baseResponse.ContentEncoding = System.Text.Encoding.UTF8;
//baseResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
baseResponse.ContentType = "application/vnd.ms-excel";
//设置导出文件名
baseResponse.AddHeader("content-disposition", "attachment; filename=" + "MaintainReport" + ".xlsx");
baseResponse.AddHeader("Content-Length", buffer.Length.ToString()); baseResponse.BinaryWrite(buffer);
baseResponse.Flush();
baseResponse.End();
}
/// <summary>
/// 获取属性值
/// </summary>
/// <param name="t">T对象实例</param>
/// <param name="propertyName">属性名</param>
/// <returns></returns>
public static string getPropertyValue(T t, string propertyName)
{
PropertyInfo info = t.GetType().GetProperty(propertyName);
//获取属性值转换暂设置如下字段,可根据实际情况添加
if (info.PropertyType == typeof(DateTime))
{
return Convert.ToDateTime(info.GetValue(t)).ToString("yyyy-MM-dd HH:mm");
}
return info.GetValue(t).ToString();
}
}

最新文章

  1. ROS 5.x自动定时备份并发送到邮箱(实用)
  2. git usage:常用git命令
  3. .NET ORM工具Pax实战
  4. react native 学习一(环境搭配和常见错误的解决)
  5. knockout 学习实例3 html
  6. jQuery Mobile 连接外部连接或切换动画
  7. Perl常用特殊变量
  8. Unity3d webplayer获取url参数
  9. Hibernate annotation多对多配置
  10. SpringBoot配置多数据源时遇到的问题
  11. 4.3 if-else语句使用
  12. Springboot:java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
  13. canvas实现验证码功能
  14. vue基础——模板语法
  15. JdbcTemplate实现CRUD操作
  16. 基于 Vue BootStrap的迷你Chrome插件
  17. leetcode112
  18. springMVC入门案例
  19. 在 Web 应用中创建 Node.js 应用程序
  20. thinkphp更新数据库的时候where(&#39;&#39;)为字符串

热门文章

  1. 使用littlefs-fuse在PC端调试littlefs文件系统
  2. ASP.NET CORE 使用Consul实现服务治理与健康检查(2)——源码篇
  3. iOS-关于一些取整方式
  4. Python3-logging日志模块
  5. linux vscode 编译配置
  6. spingboot 2.1.3 与 elasticsearch7 兼容问题
  7. 通过requestAnimationFrame判断浏览器帧率
  8. 大数据-hadoop-MapReduce计算流程
  9. zabbix批量清理模板,添加新模板
  10. python将字符串插入表中避免单双引号问题