poi/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java

61 lines
2.1 KiB
Java
Executable File

/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Demonstrates how to insert pictures in a SpreadsheetML document
*
* @author Yegor Kozlov
*/
public class WorkingWithPictures {
public static void main(String[] args) throws IOException {
//create a new workbook
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
//add a picture in this workbook.
InputStream is = new FileInputStream("lilies.jpg");
int pictureIdx = wb.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG);
is.close();
//create sheet
XSSFSheet sheet = wb.createSheet();
//create drawing
XSSFDrawing drawing = sheet.createDrawingPatriarch();
//add a picture shape
XSSFPicture pict = drawing.createPicture(new XSSFClientAnchor(), pictureIdx);
//auto-size picture
pict.resize();
//save workbook
FileOutputStream fileOut = new FileOutputStream("xssf-picture.xlsx");
wb.write(fileOut);
fileOut.close();
}
}