关于#java#的问题,如何解决?

java使用spiredoc向table表格中填入数据,以及在对应位置填入图片

img



有帮助的话 采纳一下


import java.nio.file.Files; 
import java.nio.file.Paths;

import com.spire.doc.*;

public class TableDemo {
    public static void main(String[] args) throws Exception {
        // 创建文档
        Document doc = new Document();
        
        // 添加一个3行2列的表格
        Table table = doc.addTable(3, 2); 
        
        // 填入第一行第一列
        Cell cell = table.getRow(0).getCell(0);
        cell.setValue("Name");
        
        // 填入第二行第一列 
        cell = table.getRow(1).getCell(0);
        cell.setValue("John");
        
        // 获取图片文件路径
        String imgPath = "/path/to/image.png";  
        
        // 读取图片到字节数组
        byte[] imgBytes = Files.readAllBytes(Paths.get(imgPath)); 
        
        // 将图片添加到表格第二行第二列
        cell = table.getRow(1).getCell(1);
        cell.addImage(imgBytes);
        
        // 保存文档
        doc.saveToFile("table.docx");
    } 
}