From 5241a8f6377dfd1618610dd15fd05ed6f51c8ba2 Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期三, 16 十二月 2020 11:19:45 +0800 Subject: [PATCH] 1. 添加默认流程和默认产品功能 2. 修改批量建立检测项功能 --- src/Bro.M071.DBManager/ExcelExportHelper.cs | 283 +++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 207 insertions(+), 76 deletions(-) diff --git a/src/Bro.M071.DBManager/ExcelExportHelper.cs b/src/Bro.M071.DBManager/ExcelExportHelper.cs index b894e68..b804e74 100644 --- a/src/Bro.M071.DBManager/ExcelExportHelper.cs +++ b/src/Bro.M071.DBManager/ExcelExportHelper.cs @@ -4,10 +4,34 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; +using System.IO; using System.Linq; namespace Bro.M071.DBManager { + + public class ExcelExportSet + { + public List<string> Worksheets { get; set; } = new List<string>(); + + /// <summary> + /// Key锛� Worksheet鐨勫悕绉� Value:Worksheet瀵瑰簲鐨勫垪鍚嶉泦鍚�(key 涓鸿瀵煎嚭鐨勫垪鍚� value 涓哄鍑哄悗鏄剧ず鐨勫垪鍚�) + /// </summary> + public Dictionary<string, Dictionary<string, string>> WorksheetColumns { get; set; } = new Dictionary<string, Dictionary<string, string>>(); + + public Dictionary<string, Dictionary<string, string>> WorksheetRows { get; set; } = new Dictionary<string, Dictionary<string, string>>(); + + public Dictionary<string, DataTable> WorksheetDataTable { get; set; } = new Dictionary<string, DataTable>(); + + //public ExcelExportSet() + //{ + // Worksheets = new List<string>(); + // WorksheetColumns = new Dictionary<string, Dictionary<string, string>>(); + // WorksheetDataTable = new Dictionary<string, DataTable>(); + //} + + } + /// <summary> /// Excel瀵煎嚭甯姪绫� /// </summary> @@ -20,21 +44,27 @@ /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> - public static DataTable ListToDataTable<T>(List<T> data) + public static DataTable ListToDataTable<T>(List<T> data, Dictionary<string, string> worksheetColumns) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable dataTable = new DataTable(); - for (int i = 0; i < properties.Count; i++) + Dictionary<string, string> tempColumns = new Dictionary<string, string>(); + foreach (var column in worksheetColumns) { - PropertyDescriptor property = properties[i]; - dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); + PropertyDescriptor property = properties.Find(column.Key, true); + if (property != null) + { + dataTable.Columns.Add(column.Value, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); + tempColumns[column.Key] = column.Value; + } } - object[] values = new object[properties.Count]; + object[] values = new object[tempColumns.Count]; foreach (T item in data) { - for (int i = 0; i < values.Length; i++) + for (int i = 0; i < tempColumns.Count; i++) { - values[i] = properties[i].GetValue(item); + PropertyDescriptor property = properties.Find(tempColumns.ElementAt(i).Key, true); + values[i] = property.GetValue(item); } dataTable.Rows.Add(values); } @@ -45,99 +75,200 @@ /// 瀵煎嚭Excel /// </summary> /// <param name="dataTable">鏁版嵁婧�</param> - /// <param name="heading">宸ヤ綔绨縒orksheet</param> + /// <param name="worksheet">宸ヤ綔绨縒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) + public static byte[] ExportExcel(ExcelExportSet excelExportDto, bool showSrNo = false) { - byte[] result; + //ExcelPackage.LicenseContext = LicenseContext.Commercial; 5.0浠ヤ笂鐗堟湰 闇�瑕佹巿鏉� + byte[] result = null; using (ExcelPackage package = new ExcelPackage()) { - ExcelWorksheet workSheet = package.Workbook.Worksheets.Add($"{heading}Data"); - int startRowFrom = string.IsNullOrEmpty(heading) ? 1 : 3; //寮�濮嬬殑琛� - //鏄惁鏄剧ず琛岀紪鍙� - if (showSrNo) + foreach (var worksheet in excelExportDto.Worksheets) { - DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int)); - dataColumn.SetOrdinal(0); - int index = 1; - foreach (DataRow item in dataTable.Rows) + var dataTable = excelExportDto.WorksheetDataTable[worksheet]; + ExcelWorksheet workSheet = package.Workbook.Worksheets.Add($"{worksheet}"); + //int startRowFrom = string.IsNullOrEmpty(worksheet) ? 1 : 3; //寮�濮嬬殑琛� + int startRowFrom = 1; //寮�濮嬬殑琛� + //鏄惁鏄剧ず琛岀紪鍙� + if (showSrNo) { - item[0] = index; - index++; + 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) + //Add Content Into the Excel File + workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true); + + for (int columnIndex = 1; columnIndex <= dataTable.Columns.Count; columnIndex++) { - workSheet.Column(columnIndex).AutoFit(); + // autofit width of cells with small content + 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(); + } + // 璁剧疆NG鍗曞厓鏍兼牱寮� + for (int rowIndex = 1; rowIndex <= dataTable.Rows.Count; rowIndex++) + { + if (columnCells[rowIndex, columnIndex].Text == "NG") + { + workSheet.Cells[rowIndex, columnIndex].Style.Font.Bold = true;//瀛椾綋涓虹矖浣� + workSheet.Cells[rowIndex, columnIndex].Style.Fill.PatternType = ExcelFillStyle.Solid; + workSheet.Cells[rowIndex, columnIndex].Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#E33E33"));// 璁剧疆濉厖鏍峰紡 + } + } } - 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) + // format header - bold, yellow on black 璁剧疆琛ㄥご鏍峰紡 + using (ExcelRange r = workSheet.Cells[startRowFrom, 1, startRowFrom, dataTable.Columns.Count]) { - continue; + 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")); } - if (!columnsToTake.Contains(dataTable.Columns[i].ColumnName)) + // format cells - add borders + using (ExcelRange r = workSheet.Cells[startRowFrom + 1, 1, startRowFrom + dataTable.Rows.Count, dataTable.Columns.Count]) { - workSheet.DeleteColumn(i + 1); + 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); } - } - 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; + //灏唖heet鍐欏叆琛ㄤ腑 + //if (!string.IsNullOrEmpty(worksheet)) + //{ + // workSheet.Cells["A1"].Value = worksheet; + // 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 + /// 鏂板缓 鎴� 杩藉姞 鍐欏叆Excel /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="data"></param> - /// <param name="heading"></param> - /// <param name="isShowSlNo"></param> - /// <param name="columnsToTake"></param> + /// <param name="excelExportDto"></param> + /// <param name="fileName">瑕佸啓鍏ョ殑鏂囦欢鍚�(鍏ㄨ矾寰勫悕)</param> + /// <param name="showSrNo"></param> /// <returns></returns> - public static byte[] ExportExcel<T>(List<T> data, string heading = "", bool isShowSlNo = false, params string[] columnsToTake) + public static byte[] CreateOrAppendExcel(ExcelExportSet excelExportDto, string fileName, bool showSrNo = false) { - return ExportExcel(ListToDataTable(data), heading, isShowSlNo, columnsToTake); + byte[] result = null; + bool isExist = File.Exists(fileName); + ExcelExportSet newExcelSet = new ExcelExportSet(); + + if (isExist) + { + var oldWorksheetDataTable = WorksheetToTable(fileName, excelExportDto.Worksheets); + newExcelSet.Worksheets = excelExportDto.Worksheets; + newExcelSet.WorksheetColumns = excelExportDto.WorksheetColumns; + + foreach (var sheet in excelExportDto.Worksheets) + { + var oldTable = oldWorksheetDataTable[sheet]; + newExcelSet.WorksheetDataTable[sheet] = oldTable; + if (excelExportDto.WorksheetDataTable[sheet].Rows.Count > 0) + { + //鍚堝苟涓や釜 datatable + newExcelSet.WorksheetDataTable[sheet].Merge(excelExportDto.WorksheetDataTable[sheet]); + } + } + } + else + { + newExcelSet = excelExportDto; + } + + result = ExportExcel(newExcelSet, showSrNo); + return result; + } + + /// <summary> + ///灏嗘寚瀹氱殑Excel鐨勬枃浠惰浆鎹㈡垚DataTable 锛圗xcel鐨勬寚瀹歴heet锛� + /// </summary> + /// <param name="fullFielPath">鏂囦欢鐨勭粷瀵硅矾寰�</param> + /// <returns></returns> + public static Dictionary<string, DataTable> WorksheetToTable(string fullFielPath, List<string> worksheets) + { + try + { + var resultWorksheetDataTable = new Dictionary<string, DataTable>(); + FileInfo existingFile = new FileInfo(fullFielPath); + ExcelPackage package = new ExcelPackage(existingFile); + foreach (var sheet in worksheets) + { + ExcelWorksheet worksheet = package.Workbook.Worksheets[sheet];//閫夊畾 鎸囧畾椤� + var dataTable = WorksheetToTable(worksheet); + resultWorksheetDataTable[sheet] = dataTable; + } + return resultWorksheetDataTable; + } + catch (Exception) + { + throw; + } + } + + /// <summary> + /// 灏唚orksheet杞垚datatable + /// </summary> + /// <param name="worksheet">寰呭鐞嗙殑worksheet</param> + /// <returns>杩斿洖澶勭悊鍚庣殑datatable</returns> + public static DataTable WorksheetToTable(ExcelWorksheet worksheet) + { + //鑾峰彇worksheet鐨勮鏁� + int rows = worksheet.Dimension.End.Row; + //鑾峰彇worksheet鐨勫垪鏁� + int cols = worksheet.Dimension.End.Column; + + DataTable dt = new DataTable(worksheet.Name); + DataRow dr = null; + for (int i = 1; i <= rows; i++) + { + if (i > 1) + dr = dt.Rows.Add(); + + for (int j = 1; j <= cols; j++) + { + //榛樿灏嗙涓�琛岃缃负datatable鐨勬爣棰� + if (i == 1) + dt.Columns.Add(GetString(worksheet.Cells[i, j].Value)); + //鍓╀笅鐨勫啓鍏atatable + else + dr[j - 1] = GetString(worksheet.Cells[i, j].Value); + } + } + return dt; + } + private static string GetString(object obj) + { + try + { + return (obj ?? "").ToString(); + } + catch (Exception) + { + return ""; + } } } + + } -- Gitblit v1.8.0