자바 구현 그림 엑셀 로 그리 기

4099 단어 자바그림.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]);
 }
 
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기