java实现图片用Excel画出来

所属分类: 软件编程 / java 阅读数: 39
收藏 0 赞 0 分享

本文实例为大家分享了java用Excel将图片画出来的具体代码,供大家参考,具体内容如下

能够将任何图片在excel上利用单元格背景完整的描绘出来。

像网络上出现的用excel画出超级玛丽等等,各种图片都能在excel上"画"出来。

图片我没有经过特殊处理,所以转换的图片不能太大,有多大的图片就要有多少的单元格。如640*480就有307200的单元格。

如要转换的图片:

转换后在excel中的效果:

没多大意义练练手:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
 
import javax.swing.ImageIcon;
 
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
public class Helper {
 private BufferedImage getBufferedImage(String filepath)
 {
 ImageIcon imgicon=new ImageIcon(filepath);
 BufferedImage bufferedImage = new BufferedImage(imgicon.getIconWidth(),imgicon.getIconHeight(),BufferedImage.TYPE_INT_RGB);  
 
 bufferedImage.createGraphics().drawImage(imgicon.getImage(), 0, 0,null);
 return bufferedImage;
 
 }
 
 private Colour getNearestColour(Color awtColor) { 
  Colour color = null; 
  Colour[] colors = Colour.getAllColours(); 
  if ((colors != null) && (colors.length > 0)) { 
  Colour crtColor = null; 
  int[] rgb = null; 
  int diff = 0; 
  int minDiff = 999; 
  
  for (int i = 0; i < colors.length; i++) { 
  crtColor = colors[i]; 
  rgb = new int[3]; 
  rgb[0] = crtColor.getDefaultRGB().getRed(); 
  rgb[1] = crtColor.getDefaultRGB().getGreen(); 
  rgb[2] = crtColor.getDefaultRGB().getBlue(); 
  
  diff = Math.abs(rgb[0] - awtColor.getRed()) 
   + Math.abs(rgb[1] - awtColor.getGreen()) 
   + Math.abs(rgb[2] - awtColor.getBlue()); 
  
  if (diff < minDiff) { 
   minDiff = diff; 
   color = crtColor; 
  } 
  } 
  } 
  if (color == null) 
  color = Colour.BLACK; 
  return color; 
  } 
 public void exec(String convertFromImage,String createxls) throws Exception
 {
 
 WorkbookSettings ws = new WorkbookSettings();
  ws.setLocale(new Locale("en", "EN"));
  WritableWorkbook workbook = Workbook.createWorkbook(new File(createxls), ws);
  WritableSheet s2 = workbook.createSheet("picture", 0);
  BufferedImage buffimage= getBufferedImage(convertFromImage);
  int width=buffimage.getWidth();
  int heigh=buffimage.getHeight();
  for(int i=0;i<width;i++)
  {
   for(int h=0;h<heigh;h++)
  {
    WritableCellFormat greyBackground = new WritableCellFormat();
    Color c = new Color(buffimage.getRGB(i, h)); 
   
  
  greyBackground.setBackground( getNearestColour(c) );
  
  Label lr = new Label(i, h, "", greyBackground);
 
  s2.addCell(lr);
  }
  
  }
   
 
  //写入Excel对象 
  workbook.write(); 
 
 
  workbook.close();
 
 }
 /**
 * @param args
 * @throws IOException 
 * @throws BiffException 
 */
 public static void main(String[] args) throws Exception {
 Helper helper=new Helper();
 
 System.out.println("输入的图片:"+args[0]);
 System.out.println("输出的excel:"+args[1]);
 System.out.println("转换开始");
 //转换执行的方法参数 args[0]输入的图片路径 args[1]输出的excel路径
// helper.exec( "mslogo.jpg","1.xls");
 helper.exec(args[0],args[1]);
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多