posts - 5,  comments - 7,  trackbacks - 0

 

 1package test; 
 2
 3import java.io.ByteArrayOutputStream; 
 4import java.io.File; 
 5import java.io.FileInputStream; 
 6import java.io.FileOutputStream; 
 7import java.io.IOException; 
 8
 9import org.apache.poi.hssf.usermodel.HSSFClientAnchor; 
10import org.apache.poi.hssf.usermodel.HSSFPatriarch; 
11import org.apache.poi.hssf.usermodel.HSSFSheet; 
12import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
13
14public class PicExcelTest 
15
16    public static void main(String[] args) throws IOException 
17         
18        FileOutputStream out = new FileOutputStream("D:/workbook.xls");   
19        HSSFWorkbook wb = new HSSFWorkbook(); 
20         
21        HSSFSheet sheet1 = wb.createSheet("picture"); 
22        HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();;    
23        HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)8,20); 
24        HSSFClientAnchor anchor2 = new HSSFClientAnchor(0,0,0,0,(short)0,24,(short)8,44);  
25        patriarch.createPicture(anchor1, wb.addPicture(loadImage("D:/Sunset.jpg"),HSSFWorkbook.PICTURE_TYPE_JPEG));     
26        patriarch.createPicture(anchor2, wb.addPicture(loadImage("D:/Water lilies.jpg"),HSSFWorkbook.PICTURE_TYPE_JPEG)); 
27      
28        wb.write(out);   
29        out.close();  
30    }
 
31     
32    public static byte[] loadImage(String filePath) 
33        try 
34            FileInputStream input = new FileInputStream(new File(filePath)); 
35            ByteArrayOutputStream output = new ByteArrayOutputStream(); 
36            byte buf[] = new byte[1024]; 
37            for (;;) 
38                int noBytesRead = input.read(buf); 
39                if (noBytesRead == -1
40                    return output.toByteArray(); 
41                }
 
42                output.write(buf, 0, noBytesRead); 
43            }
 
44        }
 catch (IOException e) 
45            throw new RuntimeException(e); 
46        }
 
47    }
 
48}

49
posted on 2008-11-25 11:40 Vincent-chen 阅读(250) 评论(0)  编辑  收藏 所属分类: POI

只有注册用户登录后才能发表评论。


网站导航: