|
| 1 | +--- |
| 2 | +title: Preventing Excel From Extending Cell Formatting Below Formatted Range Using SpreadProcessing |
| 3 | +description: Resolve the issue where cells below a formatted range inherit formatting when using SpreadProcessing to generate Excel files. |
| 4 | +type: how-to |
| 5 | +page_title: Avoiding Automatic Formatting Extension Below a Range in Excel with SpreadProcessing |
| 6 | +meta_title: Avoiding Automatic Formatting Extension Below a Range in Excel with SpreadProcessing |
| 7 | +slug: preventing-cell-formatting-extension-spreadprocessing |
| 8 | +tags: spread, processing, excel, formatting, cell, worksheet |
| 9 | +res_type: kb |
| 10 | +ticketid: 1693269 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | + Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +When exporting an Excel file using [SpreadProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview) and applying formatting to a range of cells, entering data immediately below the formatted range may cause Excel to automatically extend the formatting: |
| 22 | + |
| 23 | +>caption Extend Data Range Formats and Formulas |
| 24 | + |
| 25 | +
|
| 26 | +This occurs due to Excel's built-in *"Extend data range formats and formulas"* feature. The behavior is controlled by Excel itself and cannot be disabled programmatically from within the SpreadProcessing library: |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Clearing the formatting of cells in a range below the formatted data helps mitigate behavior. However, this approach works only for pre-defined ranges and does not override Excel's internal settings permanently. This article demonstrates a sample approach how to rectify such a behavior. |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To prevent Excel's automatic formatting extension, clear the formatting of cells in a specified range below the formatted and populated range. Use the following steps and code: |
| 35 | + |
| 36 | +1. Identify the used cell range in the worksheet. |
| 37 | +2. Define a range below the used cells that you want to clear formatting from. |
| 38 | +3. Apply default formatting (e.g., transparent fill, no borders, default alignment, etc.) to the defined range. |
| 39 | + |
| 40 | +Use the following code example: |
| 41 | + |
| 42 | +```csharp |
| 43 | +// Identify the used range in the worksheet |
| 44 | +CellRange usedCellRange = worksheet.UsedCellRange; |
| 45 | +int rowIndexStart = usedCellRange.ToIndex.RowIndex + 1; |
| 46 | +int columnIndexStart = usedCellRange.FromIndex.ColumnIndex + 1; |
| 47 | +int rowIndexEnd = rowIndexStart + 10; // Adjust the range size as needed |
| 48 | +int columnIndexEnd = usedCellRange.ToIndex.ColumnIndex; |
| 49 | + |
| 50 | +// Define the range to clear formatting |
| 51 | +CellRange clearRange = new CellRange(rowIndexStart, columnIndexStart, rowIndexEnd, columnIndexEnd); |
| 52 | + |
| 53 | +// Set transparent fill and default formatting |
| 54 | +var clearColor = Colors.Transparent; |
| 55 | +SetDefaultFormattingCellRange(worksheet, clearRange, clearColor); |
| 56 | + |
| 57 | +// Helper method to apply default formatting |
| 58 | +static void SetDefaultFormattingCellRange(Worksheet worksheet, CellRange cellRange, Color cellColor) |
| 59 | +{ |
| 60 | + worksheet.Cells[cellRange].SetFill(new PatternFill(PatternType.Solid, cellColor, Colors.Transparent)); |
| 61 | + worksheet.Cells[cellRange].SetIsBold(false); |
| 62 | + worksheet.Cells[cellRange].SetHorizontalAlignment(RadHorizontalAlignment.Left); |
| 63 | + worksheet.Cells[cellRange].SetVerticalAlignment(RadVerticalAlignment.Center); |
| 64 | + worksheet.Cells[cellRange].SetIsWrapped(false); |
| 65 | + worksheet.Cells[cellRange].SetValue(string.Empty); |
| 66 | + |
| 67 | + CellBorder border = new CellBorder(CellBorderStyle.Thin, ThemableColor.FromColor(Colors.LightGray)); |
| 68 | + CellBorder noBorder = new CellBorder(CellBorderStyle.None, ThemableColor.FromColor(Colors.Black)); |
| 69 | + |
| 70 | + worksheet.Cells[cellRange].SetBorders(new CellBorders( |
| 71 | + left: border, |
| 72 | + top: border, |
| 73 | + right: border, |
| 74 | + bottom: border, |
| 75 | + diagonalUp: noBorder, |
| 76 | + diagonalDown: noBorder, |
| 77 | + insideHorizontal: border, |
| 78 | + insideVertical: border) |
| 79 | + ); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +This approach minimizes the chances of Excel automatically extending formatting when new data is entered below the formatted range. |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## See Also |
| 88 | + |
| 89 | +- [SpreadProcessing Overview]({%slug radspreadprocessing-overview%}) |
| 90 | +- [Cell Styles]({%slug radspreadprocessing-features-styling-cell-styles%}) |
0 commit comments