public void Export()
{ string fileName = "";
if (string.IsNullOrEmpty(fileName) == true)
{
fileName = DateTime.Now.ToString("yyyyMMdd");
}
string tmpPath = context.Server.MapPath("~/upfiles/") + "Template_" + DateTime.Now.Ticks + ".xlsx";
FileInfo file = new FileInfo(tmpPath);
//导出Excel
FileStream fs = new FileStream(tmpPath, FileMode.CreateNew, FileAccess.ReadWrite);
try
{
ExcelPackage package = new ExcelPackage(fs);
DataSet ds = new DataSet();
//接收最后结果
string drResult = string.Empty;
DataTable dt = pmt.GetMyTaskList().Tables[];
#region 这一块是因为公司有个需求 要在单元格内进行换行,有多少个\n就需要换多少行,如果没有这个需求的 就不用要这一段了。
foreach (DataRow dr in dt.Rows) {
string [] st4=dr["approvalInfo"].ToString().Split(new string[] { "\\n" }, StringSplitOptions.None);
for(int i = ; i < st4.Length; i++)
{
drResult += st4[i] + " " + System.Environment.NewLine; //换行的关键字,这一句掉了 就没什么卵用了,为了这个换行,我差点没被逼死,这次就留着,我不想下次又找不到了。
}
dr["approvalInfo"] = drResult;
drResult = ""; }
#endregion
#region
ExcelWorksheet sheet2 = package.Workbook.Worksheets.Add("sheet1");
sheet2.DefaultColWidth = ;
sheet2.Cells.Style.Font.Name = "宋体";
sheet2.Cells.Style.Numberformat.Format = "@";
sheet2.Cells.Style.WrapText = true;
sheet2.Cells["A1"].LoadFromDataTable(dt, true); ///设置边框
sheet2.Cells[, , , dt.Columns.Count].Style.Fill.PatternType = ExcelFillStyle.Solid;
sheet2.Cells[, , , dt.Columns.Count].Style.Fill.BackgroundColor.SetColor(Color.LightGray); sheet2.Cells[, , , dt.Columns.Count].Style.Fill.PatternType = ExcelFillStyle.Solid;
sheet2.Cells[, , , dt.Columns.Count].Style.Fill.BackgroundColor.SetColor(Color.LightGray);
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.Border.Left.Style = ExcelBorderStyle.Thin;
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.Border.Right.Style = ExcelBorderStyle.Thin;
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.Border.Top.Style = ExcelBorderStyle.Thin;
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
sheet2.Cells[, , dt.Rows.Count + , dt.Columns.Count].Style.ShrinkToFit = true;//自适应宽度
#endregion package.Save();
}
catch (Exception ex)
{
Logger.Log.Error("导出Excel错误:" + ex.ToString());
}
finally
{
fs.Close();
} byte[] data = null; FileStream steam = File.Open(tmpPath, FileMode.Open, FileAccess.Read);
data = new byte[steam.Length];
int result = steam.Read(data, , data.Length);
if (data.Length <= )
data = new Byte[] { , };
steam.Close();
File.Delete(tmpPath); if (context.Request.UserAgent.ToLower().IndexOf("firefox") > -)
{
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename*=\"" +
HttpUtility.UrlEncode(fileName + ".xlsx") + "\"");
}
else
{
HttpContext.Current.Response.AddHeader("Content-Disposition",
"inline; filename=" +
HttpUtility.UrlEncode(fileName + ".xlsx", System.Text.Encoding.UTF8));
}
HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(data);
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}

单行读取的我就不写了网上多得很。 在写就有多多此一举了,  新人要不断摸索啊,我可是踩了半个月的坑,一个人日日夜夜的研究,才搞出来Epplus的图表和现在的单元格内换行啊,痛苦的不行,所以不能让你们太幸福了  哈哈~~~~~~~~~~~~~~~~~~~~~~~~~~

最新文章

  1. [解决]小程序要求的 TLS 版本必须大于等于 1.2
  2. Linux配置无线网卡驱动实现无线上网
  3. jQuery Mobile Data 属性
  4. WebGame开发总结
  5. mysql同步 小问题
  6. Java中常用来处理时间的三个类:Date、Calendar、SimpleDateFormate,以及Java中的单例设计模式:懒汉式、饿汉式以及静态内部类式
  7. jquery 循环数组输出显示在html页面
  8. 2017-2018-1 1623 bug终结者 冲刺005
  9. Nmap小技巧——探测大网络空间中的存活主机
  10. MySQL 栏位修改为区分大小写
  11. leetcode 639 Decode Ways II
  12. laravel整合workerman做聊天室
  13. OUTLOOK2019 解决 无法验证您连接到的服务器使用的安全证书
  14. shell 终端常用插件
  15. [Artoolkit] Android Sample of nftSimple
  16. Prometheus Node_exporter 之 Network Sockstat
  17. CAN总线典型特征
  18. WIFEXITED WEXITSTATUS WIFSIGNALED(转)
  19. ubuntu16.04配置java环境(重启后不会失效)
  20. Linux驱动:LCD驱动测试

热门文章

  1. python基础篇_005_迭代器和生成器
  2. 译:Dataiku 白皮书之《在银行和保险行业应用数据科学》
  3. python 类和元类(metaclass)的理解和简单运用
  4. PostgreSQL自学笔记:3 数据库的基本操作
  5. CSS(七)
  6. Codechef August Challenge 2018 : Chef at the River
  7. CSS3 三次贝塞尔曲线(cubic-bezier)
  8. 08_ for 练习 _ sumOf7
  9. Maven的安装、配置及使用入门+maven安装报错:JAVA_HOME【申明:来源于网络】
  10. HCNA-链路聚合(手工模式)