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();
     }
    }

  • 相关阅读:
    【题解】CodeChef
    【题解】AT1984 Wide Swap(拓扑排序)
    【题解】CF917D Stranger Trees(prufer序列+二项式反演)
    【题解】UVA
    【题解】P3980 [NOI2008]志愿者招募(费用流求线性规划)
    【题解】AT2064 Many Easy Problems(转换+NTT)
    【题解】AT1983 BBQ Hard (格路)
    【总结】不同卷积如何来搞
    【瞎讲】 Cayley-Hamilton 常系数齐次线性递推式第n项的快速计算 (m=1e5,n=1e18)
    计算几何小结计算几何小结
  • 原文地址:https://www.cnblogs.com/dingmy/p/2954217.html
Copyright © 2011-2022 走看看