1. public static class XSSFWorkbook_Excel
  2. {
  3. /// <summary>
  4. /// GetExcel
  5. /// </summary>
  6. /// <param name="list">结果结合</param>
  7. /// <param name="parms_header">要输出的字段名</param>
  8. /// <param name="filename">文件名</param>
  9. /// <returns></returns>
  10. public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename)
  11. {
  12. HttpContext curContext = HttpContext.Current;
  13. try
  14. {
  15. MemoryStream ms = new MemoryStream();
  16. EntityListToExcel(list, parms_header, "Sheet1").Write(ms);
  17. curContext.Response.Clear();
  18. curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xlsx");
  19. curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
  20. curContext.Response.ContentEncoding = Encoding.UTF8;
  21. curContext.Response.ContentType = "application/ms-excel";
  22. curContext.Response.BinaryWrite(ms.ToArray());
  23. ms.Close();
  24. ms.Dispose();
  25. curContext.Response.Flush();
  26. curContext.Response.End();
  27. }
  28. catch (Exception ex)
  29. {
  30. curContext.Response.Flush();
  31. curContext.Response.End();
  32. }
  33. finally
  34. {
  35. curContext.Response.Flush();
  36. curContext.Response.End();
  37. }
  38. return true;
  39. }
  40.  
  41. /// <summary>
  42. /// GetExcel
  43. /// </summary>
  44. /// <param name="list">结果结合</param>
  45. /// <param name="parms_header">要输出的字段名</param>
  46. /// <param name="filename">文件名</param>
  47. /// <param name="path">下载地址</param>
  48. /// <returns></returns>
  49. public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename, out string path)
  50. {
  51. path = "/Export/" + filename + ".xlsx";
  52. try
  53. {
  54. using (FileStream stm = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + path))
  55. {
  56. EntityListToExcel(list, parms_header, "Sheet1").Write(stm);
  57. }
  58. return true;
  59. }
  60. catch (Exception ex)
  61. {
  62. return false;
  63. }
  64. }
  65. public static XSSFWorkbook EntityListToExcel(IList list, IDictionary<string, string> parms_header, string sheetName)
  66. {
  67. try
  68. {
  69. XSSFWorkbook workbook = new XSSFWorkbook();
  70. //workbook.SetSheetHidden(,)
  71. //HSSFWorkbook workbook = new HSSFWorkbook();
  72. ISheet sheet = workbook.CreateSheet(sheetName);
  73. IRow row = sheet.CreateRow();
  74. List<string> keys = parms_header.Keys.ToList();
  75. for (int i = ; i < keys.Count; i++)
  76. {
  77. row.CreateCell(i).SetCellValue(parms_header[keys[i]]);
  78. }
  79. int rowIndex = ;
  80. foreach (var item in list)
  81. {
  82. IRow rowTmp = sheet.CreateRow(rowIndex);
  83. for (int i = ; i < keys.Count; i++)
  84. {
  85. string cellValue = "";
  86. object properotyValue = null;
  87. System.Reflection.PropertyInfo properotyInfo = null;
  88.  
  89. if (keys[i].IndexOf(".") >= )
  90. {
  91. string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  92. string subClassName = properotyArray[];
  93. string subClassProperotyName = properotyArray[];
  94. System.Reflection.PropertyInfo subClassInfo = item.GetType().GetProperty(subClassName);
  95. if (subClassInfo != null)
  96. {
  97. var subClassEn = item.GetType().GetProperty(subClassName).GetValue(item, null);
  98. properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName);
  99. if (properotyInfo != null)
  100. {
  101. properotyValue = properotyInfo.GetValue(subClassEn, null);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. properotyInfo = item.GetType().GetProperty(keys[i]);
  108. if (properotyInfo != null)
  109. {
  110. properotyValue = properotyInfo.GetValue(item, null);
  111. }
  112. }
  113. if (properotyValue != null)
  114. {
  115. cellValue = properotyValue.ToString();
  116. if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
  117. {
  118. cellValue = string.Empty;
  119. }
  120. }
  121. rowTmp.CreateCell(i).SetCellValue(cellValue);
  122. }
  123. rowIndex++;
  124. }
  125.  
  126. for (int i = ; i <= parms_header.Count(); i++)
  127. {
  128. sheet.AutoSizeColumn(i);
  129. }
  130.  
  131. for (int columnNum = ; columnNum <= parms_header.Count(); columnNum++)
  132. {
  133. int columnWidth = sheet.GetColumnWidth(columnNum) / ;
  134. for (int rowNum = ; rowNum <= sheet.LastRowNum; rowNum++)
  135. {
  136. IRow currentRow;
  137. if (sheet.GetRow(rowNum) == null)
  138. {
  139. currentRow = sheet.CreateRow(rowNum);
  140. }
  141. else
  142. {
  143. currentRow = sheet.GetRow(rowNum);
  144. }
  145.  
  146. if (currentRow.GetCell(columnNum) != null)
  147. {
  148. ICell currentCell = currentRow.GetCell(columnNum);
  149. int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
  150. if (columnWidth < length)
  151. {
  152. columnWidth = length;
  153. }
  154. }
  155. }
  156. sheet.SetColumnWidth(columnNum, columnWidth * );
  157. }
  158.  
  159. return workbook;
  160. }
  161. catch (Exception ex)
  162. {
  163. throw ex;
  164. }
  165. }
  166. }
  1. jQuery.download = function (data) {
  2. var inputs = '';
  3. Object.keys(data).forEach(function (v) {
  4. inputs = inputs + '<input type="hidden" name="' + v + '" value="' + data[v] + '" />';
  5. })
  6. jQuery('<form action="/xykj/system/" method="post">' + inputs + '</form>')
  7. .appendTo('body').submit().remove();
  8. };
  9. function Export() {
  10. var parm = {
  11. export: 1,
  12. }
  13. $.each($("#search").serializeArray(), function () {
  14. if (parm[this.name]) {
  15. if (!parm[this.name].push) {
  16. parm[this.name] = [parm[this.name]];
  17. }
  18. parm[this.name].push(this.value || '');
  19. } else {
  20. parm[this.name] = this.value || '';
  21. }
  22. })
  23. $.download(parm);
  24. }

使用Ajax提交会导致 Response.Flush();无效

c# NPOI aspx导出数据的更多相关文章

  1. 使用npoi.dll导出数据到excel

    .net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...

  2. asp.net使用MVC4框架基于NPOI做导出数据到Excel表

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  3. NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏

    应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面:   ...

  4. NPOI导出数据到Excel

    NPOI导出数据到Excel   前言 Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微 ...

  5. 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中

    using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...

  6. 利用NPOI导出数据到Execl

    相信很多童鞋都开发过Execl的导入导出功能,最近产品中无论是后台数据分析的需要,还是前端满足用户管理的方便,都有Execl导入导出的维护需求产生. 以前做这个功能,如果是web,利用HttpCont ...

  7. NPOI读取excel文件导出数据, 而此时文件正在打开中抛异常怎么办

    项目中需要用到一些数值表格, 方便起见都是用excel来的. 而如果excel正打开中, 直接使用npoi制作的工具来导出数据的话, 在这一行将会异常: workbook = new XSSFWork ...

  8. winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中

    1.通过NUGET管理器下载nopi,在引入命令空间 using System; using System.Collections.Generic; using System.Text; using ...

  9. NET使用NPOI组件将数据导出Excel-通用方法 【推荐】

    一.Excel导入及导出问题产生:   从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题:   导出问题:   如果是as ...

随机推荐

  1. LuoguP2698 【[USACO12MAR]花盆Flowerpot】

    题目描述 首先我们简化一下题意: 要找一段区间[L,R],使区间[L,R]内元素最大值减最小值大于等于D. 做法: 首先很容易想到采用二分,分什么呢? 我们二分区间长度为mid 这个时候,检验就成为了 ...

  2. Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】

    作者QQ:(648437169) 点击下载➨delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA ...

  3. 简单的爬虫程序以及使用PYQT进行界面设计(包含源码解析)

    由于这个是毕业设计的内容,而且还是跨专业的.爬虫程序肯定是很简单的,就是调用Yahoo的API进行爬取图片.这篇博客主要讲的是基础的界面设计. 放上源码,然后分部解析一下重要的地方.注:flickra ...

  4. STM32 EV1527无线通信(433)

    EV1527无线通信 先说一下这个通信协议的数据格式,这个图片是我在手册里截的. 大家按照单片机类型计算周期,我的是STM32f103vb (4CLK大致等于350um) 发送时按照 先发同步码后发D ...

  5. Go MongoDB官方数据库驱动之增删改查

    package main import ( "context" "fmt" "log" "go.mongodb.org/mongo ...

  6. Go基础编程实践(七)—— 并发

    同时运行多个函数 观察常规代码和并发代码的输出顺序. // 常规代码,顺序执行,依次输出 package main import ( "fmt" "time" ...

  7. vue SPA设计 history hash

    <body> <h3>Histort api</h3> <a class="api a">a,html</a> < ...

  8. P1018 乘积最大(DP)

    题目 P1018 乘积最大 解析 区间DP 设\(f[i][j]\)表示选\(i\)个数,插入\(j\)个乘号时的最大值 设\(num[i][j]\)是\(s[i,j]\)里的数字 转移方程就是\(f ...

  9. python二维数组切片

    python中list切片的使用非常简洁.但是list不支持二维数组.仔细研究了一下发现,因为list不是像nampy数组那么规范.list非常灵活.所以没办法进行切片操作. 后来想了两个办法来解决: ...

  10. Swiper4的基本使用

    基本介绍: 中文文档地址:https://www.swiper.com.cn/ 它是一个开源,免费,强大的触摸滑动插件. 它是用纯Javascript打造的滑动特效插件,既可用于PC端,也可用于移动端 ...