From f39f00ee0ee41e169fa24ccb85b4af36090dbd25 Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期三, 08 七月 2020 16:38:51 +0800 Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M071 --- src/Bro.M071.DBManager/ExcelExportHelper.cs | 143 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 143 insertions(+), 0 deletions(-) diff --git a/src/Bro.M071.DBManager/ExcelExportHelper.cs b/src/Bro.M071.DBManager/ExcelExportHelper.cs new file mode 100644 index 0000000..b894e68 --- /dev/null +++ b/src/Bro.M071.DBManager/ExcelExportHelper.cs @@ -0,0 +1,143 @@ +锘縰sing OfficeOpenXml; +using OfficeOpenXml.Style; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Linq; + +namespace Bro.M071.DBManager +{ + /// <summary> + /// Excel瀵煎嚭甯姪绫� + /// </summary> + public class ExcelExportHelper + { + public static string ExcelContentType => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + /// <summary> + /// List杞珼ataTable + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="data"></param> + /// <returns></returns> + public static DataTable ListToDataTable<T>(List<T> data) + { + PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); + DataTable dataTable = new DataTable(); + for (int i = 0; i < properties.Count; i++) + { + PropertyDescriptor property = properties[i]; + dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); + } + object[] values = new object[properties.Count]; + foreach (T item in data) + { + for (int i = 0; i < values.Length; i++) + { + values[i] = properties[i].GetValue(item); + } + dataTable.Rows.Add(values); + } + return dataTable; + } + + /// <summary> + /// 瀵煎嚭Excel + /// </summary> + /// <param name="dataTable">鏁版嵁婧�</param> + /// <param name="heading">宸ヤ綔绨縒orksheet</param> + /// <param name="showSrNo">//鏄惁鏄剧ず琛岀紪鍙�</param> + /// <param name="columnsToTake">瑕佸鍑虹殑鍒�</param> + /// <returns></returns> + public static byte[] ExportExcel(DataTable dataTable, string heading = "", bool showSrNo = false, params string[] columnsToTake) + { + byte[] result; + using (ExcelPackage package = new ExcelPackage()) + { + ExcelWorksheet workSheet = package.Workbook.Worksheets.Add($"{heading}Data"); + int startRowFrom = string.IsNullOrEmpty(heading) ? 1 : 3; //寮�濮嬬殑琛� + //鏄惁鏄剧ず琛岀紪鍙� + if (showSrNo) + { + DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int)); + dataColumn.SetOrdinal(0); + int index = 1; + foreach (DataRow item in dataTable.Rows) + { + item[0] = index; + index++; + } + } + //Add Content Into the Excel File + workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true); + // autofit width of cells with small content + int columnIndex = 1; + foreach (DataColumn item in dataTable.Columns) + { + ExcelRange columnCells = workSheet.Cells[workSheet.Dimension.Start.Row, columnIndex, workSheet.Dimension.End.Row, columnIndex]; + int maxLength = columnCells.Max(cell => cell.Value.ToString().Count()); + if (maxLength < 150) + { + workSheet.Column(columnIndex).AutoFit(); + } + columnIndex++; + } + // format header - bold, yellow on black + using (ExcelRange r = workSheet.Cells[startRowFrom, 1, startRowFrom, dataTable.Columns.Count]) + { + r.Style.Font.Color.SetColor(System.Drawing.Color.White); + r.Style.Font.Bold = true; + r.Style.Fill.PatternType = ExcelFillStyle.Solid; + r.Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#1fb5ad")); + } + // format cells - add borders + using (ExcelRange r = workSheet.Cells[startRowFrom + 1, 1, startRowFrom + dataTable.Rows.Count, dataTable.Columns.Count]) + { + r.Style.Border.Top.Style = ExcelBorderStyle.Thin; + r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; + r.Style.Border.Left.Style = ExcelBorderStyle.Thin; + r.Style.Border.Right.Style = ExcelBorderStyle.Thin; + r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black); + r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black); + r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black); + r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black); + } + // removed ignored columns + for (int i = dataTable.Columns.Count - 1; i >= 0; i--) + { + if (i == 0 && showSrNo) + { + continue; + } + if (!columnsToTake.Contains(dataTable.Columns[i].ColumnName)) + { + workSheet.DeleteColumn(i + 1); + } + } + if (!string.IsNullOrEmpty(heading)) + { + workSheet.Cells["A1"].Value = heading; + workSheet.Cells["A1"].Style.Font.Size = 20; + workSheet.InsertColumn(1, 1); + workSheet.InsertRow(1, 1); + workSheet.Column(1).Width = 5; + } + result = package.GetAsByteArray(); + } + return result; + } + /// <summary> + /// 瀵煎嚭Excel + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="data"></param> + /// <param name="heading"></param> + /// <param name="isShowSlNo"></param> + /// <param name="columnsToTake"></param> + /// <returns></returns> + public static byte[] ExportExcel<T>(List<T> data, string heading = "", bool isShowSlNo = false, params string[] columnsToTake) + { + return ExportExcel(ListToDataTable(data), heading, isShowSlNo, columnsToTake); + } + } +} -- Gitblit v1.8.0