Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Aug 08, 2019 8:39 am

Dear Spire.Xls Team,

Spire.Xls - Version: 9.7.0

I am using conditional formatting to color a cell if the value is out of range.
But when opening the generated xlsx file, excel tells me that it is corrupt.

I was able to find the trigger for the problem:
If I call workbook.CalculateAllValue(); before saving the Excel to file, it results in a corrupted Excel file.
The problem can also be triggered when calling workbook.SaveToFile twice. The first file will be ok. The second is always corrupt.

I created a minimalistic sample to reproduce the problem:
Code: Select all
var workbook = new Workbook {Version = ExcelVersion.Version2016};
workbook.Worksheets.Clear();

var sheet = workbook.CreateEmptySheet("Test");
sheet[1, 1].NumberValue = 5;
sheet[1, 2].NumberValue = 10;
sheet[1, 3].Formula = "=A1+B1";
sheet[1, 4].Formula = "=A1+B1+C1";

var condFormat = sheet.ConditionalFormats.Add();
condFormat.AddRange(sheet[1, 3, 1, 4]);
var cond = condFormat.AddCellValueCondition(ComparisonOperatorType.NotBetween, 1, 8);
cond.BackColor = Color.Red;

//workbook.CalculateAllValue();

workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2016);
Process.Start("Sample.xlsx");

workbook.SaveToFile("SampleCorrupt.xlsx", ExcelVersion.Version2016);
Process.Start("SampleCorrupt.xlsx");

I have to call CalculateAllValue because I want to AutoInitialize the width of the columns.
Thus I have to calculate the values because otherwise it can not set the width of the columns correctly.

Is there a way to bypass this problem?

Kind Regards
Karl

karl.pircher@recla.it
 
Posts: 15
Joined: Mon Mar 14, 2016 2:16 pm

Thu Aug 08, 2019 10:33 am

Hi,

Thanks for your inquiry. This is Amber from E-iceblue support team.
I have reproduced your issue and logged it into our bug tracking system. Once there is any progress, we will inform you.
Sorry for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Aug 23, 2019 11:47 am

Hi,

Glad to inform you that the previous issue has been fixed by Spire.XLS Pack(Hotfix) Version:9.8.11. Welcome to download and test it. Below are the links.
Website: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget: https://www.nuget.org/packages/Spire.XLS/9.8.11

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Aug 23, 2019 1:33 pm

Hello Amber,

thank you for the reply.

The fix is working with the example I provided you.
But in production it did not fix the problem.

As long as the range values are integers it works as intended.
But if you try to use a range that is not an integer number, then it causes the same problem as before.

Just change this line
Code: Select all
var cond = condFormat.AddCellValueCondition(ComparisonOperatorType.NotBetween, 1, 8);

to
Code: Select all
var cond = condFormat.AddCellValueCondition(ComparisonOperatorType.NotBetween, 1.5, 8.5);

and it will fail once again.

Kind Regards
Karl

karl.pircher@recla.it
 
Posts: 15
Joined: Mon Mar 14, 2016 2:16 pm

Mon Aug 26, 2019 3:24 am

Hi Karl,

Thanks for your feedback and sorry to reply late for weekend.
I tested the code you mentioned using Spire.XLS Pack(Hotfix) Version:9.8.11 but didn’t reproduce your issue, it work well even the range values are not integers on my side. Please try the latest Spire.Xls again. If the issue still exists, please offer us the following information for further investigation.
1. A runnable project which could reproduce your issue directly.
2. The OS and Region information, e.g Win7 64bit, China/Chinese.
3. The Excel version you used to open the file.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Nov 08, 2019 11:35 am

Hello,

sorry for the late reply.

Today I faced the same issue once again with version 9.11.2.
If I use double numbers for the values of the range condition it does not work. If I use integers it works.

You can reproduce it with this sample.
If you change the numbers after ComparisonOperatorType.NotBetween to 2 and 9, then it works as intended
Code: Select all
var workbook = new Workbook {Version = ExcelVersion.Version2016};
workbook.Worksheets.Clear();

var sheet = workbook.CreateEmptySheet("Test");
sheet[1, 1].NumberValue = 5;
sheet[1, 2].NumberValue = 10;
sheet[1, 3].Formula = "=A1+B1";
sheet[1, 4].Formula = "=A1+B1+C1";

var condFormat = sheet.ConditionalFormats.Add();
condFormat.AddRange(sheet[1, 3, 1, 4]);
var cond = condFormat.AddCellValueCondition(ComparisonOperatorType.NotBetween, 1.1, 8.5);
cond.BackColor = Color.Red;
           
workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2016);
Process.Start("Sample.xlsx");

workbook.SaveToFile("SampleCorrupt.xlsx", ExcelVersion.Version2016);
Process.Start("SampleCorrupt.xlsx");


Kind regards
Karl

karl.pircher@recla.it
 
Posts: 15
Joined: Mon Mar 14, 2016 2:16 pm

Mon Nov 11, 2019 2:24 am

Hi,

Thanks for your reply. Sorry to reply late for weekend.
I tested your code with Spire.XLS Pack(Hotfix) Version:9.11.2 but didn't reproduce your issue. It worked well no matter the range values were integers or decimals. And the second result file wouldn't be corrupted. I have uploaded my result Excel files here, please check them.

To help us investigate your issue accurately, please offer us the following information.
1. A runnable project which could reproduce your issue directly.
2. The OS and Region information, e.g Win7 64bit, China/Chinese.
3. The Excel version you used to open the file.

You could upload them here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
Attachments
result.zip
(9.28 KiB) Downloaded 266 times
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Nov 11, 2019 10:19 am

Hello,

thanks for the reply.

This is very strange. I can open your excel files without any problem.

My OS:
- Windows 10 x64 (English/US) with Country / Region = Germany
- Office 365 ProPlus (German) Version: 16.0.12130.20272

I attached the sample project with which you should be able to reproduce it.
Moreover I attached the two "broken" Excel files that I got when I run the sample program.

Maybe it has something todo with the German number format?
We use the COMMA as decimal separator instead of the . (DOT)

Kind Regards
Karl
Attachments
GeneratedFiles.zip
(9.18 KiB) Downloaded 230 times
ConsoleApp2.zip
(4.27 KiB) Downloaded 254 times

karl.pircher@recla.it
 
Posts: 15
Joined: Mon Mar 14, 2016 2:16 pm

Tue Nov 12, 2019 5:46 am

Hi,

Thanks for your information.
I reproduced your issue when I set the format as German. Please try to set the thread culture in your project and the result Excel file will be correct. Below is the code for you.

Code: Select all
            //Set the thread culture
            CultureInfo cc = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            //Your complete code

            Thread.CurrentThread.CurrentCulture = cc;


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Dec 12, 2019 9:58 am

Hi,

Greetings from E-iceblue.
Have you tried the code I offered you last time? Does it solve your issue? Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Feb 24, 2020 1:44 pm

Hi,
sorry for the late reply.
Yes. Setting the culture fixed the issue.

Thank you

karl.pircher@recla.it
 
Posts: 15
Joined: Mon Mar 14, 2016 2:16 pm

Tue Feb 25, 2020 2:11 am

Hi,

Thanks for your feedback.
Any question, please feel free to contact us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS