Skip to content

Commit 88112b3

Browse files
authored
Merge pull request #597 from telerik/new-kb-preventing-cell-formatting-extension-spreadprocessing-91e808a069864fc28b6eab9c93d98b0c
Added new kb article preventing-cell-formatting-extension-spreadprocessing
2 parents 88b4093 + c4616d7 commit 88112b3

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
118 KB
Loading
25 KB
Loading
38.1 KB
Loading
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
![Microsoft Excel's built-in "Extend data range formats and formulas" feature ><](images/excel-extend-data-range-formatting.gif)
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+
![Microsoft Excel's built-in "Extend data range formats and formulas" setting ><](images/excel-extend-data-range-formatting.png)
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+
![Eliminate the Microsoft Excel's built-in "Extend data range formats and formulas" setting ><](images/rectify-excel-extend-data-range-formatting.png)
86+
87+
## See Also
88+
89+
- [SpreadProcessing Overview]({%slug radspreadprocessing-overview%})
90+
- [Cell Styles]({%slug radspreadprocessing-features-styling-cell-styles%})

libraries/radspreadprocessing/features/styling/cell-styles.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,4 @@ You can also remove a style from the __Styles__ collection. It is as easy as rem
216216
* [Document Themes]({%slug radspreadprocessing-features-styling-document-themes%})
217217
* [Whats is a Workbook?]({%slug radspreadprocessing-working-with-workbooks-what-is-workbook%})
218218
* [Retrieving Themable Cell Color in RadSpreadProcessing]({%slug retrieve-cell-color-radspreadprocessing%})
219+
* [Preventing Excel From Extending Cell Formatting Below Formatted Range Using SpreadProcessing]({%slug preventing-cell-formatting-extension-spreadprocessing%})

0 commit comments

Comments
 (0)