/// <summary>Exports a List of string Lists (columns) into an excel document and saves it in the path given.</summary> /// <param name="columns"></param> /// <param name="path"></param> public static void ExportToExcel <T>(this List <List <T> > columns, string path) where T : class { var workbook = new XLWorkbook(); IXLWorksheet worksheet = workbook.Worksheets.Add("Sheet1"); worksheet.WriteData(columns); // Add a new sheet for our pivot table // IXLWorksheet pivotWorksheet = workbook.Worksheets.Add("Pivot Table"); //pivotWorksheet.PivotTables.AddNew("Pivot Table", pivotWorksheet.Cell(1, 1), worksheet.RangeUsed()); worksheet.Columns().AdjustToContents(); workbook.SaveAs(path); }
public static void ExportToExcelTree <T>(this List <List <T> > columns, Dictionary <string, string> groupingList, string path) where T : class { var workbook = new XLWorkbook(); IXLWorksheet worksheet = workbook.Worksheets.Add("Sheet1"); worksheet.WriteData(columns); var mainrowFromWorksheet = worksheet.Row(1); mainrowFromWorksheet.Style.Fill.BackgroundColor = XLColor.Amethyst; if (groupingList?.Count > 0) { foreach (var group in groupingList) { worksheet.Rows(group.Key + ":" + group.Value).Group(true); int header; Int32.TryParse(group.Key, out header); if (header > 0) { var rowFromWorksheet = worksheet.Row(header); rowFromWorksheet.Style.Fill.BackgroundColor = XLColor.LightGray; } int mainRow; Int32.TryParse(group.Value, out mainRow); if (mainRow > 0) { mainRow++; worksheet.Rows(mainRow.ToString() + ":" + mainRow.ToString()).Style.Font.Bold = true; } } } //worksheet.Rows(1.ToString() + ":" + 5.ToString()).Group(); //worksheet.Rows(1.ToString() + ":" + 5.ToString()).Style.Font.FontColor = XLColor.Red; // Add a new sheet for our pivot table // IXLWorksheet pivotWorksheet = workbook.Worksheets.Add("Pivot Table"); //pivotWorksheet.PivotTables.AddNew("Pivot Table", pivotWorksheet.Cell(1, 1), worksheet.RangeUsed()); worksheet.Columns().AdjustToContents(); workbook.SaveAs(path); }