请问如何用java对EXECL插入图片 jxl好像可以。不过我们用的时候,都是放一个已经有相应图片的excel文件做模版。然后再动态的写数据。good luck 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 见有文章写道,jxl只可以操作png格式的图片,是不是这样的呀?不知你那里有没有代码可以贴出来。非常感谢!! apache的POI项目支持。http://jakarta.apache.org/poi/index.html以下代码是实现在Excel中画图功能,插入图片就更好处理了。public static void testDrawGrahpics() throws Exception{ // Create a workbook with one sheet and size the first three somewhat // larger so we can fit the chemical structure diagram in. HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet( "my drawing" ); sheet.setColumnWidth((short)1, (short)(256 * 27)); HSSFRow row1 = sheet.createRow(0); row1.setHeightInPoints(10 * 15); HSSFRow row2 = sheet.createRow(1); row2.setHeightInPoints(5 * 15); HSSFRow row3 = sheet.createRow(2); row3.setHeightInPoints(10 * 15); // Add some cells so we can test that the anchoring works when we // sort them. row1.createCell((short)0).setCellValue("C"); row2.createCell((short)0).setCellValue("A"); row3.createCell((short)0).setCellValue("B"); // Create the top level drawing patriarch. HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor a; HSSFShapeGroup group; EscherGraphics g; EscherGraphics2d g2d; //g2d.drawImage(); // Anchor entirely within one cell. a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 ); group = patriarch.createGroup( a ); group.setCoordinates( 0, 0, 320, 276 ); float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1()); g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel ); g2d = new EscherGraphics2d( g ); drawStar( g2d ); a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 1, (short) 1, 1 ); group = patriarch.createGroup( a ); group.setCoordinates( 0, 0, 640, 276 ); verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());// verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet); g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel ); g2d = new EscherGraphics2d( g ); drawStar( g2d ); FileOutputStream out = new FileOutputStream("workbook.xls"); wb.write(out); out.close(); } private static void drawStar( EscherGraphics2d g2d ) { g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); for (double i = 0; i < Math.PI; i += 0.1) { g2d.setColor( new Color((int)(i * 5343062d) ) ); int x1 = (int) ( Math.cos(i) * 160.0 ) + 160; int y1 = (int) ( Math.sin(i) * 138.0 ) + 138; int x2 = (int) ( -Math.cos(i) * 160.0 ) + 160; int y2 = (int) ( -Math.sin(i) * 138.0 ) + 138; g2d.drawLine(x1,y1,x2,y2); } g2d.setFont(new Font("SansSerif",Font.BOLD | Font.ITALIC, 20)); g2d.drawString("EscherGraphics2d",70,100); g2d.setColor(Color.yellow); g2d.fillOval( 160-20,138-20,40,40); g2d.setColor(Color.black); g2d.fillPolygon(new int[] {-10+160,0+160,10+160,0+160}, new int[] {0+138,10+138,0+138,-10+138}, 4); g2d.drawPolygon(new int[] {-160+160,0+160,160+160,0+160}, new int[] {0+138,138+138,0+138,-138+138}, 4); } GJA106(中文字符) 你好,谢谢你的回复!你贴的这些代码是否是往Excel里插入多边形,一些划线的功能?我在程序中用了FORMULA ONE 工具对Excel进行操作,这些功能都可以实现。但是插图的功能没有。POI开源我曾经查看过,但没发现有对图片进行操作的功能。是否是这样的呢? 按楼主的意思是想:对Excel表中已经存在的图片进行编辑?肯定可以,从Excel中取出图片保存在内存中,对内存中的图片数据(BufferImage)进行修改,然后更新Excel中的图片。 是在Excel中的某个行列位置,插入一张图片 是在Excel中的某个行列位置,插入一张图片。图片来自于某个文件路径或者数据库中的blob类型文件,不是对Excel中已存在的图片进行修改。 ///我用的是jxl包往excel中插入图片,我的图片是放到Blob字段的。File file1=new File(dataTest.getTestName()+".xls");WritableWorkbook wwb = Workbook.createWorkbook(file1); WritableSheet ws=wwb.createSheet(dataTest.getTestName(),0);///添加图片 InputStream in = dataSubject.getSubjectImg().getBinaryStream(); ///转变成数据流 BufferedImage image = ImageIO.read(in); File file1 = new File("1.png"); ImageIO.write(image, "png", file1); ///计算图片跨多少行 imgWidth = dataSubject.getImgTrueWidth(); imgHeight = dataSubject.getImgTrueHeight(); colCount = imgWidth / cellWidth; rowCount = imgHeight / cellHeight; if (imgWidth % cellWidth != 0) { colCount = colCount + 1; } if (imgHeight % cellHeight != 0) { rowCount = rowCount + 1; } WritableImage wai = new WritableImage(0, iRow, colCount, rowCount,file1); ws.addImage(wai); in.close();///希望对你有帮助。 根据你的提示,我做了个测试。但是没有找到ImageIO这个类,就按下面的方法创建了一个img。不知img是否可以按这种方法生成。运行完后,原来的Excel文件变成了0字节大小的文件。 File myFile = new File(sFilePath);WritableWorkbook wwb = Workbook.createWorkbook(myFile);WritableSheet ws = wwb.createSheet(sFileName, 0);jxl.write.WritableImage img = new jxl.write.WritableImage(0, 0, 1, 1, new File("D:\\reportFile\\a.png"));ws.addImage(img);你知道是什么原因吗?谢谢关注 请问怎样能实现图文混排的发送,就像QQ那样,先从本机里面找出一张图片,放在聊天框里,然后输入文字,按发送,一起发给对方,对方也能收到图片和文字 关于父类private方法的覆盖问题。 求助!now,,爲什麽沒有輸出結果啊 可否通过JAVA控制其它程序 如何能将字符串中的所有数字都删除 请问..如何实现一个文件夹的复制? java里面有矩阵分析方面的包吗?? 请问大家JBoss的稳定性和效率如何? 超级新手问题,java的程序用什么来编写呢? Socket服务器给客户端发消息 求助*我的java程序编译不过去,又看不出什么问题,请帮忙~ 求一个纯JAVA的处理文件和目录的程序
不知你那里有没有代码可以贴出来。
非常感谢!!
// Create a workbook with one sheet and size the first three somewhat
// larger so we can fit the chemical structure diagram in.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet( "my drawing" );
sheet.setColumnWidth((short)1, (short)(256 * 27));
HSSFRow row1 = sheet.createRow(0);
row1.setHeightInPoints(10 * 15);
HSSFRow row2 = sheet.createRow(1);
row2.setHeightInPoints(5 * 15);
HSSFRow row3 = sheet.createRow(2);
row3.setHeightInPoints(10 * 15); // Add some cells so we can test that the anchoring works when we
// sort them.
row1.createCell((short)0).setCellValue("C");
row2.createCell((short)0).setCellValue("A");
row3.createCell((short)0).setCellValue("B"); // Create the top level drawing patriarch.
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor a;
HSSFShapeGroup group;
EscherGraphics g;
EscherGraphics2d g2d;
//g2d.drawImage();
// Anchor entirely within one cell.
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
group = patriarch.createGroup( a );
group.setCoordinates( 0, 0, 320, 276 );
float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
g2d = new EscherGraphics2d( g );
drawStar( g2d ); a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 1, (short) 1, 1 );
group = patriarch.createGroup( a );
group.setCoordinates( 0, 0, 640, 276 );
verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
// verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
g2d = new EscherGraphics2d( g );
drawStar( g2d ); FileOutputStream out = new FileOutputStream("workbook.xls");
wb.write(out);
out.close(); } private static void drawStar( EscherGraphics2d g2d )
{
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
for (double i = 0; i < Math.PI; i += 0.1)
{
g2d.setColor( new Color((int)(i * 5343062d) ) );
int x1 = (int) ( Math.cos(i) * 160.0 ) + 160;
int y1 = (int) ( Math.sin(i) * 138.0 ) + 138;
int x2 = (int) ( -Math.cos(i) * 160.0 ) + 160;
int y2 = (int) ( -Math.sin(i) * 138.0 ) + 138;
g2d.drawLine(x1,y1,x2,y2);
}
g2d.setFont(new Font("SansSerif",Font.BOLD | Font.ITALIC, 20));
g2d.drawString("EscherGraphics2d",70,100);
g2d.setColor(Color.yellow);
g2d.fillOval( 160-20,138-20,40,40);
g2d.setColor(Color.black);
g2d.fillPolygon(new int[] {-10+160,0+160,10+160,0+160}, new int[] {0+138,10+138,0+138,-10+138}, 4);
g2d.drawPolygon(new int[] {-160+160,0+160,160+160,0+160}, new int[] {0+138,138+138,0+138,-138+138}, 4);
}
肯定可以,从Excel中取出图片保存在内存中,对内存中的图片数据(BufferImage)进行修改,然后更新Excel中的图片。
WritableWorkbook wwb = Workbook.createWorkbook(file1);
WritableSheet ws=wwb.createSheet(dataTest.getTestName(),0);
///添加图片
InputStream in = dataSubject.getSubjectImg().getBinaryStream(); ///转变成数据流
BufferedImage image = ImageIO.read(in);
File file1 = new File("1.png");
ImageIO.write(image, "png", file1);
///计算图片跨多少行
imgWidth = dataSubject.getImgTrueWidth();
imgHeight = dataSubject.getImgTrueHeight();
colCount = imgWidth / cellWidth;
rowCount = imgHeight / cellHeight;
if (imgWidth % cellWidth != 0) {
colCount = colCount + 1;
}
if (imgHeight % cellHeight != 0) {
rowCount = rowCount + 1;
} WritableImage wai = new WritableImage(0, iRow, colCount, rowCount,file1); ws.addImage(wai);
in.close();///希望对你有帮助。
面的方法创建了一个img。不知img是否可以按这种方法生成。运行完后,原
来的Excel文件变成了0字节大小的文件。
File myFile = new File(sFilePath);
WritableWorkbook wwb = Workbook.createWorkbook(myFile);
WritableSheet ws = wwb.createSheet(sFileName, 0);
jxl.write.WritableImage img = new jxl.write.WritableImage(0, 0, 1,
1, new File("D:\\reportFile\\a.png"));
ws.addImage(img);
你知道是什么原因吗?谢谢关注