zoukankan      html  css  js  c++  java
  • java poi 向excel写入图片

    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    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 Picture {
        public static void main(String[] args) {
            //String filepath = SpringConfigUtil.getValue("filepath");
            //String picturepath = SpringConfigUtil.getValue("picturepath");
            //int rowNum = Integer.parseInt(SpringConfigUtil.getValue("rowNum"));
           // int cellNum = Integer.parseInt(SpringConfigUtil.getValue("cellNum"));
            boolean result = insertPicture("E:\data\file\201509\2015.xls", "E:\data\file\201509\test.jpg", 3, 5);// 1代表插入的行数-1
                                                                    // ,2代表插入的列数-1
    
            System.out.println("图片插入结果为==" + result);
    
        }
    
        /* 插入图片地址 文件地址 插入图片位置的关键字 图片类型 */
    
        /**
         * @param filePath
         * @param picturePath
         * @param rowNum
         * @param cellNum
         * @return
         */
        public static boolean insertPicture(String filePath, String picturePath, int rowNum, int cellNum) {
            // 初始化IO流内容
    
            FileOutputStream fileOut = null;
            BufferedImage bufferImg = null;
            int rowN = rowNum;// 图片插入行的初始化
            int cellN = cellNum;// 图片插入列的初始化
            try {
    
                // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    
                bufferImg = ImageIO.read(new File(picturePath));// 图片地址
    
                ImageIO.write(bufferImg, "png", byteArrayOut);
    
                // 创建一个工作薄
                HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
                HSSFSheet sheet1 = wb.getSheetAt(0);
    
                // 创建插入图片需要的容器
                HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
                /*
                 * HSSFClientAnchor几个数字解释:3:是x轴的开始节点, 0:
                 * 是y轴的开始节点,1023:是x轴的结束节点,255:是y轴的结束节点
                 * ,1:是从Excel的2列开始插入图片,10:是从excel的第11行开始插入图片,
                 * 11:图片占用11列的位置,25:图片结束在excel的26行
                 */
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) ((short) cellN), (rowN),
                        (short) ((short) cellN + 1), (rowN));
    
                anchor.setAnchorType(2);
    
                // 插入图片
    
                patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
    
                fileOut = new FileOutputStream(filePath);
                // 写入excel文件
                wb.write(fileOut);
                fileOut.close();
                return true;
            } catch (IOException io) {
    
                io.printStackTrace();
    
                System.out.println("io erorr :  " + io.getMessage());
                return false;
    
            } finally {
    
                if (fileOut != null) {
    
                    try {
    
                        fileOut.close();
    
                    } catch (IOException e) {
    
                        e.printStackTrace();
    
                    }
                }
            }
    
        }
    }

    参数读取:

    import java.util.Properties;
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    public class SpringConfigUtil {
    
        private static Properties spring_props = new Properties();
        static {
            try {
                Resource resource = new ClassPathResource("/META-INF/config/app-config.properties");
                spring_props = PropertiesLoaderUtils.loadProperties(resource);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static String getValue(String key) {
            String value = "";
            if (spring_props.containsKey(key)) {
                value = spring_props.getProperty(key, "");
            }
            return value;
        }
    }
  • 相关阅读:
    https://和http://区别
    选择排序法
    冒泡排序法-----一点也不简单喔
    hadoop启动后没有datanode怎么办
    洛谷 1379 八数码难题
    Floyd 算法详解
    datagridview 点击列标题排序
    Python【每日一问】05
    Python【每日一问】04
    Python【每日一问】03
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4877176.html
Copyright © 2011-2022 走看看