How to ApplyStyle or formatting to a row/column or cells in .NET

Hi,
we are using Aspose.Cells for .NET (.net4.0 dll).
I’m using Apose inside powershell.
As an overview:
We are reading a text-file (plain texdt, separator is TAB.
After reading, I try to apply new a style for the header row.
The new Format is not applied.
(When saving the file as xlsx, all ma other data-m,anipulation are done.)
Here is what I’m doing:

#=====>> loadoptions for loading IPS-Text file
[Aspose.Cells.TxtLoadOptions]$TxtLoadOptions=New-Object Aspose.Cells.TxtLoadOptions;
$TxtLoadOptions.ConvertNumericData=$false;
$TxtLoadOptions.Separator=[char]9;
#<<===== loadoptions for loading IPS-Text file

$wb = new-object Aspose.Cells.Workbook($FilenameIn,$TxtLoadOptions);
[Int]$maxR;
[Int]$HFirstCol;
[Int]$HLastCol;
$maxR=$wb.Worksheets[0].Cells.MaxDataRow;
$HFirstCol=$wb.Worksheets[0].Cells.Rows[0].FirstCell.Column;
$HLastCol=$wb.Worksheets[0].Cells.Rows[0].LastCell.Column;

#=====>> Style & StyleFlags
#$Style = $wb.CreateStyle();
[Aspose.Cells.Style]$Style=$wb.CreateStyle();
$Style.Font.IsBold=$true;
$Style.Font.Name=“Calibri”;
$Style.Font.Size=14;

$styleFlag = New-Object Aspose.Cells.StyleFlag
$styleFlag.HorizontalAlignment = $true;
$styleFlag.VerticalAlignment = $true;
$styleFlag.ShrinkToFit = $true;
$styleFlag.Borders = $true;
$styleFlag.FontColor = $true;
$styleFlag.BottomBorder=$true;
#<<===== Style & StyleFlags

#=====>> creating 2. sheet as table
$wb.Worksheets.AddCopy(0);
#$wb.Worksheets.Add(“Pavis Table”);
[Aspose.Cells.Tables.ListObjectCollection] $listObjects = $wb.Worksheets[1].ListObjects;
$listObjects.Add(0, 0, $maxR, $HLastCol, $true);
[Aspose.Cells.Worksheet]$ws1=$wb.Worksheets[1];
$ws1.AutoFitRows(0, $maxR);
$ws1.AutoFitColumns(0,$HLastCol);
#<<===== creating 2. sheet as table

[Aspose.Cells.Worksheet]$ws0=$wb.Worksheets[0];
$ws0.AutoFitRows(0, $maxR);
$ws0.AutoFitColumns(0,$HLastCol);
[Aspose.Cells.Row]$row=$wb.Worksheets[0].Cells.Rows[0];
$row.ApplyStyle($Style,$styleFlag);
$wb.Save($FilenameOut)

@PAVIS,

Your code looks not compatible when comparing Style with its relevant StyleFlag attributes. You define Style and specify Style attributes. But you do not set the corresponding Boolean attributes for StyleFlag object and vice versa. For example, you only use relevant StyleFlag.FontColor as true. However, what you set for the Style is font size, name and size. This is wrong. Similarly you set the Boolean attributes for alignments, borders, etc. but there are no corresponding attributes (defined) for Style object. Please fix all these issues and then try your scenario/case again, it should work fine.

Moreover, please see the document with examples on formatting rows and columns for your complete reference.

If you still find any issue, kindly do provide sample code (runnable) with template Excel file(s) to reproduce the issue, we will check it soon.

PS. please zip the Excel file(s) prior attaching.

1 Like