java实现图片用Excel画出来

所属分类: 软件编程 / java 阅读数: 56
收藏 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]);
 }
 
}

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

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

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多