
Java에서 Excel에 신호등 아이콘 추가
2022-10-19 last update
10 minutes reading java api conditionalformatting excelExcel의 신호등 아이콘은 주로 프로젝트의 진행 상황과 상태를 관리하는 데 사용되는 조건부 서식입니다. 이 기사에서는 무료 라이브러리인 Java용 Free Spire.XLS를 사용하여 Excel에서 신호등 아이콘을 프로그래밍 방식으로 추가하는 방법을 배웁니다.
1# free library을 다운로드하고 압축을 푼 다음 Spire.Xls.jar 파일을 프로젝트에 종속성으로 추가합니다.
2# pom.xml에 다음 설정을 추가하여 jar 종속성을 maven 프로젝트에 직접 추가합니다.
Jar 종속성 가져오기(2 메서드)
1# free library을 다운로드하고 압축을 푼 다음 Spire.Xls.jar 파일을 프로젝트에 종속성으로 추가합니다.
2# pom.xml에 다음 설정을 추가하여 jar 종속성을 maven 프로젝트에 직접 추가합니다.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
샘플 코드
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class setTrafficLightsIcons {
public static void main(String[] args) {
//Create a workbook
Workbook workbook = new Workbook();
//Add a worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add data to cells
sheet.getCellRange("A1").setText("Project Progress");
sheet.getCellRange("A2").setNumberValue(0.95);
sheet.getCellRange("A3").setNumberValue(0.5);
sheet.getCellRange("A4").setNumberValue(0.1);
sheet.getCellRange("A5").setNumberValue(0.9);
sheet.getCellRange("A6").setNumberValue(0.7);
sheet.getCellRange("A7").setNumberValue(0.6);
//set number format for the cell range
sheet.getCellRange("A2:A7").setNumberFormat("0%");
//Set row height and column width for the cell range
sheet.getAllocatedRange().setRowHeight(20);
sheet.getAllocatedRange().setColumnWidth(25);
//Add a conditional formatting
XlsConditionalFormats conditional = sheet.getConditionalFormats().add();
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format1 = conditional.addCondition();
//Set the conditional format type, formula, comparison operator type and color
format1.setFormatType(ConditionalFormatType.CellValue);
format1.setFirstFormula("300");
format1.setOperator(ComparisonOperatorType.Less);
format1.setFontColor(Color.black);
format1.setBackColor(Color.lightGray);
//Add a conditional formatting of traffic lights icon to the cell range
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format = conditional.addCondition();
format.setFormatType(ConditionalFormatType.IconSet);
format.getIconSet().setIconSetType(IconSetType.ThreeTrafficLights1);
//Save to file
workbook.saveToFile( "output/setTrafficLightsIcons.xlsx", ExcelVersion.Version2013);
}
}
