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);
}
// 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);
}
解决方案 »
- 论坛的引用回复功能怎么做呀?求高手解答....
- 进入不了所有action 《Struts2》
- 请教:Jbuilder+MySQL开发EJB
- 求助:could not instantiate id generator
- URLConnection只能用在applet或application吗?为什么放在servlet里不行?
- EJB已经部署在服务器,那我客户端(用JSP)应怎么样部署HOME和REMOTE接口呢?
- tomcat中关于更新类后重启服务的问题?
- 江湖救急!网络书籍怎么选?还是继续《JAVA狠心技术卷2》?
- 有关包的导入问题,请教高手!急!
- javaweb 如何在展示的每一行数据后有 更新和删除图标 点击那个图标就会有更新和删除操作
- javamail如何发送带有图片的html??
- 100分求助
肯定可以,从Excel中取出图片保存在内存中,对内存中的图片数据(BufferImage)进行修改,然后更新Excel中的图片。