zoukankan      html  css  js  c++  java
  • Epplus DataTable一次性导出

            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 这一块是因为公司有个需求 要在单元格内进行换行,有多少个 就需要换多少行,如果没有这个需求的 就不用要这一段了。
    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的图表和现在的单元格内换行啊,痛苦的不行,所以不能让你们太幸福了  哈哈~~~~~~~~~~~~~~~~~~~~~~~~~~

  • 相关阅读:
    图论复习
    数据结构复习
    数学复习
    字符串算法复习
    还有9天就要走了
    JS 多个一维数组排例组合的所有可能
    PHP 多个一维数组排列组合的所有可能
    维护DOWNID
    LINUX
    Multiple Versions of PHP run on server
  • 原文地址:https://www.cnblogs.com/Cjb8973/p/8057407.html
Copyright © 2011-2022 走看看