|
| 1 | +using Syncfusion.Pdf; |
| 2 | +using Syncfusion.XlsIO; |
| 3 | +using Syncfusion.XlsIORenderer; |
| 4 | +using System; |
| 5 | +using static System.Net.Mime.MediaTypeNames; |
| 6 | + |
| 7 | +class Program |
| 8 | +{ |
| 9 | + static void Main() |
| 10 | + { |
| 11 | + using (ExcelEngine engine = new ExcelEngine()) |
| 12 | + { |
| 13 | + IApplication app = engine.Excel; |
| 14 | + app.DefaultVersion = ExcelVersion.Xlsx; |
| 15 | + |
| 16 | + IWorkbook workbook = app.Workbooks.Create(1); |
| 17 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 18 | + worksheet.Name = "Monthly Sales"; |
| 19 | + |
| 20 | + // Title |
| 21 | + worksheet.Range["A1"].Text = "Monthly Sales Report"; |
| 22 | + worksheet.Range["A1"].CellStyle.Font.Bold = true; |
| 23 | + worksheet.Range["A1"].CellStyle.Font.Size = 16; |
| 24 | + |
| 25 | + // Headers |
| 26 | + worksheet.Range["A3"].Text = "Order ID"; |
| 27 | + worksheet.Range["B3"].Text = "Date"; |
| 28 | + worksheet.Range["C3"].Text = "Region"; |
| 29 | + worksheet.Range["D3"].Text = "Salesperson"; |
| 30 | + worksheet.Range["E3"].Text = "Units"; |
| 31 | + worksheet.Range["F3"].Text = "Amount"; |
| 32 | + worksheet.Range["A3:F3"].CellStyle.Font.Bold = true; |
| 33 | + worksheet.Range["A3:F3"].CellStyle.Color = Syncfusion.Drawing.Color.FromArgb(240, 240, 240); |
| 34 | + |
| 35 | + // Generate Sales Data |
| 36 | + int row = 4; |
| 37 | + string[] regions = new[] { "North", "South", "West", "East" }; |
| 38 | + Random rnd = new Random(17); |
| 39 | + DateTime month = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); |
| 40 | + |
| 41 | + foreach (string region in regions) |
| 42 | + { |
| 43 | + worksheet.Range["A" + row].Text = region + " Region"; |
| 44 | + worksheet.Range["A" + row].CellStyle.Font.Bold = true; |
| 45 | + worksheet.Range["A" + row].CellStyle.Font.Size = 12; |
| 46 | + row++; |
| 47 | + |
| 48 | + for (int count = 0; count < 20; count++) |
| 49 | + { |
| 50 | + worksheet.Range["A" + row].Text = $"ORD-{region.Substring(0, 1)}-{1000 + count}"; |
| 51 | + worksheet.Range["B" + row].DateTime = month.AddDays(rnd.Next(0, 28)); |
| 52 | + worksheet.Range["B" + row].NumberFormat = "dd-MMM"; |
| 53 | + worksheet.Range["C" + row].Text = region; |
| 54 | + worksheet.Range["D" + row].Text = "Rep " + rnd.Next(1, 6); |
| 55 | + worksheet.Range["E" + row].Number = rnd.Next(1, 25); |
| 56 | + worksheet.Range["F" + row].Number = Math.Round(2500 + rnd.NextDouble() * 20000, 2); |
| 57 | + worksheet.Range["F" + row].NumberFormat = "#,##0.00"; |
| 58 | + row++; |
| 59 | + } |
| 60 | + row++; |
| 61 | + } |
| 62 | + |
| 63 | + worksheet.UsedRange.AutofitColumns(); |
| 64 | + |
| 65 | + // Page Setup Options |
| 66 | + IPageSetup pageSetup = worksheet.PageSetup; |
| 67 | + |
| 68 | + // Set Paper Size to A4 |
| 69 | + pageSetup.PaperSize = ExcelPaperSize.PaperA4; |
| 70 | + |
| 71 | + // Set Orientation to Landscape |
| 72 | + pageSetup.Orientation = ExcelPageOrientation.Landscape; |
| 73 | + |
| 74 | + // Set Margins in inches |
| 75 | + |
| 76 | + // Top, Bottom - 0.5 inch |
| 77 | + pageSetup.TopMargin = 0.75; |
| 78 | + pageSetup.BottomMargin = 0.75; |
| 79 | + |
| 80 | + // Left, Right - 0.25 inch |
| 81 | + pageSetup.LeftMargin = 0.25; |
| 82 | + pageSetup.RightMargin = 0.25; |
| 83 | + |
| 84 | + // Header, Footer - 0.3 inch |
| 85 | + pageSetup.HeaderMargin = 0.3; |
| 86 | + pageSetup.FooterMargin = 0.3; |
| 87 | + |
| 88 | + // Set fit to page as false. |
| 89 | + pageSetup.IsFitToPage = false; |
| 90 | + |
| 91 | + // Set repeat rows as 3rd row |
| 92 | + pageSetup.PrintTitleRows = "$3:$3"; |
| 93 | + |
| 94 | + // Define the print area from A3 to last used row in column F |
| 95 | + int lastRow = worksheet.UsedRange.LastRow; |
| 96 | + pageSetup.PrintArea = $"A3:F{lastRow}"; |
| 97 | + |
| 98 | + // Apply left header as "Monthly Sales" with Calibri font of size 14 and bold |
| 99 | + pageSetup.LeftHeader = "&\"Calibri,Bold\"&14 Monthly Sales"; |
| 100 | + |
| 101 | + // Apply center header as "Month Year" with bold |
| 102 | + pageSetup.CenterHeader = "&B&10" + month.ToString("MMMM yyyy"); |
| 103 | + |
| 104 | + // Apply right header with page number and total pages |
| 105 | + pageSetup.RightHeader = "Page &P of &N"; |
| 106 | + |
| 107 | + // Apply left footer with sheet name |
| 108 | + pageSetup.LeftFooter = "Sheet: &A"; |
| 109 | + |
| 110 | + // Apply center footer with current date and time |
| 111 | + pageSetup.CenterFooter = "Generated: &D &T"; |
| 112 | + |
| 113 | + // Apply right footer with file name |
| 114 | + pageSetup.RightFooter = "&F"; |
| 115 | + |
| 116 | + // Set the scaling to 120% |
| 117 | + pageSetup.Zoom = 120; |
| 118 | + |
| 119 | + // Fit to 1 page wide and unlimited tall |
| 120 | + pageSetup.FitToPagesTall = 0; |
| 121 | + |
| 122 | + // Fit to 1 page wide |
| 123 | + pageSetup.FitToPagesWide = 1; |
| 124 | + |
| 125 | + // Add a page break before each region. Assuming each region block (title + data) takes 22 rows including spacing. |
| 126 | + int firstRegionRow = 4; |
| 127 | + int blockHeight = 22; |
| 128 | + for (int count = 1; count < regions.Length; count++) |
| 129 | + { |
| 130 | + int titleRow = firstRegionRow + count * blockHeight; |
| 131 | + |
| 132 | + // Add a horizontal page break before the title row of each region |
| 133 | + worksheet.HPageBreaks.Add(worksheet.Range["A" + titleRow]); |
| 134 | + } |
| 135 | + |
| 136 | + // Set gridlines to be hidden in the printed page |
| 137 | + pageSetup.PrintGridlines = false; |
| 138 | + |
| 139 | + // Set center the sheet horizontally on the page |
| 140 | + pageSetup.CenterHorizontally = true; |
| 141 | + |
| 142 | + // Set not to center the sheet vertically on the page |
| 143 | + pageSetup.CenterVertically = false; |
| 144 | + |
| 145 | + |
| 146 | + //Do not print headings |
| 147 | + pageSetup.PrintHeadings = false; |
| 148 | + |
| 149 | + // Do not print comments |
| 150 | + pageSetup.PrintComments = ExcelPrintLocation.PrintNoComments; |
| 151 | + |
| 152 | + // Do print in black and white only |
| 153 | + pageSetup.BlackAndWhite = true; |
| 154 | + |
| 155 | + // Draft quality set to false |
| 156 | + pageSetup.Draft = false; |
| 157 | + |
| 158 | + // Set first page number as 2 |
| 159 | + pageSetup.FirstPageNumber = 2; |
| 160 | + |
| 161 | + // Save workbook as Excel document |
| 162 | + workbook.SaveAs("MonthlySales.xlsx"); |
| 163 | + |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments