자바 구현 그림 엑셀 로 그리 기
모든 그림 을 엑셀 에서 셀 배경 을 이용 하여 완전 하 게 묘사 할 수 있다.
인터넷 에 등장 하 는 엑셀 로 슈퍼 마 리 를 그 리 는 등 각종 사진 이 엑셀 에'그 려'된다.
그림 은 제 가 특별한 처 리 를 거치 지 않 았 기 때문에 전환 한 그림 은 너무 크 면 안 되 고 큰 그림 이 있 으 면 셀 이 있어 야 합 니 다.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]);
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.