public static void exportToExcel(DataGridView dgv, string name, bool footer) { SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY"); // Load Excel (XLSX) from a file. ExcelFile workbook = new ExcelFile(); ExcelWorksheet worksheet = workbook.Worksheets.Add($"{name}"); worksheet.Cells["A1"].Value = ""; workbook.Save($"{name}.xlsx"); var workbook1 = ExcelFile.Load($"{name}.xlsx"); // Import DataGridView back to active worksheet. DataGridViewConverter.ImportFromDataGridView( workbook1.Worksheets.ActiveWorksheet, dgv, new ImportFromDataGridViewOptions() { ColumnHeaders = true }); // Save Excel (XLSX) to a file. workbook1.Save($"{name}.xls"); File.Delete($"{name}.xlsx"); if (footer == true) { SheetHeaderFooter hfooter = workbook1.Worksheets.ActiveWorksheet.HeadersFooters; hfooter.DefaultPage.Footer.CenterSection.Content = $"Number of Non-Active Members: {numberNonActive},\nNumber of Active Members: {numberActive},\nNumber of Members Owing: {numberOwing},\nTotal Amount Owed: {amountOwedTotal}"; workbook1.Save($"{name}.xls"); } }
static void Main(string[] args) { // If using Professional version, put your serial key below. SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY"); ExcelFile ef = new ExcelFile(); ExcelWorksheet ws = ef.Worksheets.Add("Header and Footer"); string pathToResources = "Resources"; SheetHeaderFooter headerFooter = ws.HeadersFooters; // Show title only on the first page headerFooter.FirstPage.Header.CenterSection.Content = "Title on the first page"; // Show logo headerFooter.FirstPage.Header.LeftSection.AppendPicture(Path.Combine(pathToResources, "Dices.png"), 40, 40); headerFooter.DefaultPage.Header.LeftSection = headerFooter.FirstPage.Header.LeftSection; // "Page number" of "Number of pages" headerFooter.FirstPage.Footer.RightSection.Append("Page ").Append(HeaderFooterFieldType.PageNumber).Append(" of ").Append(HeaderFooterFieldType.NumberOfPages); headerFooter.DefaultPage.Footer = headerFooter.FirstPage.Footer; // Fill Sheet1 with some data for (int i = 0; i < 140; i++) { for (int j = 0; j < 9; j++) { ws.Cells[i, j].Value = i + j; } } ef.Save("Header and Footer.xlsx"); }