Java를 사용하여 Excel에서 평균 이하 또는 이상의 값 강조 표시
설치(2가지 방법)
1# free library을 다운로드하고 압축을 푼 다음 Spire.Xls.jar 파일을 종속성으로 프로젝트에 추가합니다.
2# pom.xml에 다음 구성을 추가하여 maven 프로젝트에 jar 종속성을 추가할 수도 있습니다.
<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>3.9.1</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 HighlightBelowAboveAverage {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Use conditional formatting to highlight the values below average in the range "B2:B9"
XlsConditionalFormats format1 = sheet.getConditionalFormats().add();
format1.addRange(sheet.getCellRange("B2:B9"));
IConditionalFormat cf1 = format1.addAverageCondition(AverageType.Below);
cf1.setBackColor(Color.red);
//Use conditional formatting to highlight the values above average in the range "B2:B9"
XlsConditionalFormats format2 = sheet.getConditionalFormats().add();
format2.addRange(sheet.getCellRange("B2:B9"));
IConditionalFormat cf2 = format1.addAverageCondition(AverageType.Above);
cf2.setBackColor(Color.yellow);
//Get the count of values below average
sheet.getCellRange("D12").setFormula("=COUNTIF(B2:B9,\"<\"&AVERAGE(B2:B9))");
//Get the count of values above average
sheet.getCellRange("D13").setFormula("=COUNTIF(B2:B9,\">\"&AVERAGE(B2:B9))");
//Save the file
workbook.saveToFile("BolowOrAboveAverage.xlsx", ExcelVersion.Version2016);
}
}
전에
후에
Reference
이 문제에 관하여(Java를 사용하여 Excel에서 평균 이하 또는 이상의 값 강조 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/codesharing/highlight-values-below-or-above-average-in-excel-using-java-3mj3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)