public static class XSSFWorkbook_Excel
{
/// <summary>
/// GetExcel
/// </summary>
/// <param name="list">结果结合</param>
/// <param name="parms_header">要输出的字段名</param>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename)
{
HttpContext curContext = HttpContext.Current;
try
{
MemoryStream ms = new MemoryStream();
EntityListToExcel(list, parms_header, "Sheet1").Write(ms);
curContext.Response.Clear();
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xlsx");
curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.ContentType = "application/ms-excel";
curContext.Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
curContext.Response.Flush();
curContext.Response.End();
}
catch (Exception ex)
{
curContext.Response.Flush();
curContext.Response.End();
}
finally
{
curContext.Response.Flush();
curContext.Response.End();
}
return true;
} /// <summary>
/// GetExcel
/// </summary>
/// <param name="list">结果结合</param>
/// <param name="parms_header">要输出的字段名</param>
/// <param name="filename">文件名</param>
/// <param name="path">下载地址</param>
/// <returns></returns>
public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename, out string path)
{
path = "/Export/" + filename + ".xlsx";
try
{
using (FileStream stm = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + path))
{
EntityListToExcel(list, parms_header, "Sheet1").Write(stm);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public static XSSFWorkbook EntityListToExcel(IList list, IDictionary<string, string> parms_header, string sheetName)
{
try
{
XSSFWorkbook workbook = new XSSFWorkbook();
//workbook.SetSheetHidden(,)
//HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName);
IRow row = sheet.CreateRow();
List<string> keys = parms_header.Keys.ToList();
for (int i = ; i < keys.Count; i++)
{
row.CreateCell(i).SetCellValue(parms_header[keys[i]]);
}
int rowIndex = ;
foreach (var item in list)
{
IRow rowTmp = sheet.CreateRow(rowIndex);
for (int i = ; i < keys.Count; i++)
{
string cellValue = "";
object properotyValue = null;
System.Reflection.PropertyInfo properotyInfo = null; if (keys[i].IndexOf(".") >= )
{
string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
string subClassName = properotyArray[];
string subClassProperotyName = properotyArray[];
System.Reflection.PropertyInfo subClassInfo = item.GetType().GetProperty(subClassName);
if (subClassInfo != null)
{
var subClassEn = item.GetType().GetProperty(subClassName).GetValue(item, null);
properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName);
if (properotyInfo != null)
{
properotyValue = properotyInfo.GetValue(subClassEn, null);
}
}
}
else
{
properotyInfo = item.GetType().GetProperty(keys[i]);
if (properotyInfo != null)
{
properotyValue = properotyInfo.GetValue(item, null);
}
}
if (properotyValue != null)
{
cellValue = properotyValue.ToString();
if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
{
cellValue = string.Empty;
}
}
rowTmp.CreateCell(i).SetCellValue(cellValue);
}
rowIndex++;
} for (int i = ; i <= parms_header.Count(); i++)
{
sheet.AutoSizeColumn(i);
} for (int columnNum = ; columnNum <= parms_header.Count(); columnNum++)
{
int columnWidth = sheet.GetColumnWidth(columnNum) / ;
for (int rowNum = ; rowNum <= sheet.LastRowNum; rowNum++)
{
IRow currentRow;
if (sheet.GetRow(rowNum) == null)
{
currentRow = sheet.CreateRow(rowNum);
}
else
{
currentRow = sheet.GetRow(rowNum);
} if (currentRow.GetCell(columnNum) != null)
{
ICell currentCell = currentRow.GetCell(columnNum);
int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
if (columnWidth < length)
{
columnWidth = length;
}
}
}
sheet.SetColumnWidth(columnNum, columnWidth * );
} return workbook;
}
catch (Exception ex)
{
throw ex;
}
}
}
jQuery.download = function (data) {
var inputs = '';
Object.keys(data).forEach(function (v) {
inputs = inputs + '<input type="hidden" name="' + v + '" value="' + data[v] + '" />';
})
jQuery('<form action="/xykj/system/" method="post">' + inputs + '</form>')
.appendTo('body').submit().remove();
};
function Export() {
var parm = {
export: 1,
}
$.each($("#search").serializeArray(), function () {
if (parm[this.name]) {
if (!parm[this.name].push) {
parm[this.name] = [parm[this.name]];
}
parm[this.name].push(this.value || '');
} else {
parm[this.name] = this.value || '';
}
})
$.download(parm);
}

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

最新文章

  1. Windows phone应用开发[19]-RSA数据加密
  2. Cocos2dx3.11.1Android播放视频,后台 黑屏,无法记忆播放bug修改
  3. java 调用 sql server存储过程
  4. Enterprise Solution 应用程序开发框架培训
  5. Swift 3 迁移工作总结
  6. First glance in Go
  7. 使用 IntraWeb (43) - 测试读取 SqLite (二)
  8. no sigar-amd64-winnt.dll in java.library.path 错误
  9. windows 服务安装脚本拾遗
  10. memcache的安装及管理
  11. View的工作原理(二)——layout
  12. Python学习笔记2-Python神奇的语法和格式化输出
  13. Confluence 6 从你的 JDBC 连接中直接启用校验查询
  14. C# 使用lambda表达式过滤掉数组中的空字符串
  15. EXCEL VBA——数组,使用数组提升程序效率
  16. (不用循环也可以记录数组里的数)Color the ball --hdu--1556
  17. Net::HTTP 一次添加 cookie, body 发送post请求
  18. Python调用C/C++程序
  19. nginx 启动重启脚本
  20. Error message: “&#39;chromedriver&#39; executable needs to be available in the path”

热门文章

  1. js拼接url以及为html某标签属性赋值
  2. C# vb .net实现缩放特效滤镜
  3. $(...).wordExport is not a function
  4. 在docker容器上如何实现代码的版本管理
  5. 自学Python编程的第十天(希望有IT大牛看见的指点小弟我,万分感谢)---------来自苦逼的转行人
  6. GoogleMap增加标记和路线轨迹的方法
  7. js跳出循环的方法区别(break,continue,return)(转载)
  8. SQL必知必会实践--mysql
  9. Linux环境变量设置declare/typeset
  10. QQ推广工具