- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
- import org.apache.poi.hssf.usermodel.HSSFPatriarch;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- public class ExportPictureToExcel {
- public static void main(String args[]){
- exportChartToExcel();
- }
- private static void exportChartToExcel(){
- String excelFileName = "E://platoDemo.xls";
- String pictureFileName = "E://platoDemo.jpg";
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet();
- ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
- try {
- BufferedImage bufferImg = ImageIO.read(new File(pictureFileName));
- ImageIO.write(bufferImg, "JPG", byteArrayOut);
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short) 0, 0, (short) 10, 20);
- HSSFPatriarch patri = sheet.createDrawingPatriarch();
- patri.createPicture(anchor, wb.addPicture(byteArrayOut
- .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
- FileOutputStream outStream = new FileOutputStream(excelFileName);
- wb.write(outStream);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
用Java將圖片寫入到Excel中
最新推荐文章于 2023-12-09 10:10:47 发布