📃

Excel Export

TechnologyC#
Editor of the PageLatif Bahadır ALTUN
public byte[] ExcelCreatorRowByRow<T>(string listname, List<T> model)
        {
            byte[] content;

            DataTable dt = new DataTable();
            dt.TableName = "Table1";
            Type type1 = typeof(T);
            var propertyList = new List<PropertyInfo>();
            var boldRowList = new List<int>();
            bool newRowStatus = false;
            string columnName = "";

            foreach (PropertyInfo property in type1.GetProperties())
            {
                var probName = property.Name;
                var probType = property.PropertyType.IsGenericType ? dt.GetType() : property.PropertyType;
                dt.Columns.Add(new DataColumn(property.PropertyType.IsGenericType ? " " : probName));
                propertyList.Add(property);
            }
            foreach (var element in model)
            {
                var row = dt.NewRow();
                foreach (var prob in propertyList)
                {
                    if (prob.GetValue(element) != null)
                    {
                        if (prob.PropertyType.IsGenericType) // ürün vs kırılımları için
                        {
                            newRowStatus = true;
                            var list2 = (List<Dictionary<string, string>>)prob.GetValue(element);
                            dt.Rows.Add(row);
                            
                            var subHeaderRow = dt.NewRow();

                            var headers = list2[0];
                            int headIndex = 0;
                            foreach (var header in headers)
                            {
                                if (dt.Columns.Count == (headIndex + 1))
                                {
                                    for(int i = 0; i < dt.Columns.Count - headIndex; i++)
                                    {
                                        columnName += "-";
                                    }
                                    dt.Columns.Add(new DataColumn(columnName));
                                }

                                subHeaderRow.SetField(headIndex, header.Key);
                                headIndex++;
                            }
                            dt.Rows.Add(subHeaderRow);
                            boldRowList.Add(dt.Rows.Count);

                            foreach (var dict in list2)
                            {
                                var newRow = dt.NewRow();
                                int index = 0;
                                foreach (var detail in dict)
                                {
                                    newRow.SetField(index, detail.Value);
                                    index++;
                                }
                                dt.Rows.Add(newRow);
                            }
                            var emptyRow = dt.NewRow();
                            dt.Rows.Add(emptyRow);// fatura ürünle birlikte geldğinde ayırmak için boşluk.
                        }
                        else // kırılım yoksa
                        {
                            KeyValueObject keyValueObject = new KeyValueObject()
                            {
                                Key = prob.Name,
                                Value = prob.GetValue(element).ToString(),
                            };
                            row.SetField(keyValueObject.Key, keyValueObject.Value);
                        }
                    }
                }
                if(!newRowStatus)
                    dt.Rows.Add(row);
            }

            using (var workbook = new XLWorkbook())
            {
                var sheet = workbook.Worksheets.Add(dt, listname);
                workbook.Worksheets.First().Columns().AdjustToContents();
                workbook.Worksheets.First().Rows().AdjustToContents();

                if (newRowStatus)
                {
                    foreach (var row in boldRowList)
                    {
                        sheet.Rows(row, (row)).Style.Font.FontColor = XLColor.Red;
                        sheet.Rows(row + 1, (row + 1)).Style.Font.Bold = true;
                    }
                }

                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    content = stream.ToArray();
                    return content;
                }
            }
        }