com.automaster.autoview.server.servlet.TesteImagemPOI.java Source code

Java tutorial

Introduction

Here is the source code for com.automaster.autoview.server.servlet.TesteImagemPOI.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.automaster.autoview.server.servlet;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 *
 * @author Adriano
 */
public class TesteImagemPOI {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
        byte data[] = new byte[8000];
        try {
            new DataInputStream(new FileInputStream(
                    "D:\\Users\\Adriano\\Documents\\NetBeansProjects\\JRGWT\\web\\imagens\\logo.jpg")).read(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int index = wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG);

        HSSFClientAnchor ancora = new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 5, (short) 10, 10);
        ancora.setAnchorType(2);
        sheet.createDrawingPatriarch().createPicture(ancora, index);

        //Write the Excel file
        FileOutputStream fileOut = null;
        fileOut = new FileOutputStream("C://myFile.xls");
        wb.write(fileOut);
        fileOut.close();

    }
}