简体   繁体   中英

Conditional format on Excel file

Is that possible to do some conditional format on Excel file with JXL or Apache POI or something else? API java Like Macro VBA for example?

' Mise en forme couleur pour différence
For i = 3 To fin Step 1
    Range("C" & i).Select
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=B" & i
    Selection.FormatConditions(1).Interior.ColorIndex = 6 

so, in your target language, these are the Excel Objects you need:

  1. Create an instance of Excel.Application in a variable like oXlApp
  2. use the Excel.Application object to open your target workbook:
    • Call the Open method of the oXlApp Workbooks collection passing in the filename as an argument to the Open method
  3. use the Selection property of the oxlApp object to return an Excel Range object
  4. modify the Selection or Range as below:

    1. Execute a Delete method on the FormatConditions property of the Range
    2. Execute an Add command on the FormatConditions property of the Range

      • pass in these arguments to the Add method:
        • Type:=xlExpression
        • Formula1:="=$B2<>$C2"
      • set this property of the first Format Condition to set the cells color :
        • FormatConditions(1).Interior.ColorIndex = 36

    '

  5. Call the Save method on your workbook
  6. Clean up calling the Close method on the Workbook then Quit on the oXlApp and finally, RELEASE MEMORY!

in Excel VBA we would do something like this:

activesheet.usedrange.select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$B2<>$C2"
Selection.FormatConditions(1).Interior.ColorIndex = 36

this will first select the used cells, then add a conditional formatting for cols b and c

Also, see this one StackOverFlow: Manipulate Excel from Jacob/Java

and look at the JXL Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM