代码改变世界

Epplus DataTable一次性导出

2017-12-18 14:22  金金金金金  阅读(1882)  评论(1编辑  收藏  举报
        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[0];
#region 这一块是因为公司有个需求 要在单元格内进行换行,有多少个\n就需要换多少行,如果没有这个需求的 就不用要这一段了。
foreach (DataRow dr in dt.Rows) { string [] st4=dr["approvalInfo"].ToString().Split(new string[] { "\\n" }, StringSplitOptions.None); for(int i = 0; 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 = 18; sheet2.Cells.Style.Font.Name = "宋体"; sheet2.Cells.Style.Numberformat.Format = "@"; sheet2.Cells.Style.WrapText = true; sheet2.Cells["A1"].LoadFromDataTable(dt, true); ///设置边框 sheet2.Cells[1, 1, 1, dt.Columns.Count].Style.Fill.PatternType = ExcelFillStyle.Solid; sheet2.Cells[1, 1, 1, dt.Columns.Count].Style.Fill.BackgroundColor.SetColor(Color.LightGray); sheet2.Cells[2, 1, 2, dt.Columns.Count].Style.Fill.PatternType = ExcelFillStyle.Solid; sheet2.Cells[2, 1, 2, dt.Columns.Count].Style.Fill.BackgroundColor.SetColor(Color.LightGray); sheet2.Cells[1, 1, dt.Rows.Count + 1, dt.Columns.Count].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center; sheet2.Cells[1, 1, dt.Rows.Count + 1, dt.Columns.Count].Style.Border.Left.Style = ExcelBorderStyle.Thin; sheet2.Cells[1, 1, dt.Rows.Count + 1, dt.Columns.Count].Style.Border.Right.Style = ExcelBorderStyle.Thin; sheet2.Cells[1, 1, dt.Rows.Count + 1, dt.Columns.Count].Style.Border.Top.Style = ExcelBorderStyle.Thin; sheet2.Cells[1, 1, dt.Rows.Count + 1, dt.Columns.Count].Style.Border.Bottom.Style = ExcelBorderStyle.Thin; sheet2.Cells[1, 1, dt.Rows.Count + 1, 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, 0, data.Length); if (data.Length <= 0) data = new Byte[] { 13, 10 }; steam.Close(); File.Delete(tmpPath); if (context.Request.UserAgent.ToLower().IndexOf("firefox") > -1) { 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的图表和现在的单元格内换行啊,痛苦的不行,所以不能让你们太幸福了  哈哈~~~~~~~~~~~~~~~~~~~~~~~~~~