Cell Formatting not updating correctly both in code and in open Excel File

Hello,


I’ve been trying to simply get the formatted cell value to show up when a newly excel file is exported. The problem I’m facing is:

1. Code updates cell values and applies a style with a style.Custom or style.Number value (order of application irrelevant, Ive also tried to use Style.Update())
2. Save file (from Code)
3. Open file (in Excel), navigate to worksheet. Cells show data value not formatted value.
4. Requires to go into edit mode on the cell then go off of it again for formatted value to appear.

Side Note: I also noticed that the StringValue is the PutValue() immediately after inserting. Is there a way I can simulate editing a cell then saving it (without actually changing it) inside of the code so I don’t have to edit each cell in the actual Excel file just to get the formatting back?

Thanks.

Hi,

Thanks for your question and using Aspose.Cells for .NET.

I have tested the cell formatting using the following code with the latest version:
Aspose.Cells
for .NET v7.3.0.2

and everything is working fine.

As you can see in the code Style.Custom is working fine.

I have attached the output xlsx file and the screenshot for your reference. Let us know if you still have any question.

C#


Workbook workbook = new Workbook();


Worksheet worksheet = workbook.Worksheets[0];


Cell cell = worksheet.Cells[“A1”];


cell.PutValue(3424);


Style st = cell.GetStyle();

st.Custom = “”$"#,##0_);[Red]\("$"#,##0\)";

cell.SetStyle(st);


workbook.Save(“output.xlsx”);

Screenshot:

Your demo works for me, after playing around with it I realize where my mistake was made. The object value must have been processed as a string, if you change your code to:


object data = “3424”;
cell.PutValue(data);

it does what I was experiencing, I’ll have to send the type data through I didn’t realize that was the cause. Sorry for the confusion and thanks for the help.

Hi,

Thanks for your feedback.

It’s good to know your issue is now resolved.

Also, if you want to use string, then you can use another overload i.e Cell.PutValue(string stringValue, bool isConverted)

See the following code.

C#


string data = “3426”;

cell.PutValue(data, true);


If you have any other question or face any other issue, please let us know, we will help you asap.