Java/Excel에 이미지 주석 추가

Excel 문서에서 특정 셀에 주석을 추가하여 추가 정보를 추가하거나 셀 내용을 설명할 수 있습니다. 외에도 이 무료 API는 이미지 댓글 추가도 지원합니다. 이 기사에서는 Java 애플리케이션에서 이 작업을 수행하는 방법을 설명합니다.

JAR 종속성 가져오기(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>


이미지 댓글 추가



자세한 단계와 전체 코드는 다음과 같습니다.
● Workbook 인스턴스를 생성한 다음 Workbook.loadFromFile() 메서드를 사용하여 Excel 파일을 로드합니다.
● Workbook.getWorksheets().get() 메서드를 사용하여 Excel 파일의 첫 번째 워크시트를 가져옵니다.
● Worksheet.getCellRange() 메서드를 사용하여 특정 셀을 가져옵니다.
● Cellrange.addComment() 메서드를 사용하여 해당 셀에 설명을 추가합니다.
● 이미지를 로드하고 ExcelComment.getFill().customPicture() 메서드를 사용하여 이미지로 주석을 채웁니다.
● 주석의 높이/너비를 설정하고 ExcelComment 클래스에서 제공하는 메소드를 사용하여 주석을 표시하도록 설정합니다.
● Workbook.saveToFile() 메서드를 사용하여 문서를 다른 파일로 저장합니다.

import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageComment {
    public static void main(String[] args)throws IOException {
        //Load the sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("member.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the specific cell
        CellRange range = sheet.getCellRange("E7");
        //Add the commet
        ExcelComment comment = range.addComment();
        //Load an image
        BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\flag.jpg"));
        //Use the image to fill the comment
        comment.getFill().customPicture(bufferedImage, "C:\\Users\\Administrator\\Desktop\\flag.jpg");

        //Set the height and width for the comment
        comment.setHeight(bufferedImage.getHeight());
        comment.setWidth(bufferedImage.getWidth());
        //Show the comment
        comment.setVisible(true);

        //Save the document to file
        workbook.saveToFile("SetImageComment.xlsx", ExcelVersion.Version2013);
    }
}


좋은 웹페이지 즐겨찾기