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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM