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 這一塊是由於公司有個需求 要在單元格內進行換行,有多少個\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的圖表和如今的單元格內換行啊,痛苦的不行,因此不能讓大家太幸福了  哈哈~~~~~~~~~~~~~~~~~~~~~~~~~~app

相關文章
相關標籤/搜索