[Java] java操作excel插入图片 →→→→→进入此内容的聊天室

来自 , 2019-04-19, 写在 Java, 查看 130 次.
URL http://www.code666.cn/view/4d95d05a
  1. import com.smartxls.WorkBook;
  2.  
  3. import java.io.FileOutputStream;
  4.  
  5. public class ReadImageSample
  6. {
  7.  
  8.     public static void main(String args[])
  9.     {
  10.         try
  11.         {
  12.             WorkBook workBook = new WorkBook();
  13.  
  14.             //open the workbook
  15.             workBook.read("..\\template\\book.xls");
  16.  
  17.             String filename = "img";
  18.             int type = workBook.getPictureType(0);
  19.             if(type == -1)
  20.                 filename += ".gif";
  21.             else if(type == 5)
  22.                 filename += ".jpg";
  23.             else if(type == 6)
  24.                 filename += ".png";
  25.             else if(type == 7)
  26.                 filename += ".bmp";
  27.  
  28.             byte[] imagedata = workBook.getPictureData(0);
  29.            
  30.             FileOutputStream fos = new FileOutputStream(filename);
  31.             fos.write(imagedata);
  32.             fos.close();
  33.         }
  34.         catch (Exception e)
  35.         {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. import com.smartxls.PictureShape;
  44. import com.smartxls.ShapeFormat;
  45. import com.smartxls.WorkBook;
  46.  
  47. public class WriteImagesSample
  48. {
  49.  
  50.     public static void main(String args[])
  51.     {
  52.         try
  53.         {
  54.             WorkBook workBook = new WorkBook();
  55.  
  56.             //Inserting image
  57.             PictureShape pictureShape = workBook.addPicture(1, 0, 3, 8, "..\\template\\MS.GIF");
  58.             ShapeFormat shapeFormat = pictureShape.getFormat();
  59.             shapeFormat.setPlacementStyle(ShapeFormat.PlacementFreeFloating);
  60.             pictureShape.setFormat();
  61.  
  62.             workBook.write(".\\pic.xls");
  63.         }
  64.         catch (Exception e)
  65.         {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69. }
  70. //java/1305

回复 "java操作excel插入图片"

这儿你可以回复上面这条便签

captcha