zoukankan      html  css  js  c++  java
  • POI读写07版及以上EXCEL图片篇

    第一部分:读

    package pic;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Iterator;
    import java.util.List;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFPictureData;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ReadPicFromExcel07 {
     public static void main(String[] args) throws Exception {
            /*读EXCEL图片,将excel中的图片保存到桌面上*/
      String basePath = "C:\\Documents and Settings\\Administrator\\桌面\\";
      FileInputStream fis = new FileInputStream(basePath + "pic.xlsx");
      XSSFWorkbook workbook = new XSSFWorkbook(fis);
      fis.close();
      List<XSSFPictureData> pictures = workbook.getAllPictures();
      for (int i = 0; i < pictures.size(); i++) {
       XSSFPictureData pictureData = pictures.get(i);
       byte[] data = pictureData.getData();
       String ext = pictureData.suggestFileExtension();//获取扩展名

       FileOutputStream out = new FileOutputStream(basePath + "img_" + i + "." + ext);
       out.write(data);
       out.close();
      }/*读EXCEL图片完毕*/
      
      /*读EXCEL文字内容*/
      XSSFSheet sheet = workbook.getSheetAt(0);
      Iterator<Row> rows = sheet.rowIterator();
      Row row;Cell cell;
      while(rows.hasNext()){
       row = rows.next();
       Iterator<Cell> cells = row.cellIterator();
       while(cells.hasNext()){
        cell = cells.next();
        System.out.println("RowIndex:"+cell.getRowIndex());
        System.out.println("ColumnIndex:"+cell.getColumnIndex());
        System.out.println("值:"+cell.getStringCellValue());
       }
      }
      /*
       * 图片在excel中,并不属于excel表格中的元素,可以理解为浮在表格上面,无法定格在表格中。
       * */
     }
    }

    第二部分:写

    package pic;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.apache.poi.ss.usermodel.ClientAnchor;
    import org.apache.poi.ss.usermodel.CreationHelper;
    import org.apache.poi.ss.usermodel.Drawing;
    import org.apache.poi.ss.usermodel.Picture;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.util.IOUtils;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class WritePicToExcel07 {
     public static void main(String[] args) throws Exception {
      /* 写EXCEL,将目标excel中图片写入到新的excel中 */
      String basePath = "C:\\Documents and Settings\\Administrator\\桌面\\";
      
      Workbook wb = new XSSFWorkbook();
      
      FileInputStream fis = new FileInputStream(basePath + "img_0.jpeg");
      byte[] bytes = IOUtils.toByteArray(fis);
      int pictureIdx = wb.addPicture(bytes, wb.PICTURE_TYPE_JPEG);
      fis.close();
      
      Sheet sheet = wb.createSheet("sheet1");
      //创建一个顶级容器
      Drawing drawing = sheet.createDrawingPatriarch();
      CreationHelper helper = wb.getCreationHelper();
      ClientAnchor anchor = helper.createClientAnchor();
      anchor.setCol1(3);
         anchor.setRow1(2);
         Picture pict = drawing.createPicture(anchor, pictureIdx);
         //auto-size picture relative to its top-left corner
         pict.resize();//该方法只支持JPEG 和 PNG后缀文件
         String file = "生成的EXCEL.xls";
         if(wb instanceof XSSFWorkbook) file += "x";
         FileOutputStream fos = new FileOutputStream(basePath+file);
      
    //  Row row = sheet.createRow(0);//生成第一行
    //  row.createCell(0).setCellValue("A");
    //  row.createCell(1).setCellValue("B");
      wb.write(fos);
      fos.close();
     }
    }

  • 相关阅读:
    生活中头疼脑热及医生诊断用药相关,持续更新
    python3 面试题 英文单词全部都是以首字母大写,逐个反转每个单词
    python 代码如何打包成.exe文件(Pyinstaller)
    charles使用
    经典bug
    python3面试-查找字符串数组中的最长公共前缀
    python3面试题 按规律写出下一个数1,11,21,1211,111221
    python3 测试的时候如何批量随机生成伪数据?(faker模块的)
    python3面试题-一个包含n个整数的数组a,判断a中是否存在三个元素,a,b,c,使得a+b+c=0
    python3面试-将N(N<10000)个人排成一排,从第1个人开始报数;如果报数是M的倍数就出列
  • 原文地址:https://www.cnblogs.com/dingmy/p/2954217.html
Copyright © 2011-2022 走看看