图片用Excel画出来(JAVA)
2012-03-28 16:02
211 查看
能够将任何图片在excel上利用单元格背景完整的描绘出来。
像网络上出现的用excel画出超级玛丽等等,各种图片都能在excel上"画"出来。
图片我没有经过特殊处理,所以转换的图片不能太大,有多大的图片就要有多少的单元格。如640*480就有307200的单元格。
如要转换的图片:
转换后在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]); } }
相关文章推荐
- JAVA将Excel中的报表导出为图片格式(二)实现思路
- JAVA实现在excel插入图片(POI模式)
- java利用POI向Excel(xls)写入图片,并对图片引用超链接
- Java操作Excel(读、写、搜索关键字、插入图片)
- java poi 在excel中插入图片
- 在Java中读取并保存EXCEL文件中图片
- java 导出Excel 转图片地址为图片
- Java 调用 Apache POI 往 Excel 插入图片
- JAVA POI自动导出数据和图片到excel
- java画图程序_图片用字母画出来_源码发布
- Java实现图片上传到服务器,并把上传的图片读取出来
- Java将png、jpg、bmp等格式图片批量插入到Excel单个单元格内
- java POI导出excel(带有图片的数据)以及ftp上传图片到另一台服务器的方法参考代码
- java操作excel实现图片水印
- Java实现头像截取裁剪后图片保存,获取的是base64数据,怎么转换为图片并保存到数据库。并读取出来。
- Java实现图片上传到服务器,并把上传的图片读取出来
- Java:扫描包含图片的文件夹,将符合分辨率格式的复制出来
- java读取excel中的多图片
- POI java 处理excel上传图片
- JAVA_将JFreeChart图片导出到Excel