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.

Sat Dec 05, 2020 7:37 am

Hi,

Please can you help me to format the Databar like my sample,

Dim conditional As XlsConditionalFormats = sheet.ConditionalFormats.Add()
conditional.AddRange(sheet.Range("B1:B4"))
Dim format As IConditionalFormat = conditional.AddCondition()
format.FormatType = ConditionalFormatType.DataBar
..?

Thank you
GH
Attachments
Screenshot 2020-12-05 083607.png
Screenshot 2020-12-05 083607.png (2.27 KiB) Viewed 780 times

GHWels
 
Posts: 90
Joined: Sun Nov 23, 2014 7:22 pm

Mon Dec 07, 2020 3:40 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
Please refer to the following code. If there are any questions, please feel free to contact us.
Code: Select all
        Dim workbook As Workbook = New Workbook
        Dim sheet As Worksheet = workbook.Worksheets(0)
        Dim conditional As XlsConditionalFormats = sheet.ConditionalFormats.Add()
        sheet.Range("B1").NumberValue = -0.2
        sheet.Range("B2").NumberValue = -0.3
        sheet.Range("B3").NumberValue = 0.12
        sheet.Range("B4").NumberValue = 1
        sheet.Range("B1:B4").NumberFormat = "0%"
        conditional.AddRange(sheet.Range("B1:B4"))
        Dim format As IConditionalFormat = conditional.AddCondition()
        format.FormatType = ConditionalFormatType.DataBar
        format.DataBar.AxisPosition = DataBarAxisPosition.DataBarAxisMidpoint
        format.DataBar.BarFillType = DataBarFillType.DataBarFillGradient
        Dim cellRanges As CellRange = sheet.Range("B1:B4")
        For Each cellrange As CellRange In cellRanges
            If (cellrange.NumberValue > 0) Then
                format.DataBar.BarColor = Color.CadetBlue
            Else
                format.DataBar.BarColor = Color.IndianRed
            End If
        Next
        workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013)
        System.Diagnostics.Process.Start("result.xlsx")


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Dec 07, 2020 7:36 am

Hey Brian,

Thank you very much for your fast and perfect answer.

GH

GHWels
 
Posts: 90
Joined: Sun Nov 23, 2014 7:22 pm

Mon Dec 07, 2020 8:11 am

You are welcome. If you have any other question, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.XLS