public void TEst() { var dics = new List<Dictionary<string, int>>(); dics.Add(new Dictionary<string, int> { { "key0", 12 }, { "key1", 4 }, { "key2", 5 }, { "key3", 8 } }); dics.Add(new Dictionary<string, int> { { "key0", 3 }, { "key1", 6 }, { "key2", 19 } }); dics.Add(new Dictionary<string, int> { { "key0", 11 }, { "key1", 16 }, { "key2", 2 }, { "key3", 28 }, { "key4", 38 }, { "key5", 55 } }); GetMaxWidthByDic(dics, 1); }
/// <summary> /// get maxwidth /// </summary> /// <param name="dics"></param> /// <param name="inputColumnindex"></param> /// <returns></returns> private int GetMaxWidthByDic(List<Dictionary<string, int>> dics, int inputColumnindex) { var minLengthDics = new List<int>(); int diclength = dics.Count;//dic 的数量 for (int i = 0; i < dics.Count; i++) { minLengthDics.Add(dics[i].Count); } int minLengthDic = minLengthDics.Min();//最小Dic的key长度 if (inputColumnindex > minLengthDic) { inputColumnindex = minLengthDic;//最大输入的值重新处理判断 } int index = 0; for (int i2 = 0; i2 < dics.Count; i2++) { if (dics[i2].Count == minLengthDic) { index = i2; break; } } var newdic = new Dictionary<string, int>(); for (int c = 0; c < dics[index].Count; c++)//最小的字典的那个长度 { var MaxInt = new List<int>(); for (int L = 0; L < diclength; L++)//dic数量 { MaxInt.Add(dics[L]["key" + c]); } int maxV = MaxInt.Max(); newdic.Add($"key{c}", maxV); } return newdic["key" + inputColumnindex]; // foreach (var q in newdic.Keys) // { // Console.WriteLine($"key:{q}--value:{newdic[q]}"); // } }
public async Task<byte[]> ExportReportQuotationByNpoiAsync(Guid quotationId, QuotationOutput _quotationResult) { //Quotation主要信息 // var quotationResult =_quotationResult;// await _quotationconfigAppService.GetByIdAsync(quotationId); var quotationMainExportDtos = new List<QuotationMainExportDto>(); var quotationAllInfo = _quotationResult;// quotationResult.Data; var quotationMainExportDto = ObjectMapper.Map<QuotationOutput, QuotationMainExportDto>(quotationAllInfo); quotationMainExportDtos.Add(quotationMainExportDto); var itemsProduct = quotationAllInfo.QuotationProductModels.ToList(); var quotationSelctProductExportDto = ObjectMapper.Map<List<QuotationProductModelsOutput>, List<QuotationSelctProductExportDto>>(itemsProduct); //是否选中的yes赋值 var choseProcessDic = new Dictionary<string, List<string>>(); foreach (var item in itemsProduct) { var itemSelectPros = item.ProcessSectionOutputDtos.ToList().Where(c => c.IsChecked == true).Select(c => c.ProcessSectionName).ToList(); choseProcessDic.Add(item.ModelPN ?? "keyno", itemSelectPros); } string processNameStr = ConnectionJsonDto.SmtWaveBackProcess ?? "SMT,Wave,Back"; var processSectionName = processNameStr.Split(","); foreach (var qproduct in quotationSelctProductExportDto) { qproduct.SmtProcess = choseProcessDic[qproduct.modelPN].Any(c => c.Contains(processSectionName[0])) ? "YES" : "NO"; qproduct.WaveProcess = choseProcessDic[qproduct.modelPN].Any(c => c.Contains(processSectionName[1])) ? "YES" : "NO"; qproduct.BackendProcess = choseProcessDic[qproduct.modelPN].Any(c => c.Contains(processSectionName[2])) ? "YES" : "NO"; qproduct.OtherProcess = "NO"; } //quotationSelctProductExportDto var productIds = quotationAllInfo.QuotationProductModels.ToList().Select(c => c.ProductId).ToList(); if (productIds == null || productIds.Count <= 0) throw new Exception("Please Chose Product First!"); var listProduct = new List<QueryProductModelAllInfo>(); foreach (var _productId in productIds) { //产品信息 var productResult = await _productAppService.QueryOneProductModel(_productId); if (productResult.Data != null) listProduct.Add(productResult.Data); } IWorkbook wookbook = new XSSFWorkbook(); ISheet sheet = wookbook.CreateSheet("Quotation"); int rowIndex = 0;//第几行 IRow headRow = sheet.CreateRow(rowIndex); headRow.Height = 350; ICellStyle cellStyleBgColor = wookbook.CreateCellStyle(); // cellStyleBgColor.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.White.Index; //IndexedColors.Grey25Percent.Index; // cellStyleBgColor.FillPattern = FillPattern.SolidForeground; cellStyleBgColor.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; //Quotation var dicQuotation_tupe = DoRowCellWork_productInfos<QuotationMainExportDto>(rowIndex, sheet, cellStyleBgColor, quotationMainExportDtos); //product int product_rowIndex = quotationMainExportDtos.Count + 3; var dicQuotationProduct_tupe = DoRowCellWork_productInfos<QuotationSelctProductExportDto>(product_rowIndex, sheet, cellStyleBgColor, quotationSelctProductExportDto); var listDic = new List<Dictionary<string, int>>(); listDic.Add(dicQuotation_tupe.Item2); listDic.Add(dicQuotationProduct_tupe.Item2);
//参数一:同sheet中最小的哪一行 head ,参数二:当前的sheet即可,参数3:数据,如4个table的数据在一个sheet中 AutoSetExcelWidth(dicQuotationProduct_tupe.Item1, sheet, listDic);// TODO }
private Tuple<IRow, Dictionary<string, int>> DoRowCellWork_productInfos<T>(int rowCount, ISheet sheet, ICellStyle cellStyleBgColor, List<T> datas) { var dic = new Dictionary<string, int>(); int product_rowIndex = rowCount; PropertyInfo[] pts = typeof(T).GetProperties(); // if (myorder) //orderbypropertity // { // //order propertity // pts = pts.Where(c => Attribute.IsDefined(c, typeof(DataMemberAttribute))) // .OrderBy(c => ((DataMemberAttribute)Attribute.GetCustomAttribute(c, typeof(DataMemberAttribute))).Order).ToArray(); // } IRow headRow_product = sheet.CreateRow(product_rowIndex); headRow_product.Height = 350; for (int i = 0; i < pts.Length; i++) { var qname = pts[i].GetCustomAttribute<QuotaNameAttribute>(); if (qname != null) { ICell cell = headRow_product.CreateCell(i); // cell.SetCellValue(pts[i].Name); cell.SetCellValue(qname.DisplayName); cell.CellStyle = cellStyleBgColor; dic.Add($"key{i}", (Encoding.Default.GetBytes(cell.StringCellValue).Length * 256) + 500);//200 } } for (int s = 0; s < datas.Count; s++) { product_rowIndex += 1; var row = sheet.CreateRow(product_rowIndex); for (int p = 0; p < pts.Length; p++) { string strV = pts[p].GetValue(datas[s] ?? default)?.ToString() ?? ""; ICell cell = row.CreateCell(p); cell.SetCellValue(strV); int valuelength = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 500; valuelength = valuelength > 15000 ? 15000 : valuelength; if (dic[$"key{p}"] < valuelength) dic[$"key{p}"] = valuelength; } } // TODO jason 20240221 for (int i = 0; i < headRow_product.Cells.Count; i++) { sheet.SetColumnWidth(i, dic[$"key{i}"]); } return Tuple.Create(headRow_product, dic);// dic; }
标签:head,sheet,int,Npoi,Add,dics,var,new From: https://www.cnblogs.com/Fengge518/p/18034330