I’m using Cells 21.2 to create a spreadsheet with many merged cells. In the example, I have a merged B2-B4 and placed a value of “Row 1\nRow 2\nRow 3\nRow 4” in the B2. A second merge of C8-C12 has a value of “Test”. I have set StandardHeight = 5. When AutoFitRows is called, B2 expands to 3 rows of 14.25, but the 4 rows of text do not fit. Row 8 expands to 14.25 and leaves Row 9-12 at 5.
The expected result is for Rows 2-4 to expand so that “Row 1\nRow 2\nRow 3\nRow 4” is visible, and Rows 8-12 should remain at StandardHeight.
I have tried several combinations of AutoFitterOptions without success. If AutoFit can’t do this, is there a call to see if wrapped text exceeds the height of the cell?
Code and Excel results attached.
Please advise. Thanks.
static void Main(string[] args)
{
string dataDir = "C:\\AsposeTest\\";
// Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
// When you create a new workbook, a default "Sheet1" is added to the workbook.
Worksheet sheet = wb.Worksheets[0];
sheet.Cells.StandardHeight = 5;
string cellData;
cellData = (string)"Row 1\nRow 2\nRow 3\nRow 4";
Cell cell = sheet.Cells[1, 1];
sheet.Cells.Merge(1, 1, 3, 1);
Style style = cell.GetStyle();
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.TopBorder].Color = Color.Black;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.BottomBorder].Color = Color.Black;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.LeftBorder].Color = Color.Red;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.RightBorder].Color = Color.Black;
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
style.VerticalAlignment = Aspose.Cells.TextAlignmentType.Center;
style.IsTextWrapped = true;
// Apply the border styles to the cell
if (cell.IsMerged == true)
{ cell.GetMergedRange().SetStyle(style); }
else { cell.SetStyle(style); }
cell.HtmlString = cellData;
sheet.Cells.Merge(7, 2, 5, 1);
sheet.Cells[7, 2].PutValue("Test");
AutoFitterOptions options = new AutoFitterOptions();
//options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
options.AutoFitMergedCellsType = AutoFitMergedCellsType.None;
//options.AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph;
sheet.AutoFitRows(options);
// Save the Excel file.
wb.Save(dataDir + "AutoFitRow_Test.xlsx", SaveFormat.Xlsx);
}
AsposeTest.zip (14.1 KB)