package com.sunxung.factoring.component.util;

import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

/**
 * @className:ConvertToImage
 * @description:excel转换成图片
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年12月24日 下午3:56:37
 */
public class ConvertToImage {

    /**
     * @param filepath .xls或者.xlsx文件的路径
     * @parampicpath jpg或者png图片的路径
     */
    public static void convertToImage(String filepath, String picpath) {
        // Create a new Workbook object
        // Open a template excel file
        Workbook book = null;
        try {
            // book = new Workbook(dataDir + "2018各项目情况.xlsx");
            book = new Workbook(filepath);
            // Get the first worksheet
            // Worksheet sheet = book.getWorksheets().get(0);
            Worksheet sheet = book.getWorksheets().get(0);
            // Define ImageOrPrintOptions
            ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
            // Specify the image format
            imgOptions.setImageFormat(ImageFormat.getJpeg());
            imgOptions.setCellAutoFit(true);
            imgOptions.setOnePagePerSheet(true);
            imgOptions.setOnlyArea(true);
            // imgOptions.setDefaultFont("200");
            // Render the sheet with respect to specified image/print options
            SheetRender render = new SheetRender(sheet, imgOptions);
            // Render the image for the sheet
            // render.toImage(0, dataDir + "SheetImage.jpg");
            render.toImage(0, picpath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        convertToImage("/Users/duxiaojiao/Documents/workspace/workfile/fileResource/other/国内有追保理信息表.xls",
                "/Users/duxiaojiao/Downloads/SheetImageTest.jpg");
    }
}
