Hello,
In the attached workbook Range F5:H5 and range I5:K5 have been formatted using the Center Across Selection horizontal style feature in Excel. Which should line up to the TextAlignmentType.CenterAcross enum in Aspose. These cells appear like Merged Cells without actually merging the cells. My question is how can I inspect a workbook to find these styles that apply to a range of cells. If I look at worksheet.Cells[“F5”].GetDisplayStyle().HorizontalAlignment the property equals TextAlignmentType.CenterAcross, however I can’t figure out how to tell that the next 2 cells are part of this CenterAcross selection.
I have checked your template Excel file and evaluated your requirements. Your mentioned ranges are not merged, so there is no attribute to distinguish these are your so called two ranges. I think you may search/find the cells with Center Across Style. You may use Find/Search options provided by Aspose.Cells for your task. See the following sample code that will give you cells list/details with Center Across Selection Style for your reference.
e.g., Sample code:
Workbook workbook = new Workbook("e:\\test2\\CenterAcrossSelection.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
//Defind style object based on F5 cell's style
Style style = worksheet.Cells["F5"].GetDisplayStyle(true);
//Specify the style for searching
FindOptions options = new FindOptions();
options.Style = style;
Cell nextCell = null;
do
{
//Find the cell that has a style of cell F5
nextCell = worksheet.Cells.Find(null, nextCell, options);
if (nextCell == null)
break;
//Write cell name with its horizontal alignment settings.
Console.WriteLine("Cell Name: " + nextCell.Name + ", Horizontal Alignment Style: " + nextCell.GetDisplayStyle().HorizontalAlignment.ToString());
} while (true);
I appreciate the code sample but that doesn’t quite address the need.
Using the code below you can create a style for the range B2:E2 to set the CenterAcross text. But I don’t see any way possible to interrogate a workbook to then find this style that spans a range. The example you provided helps to find the cells with a style set to CenterAcross, but no way to tell that the style applies to a range of cells.
Is there any way to iterate through Style objects and ask what range they apply to?
[Test]
public async Task foo47()
{
var workbook = new Workbook();
workbook.Worksheets.First().Cells["B2"].PutValue("Hello World");
var range = workbook.Worksheets.First().Cells.CreateRange("B2:E2");
var style = workbook.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.CenterAcross;
range.SetStyle(style, true);
workbook.Save(@"C:\out.xlsx");
}
This technique does the job! Checking if(cell.Type == CellValueType.IsNull) was the important bit that I needed to be able to tell where the CenterAcross ends. Thanks again for the help.