当前位置:编程学习 > JAVA >>

POI问题

公司在做报表工具,在导出excel的时候有时需要导出图标,但是POI框架又没办法操作excel自身的图标工具,所以打算用JFreeChart生成图表之后然后以图片的形式插入到excel模板当中,但现在的问题是插入的图片都会失真,求高手解答啊,代码附上

Workbook workBook = Util.createWorkBook("E:/test2.xls");
Sheet sheet = workBook.getSheetAt(0);
sheet.setDefaultColumnWidth(8);

CellRangeAddress range = Util.getRange(sheet)[0];

int startRow = range.getFirstRow();
int endRow = range.getLastRow();

double rowHeight = 0;
Row row;
for(int i = startRow;i <= endRow;i ++){
row = sheet.getRow(i);
rowHeight += row.getHeightInPoints();
}
rowHeight *= 1.33;
int height = Double.valueOf(rowHeight).intValue();

int startColumn = range.getFirstColumn();
int endColumn = range.getLastColumn();
double columnWidth = 0;
for(int i = startColumn;i <= endColumn;i ++){
columnWidth += sheet.getColumnWidth(i);
}
columnWidth /= 256;
columnWidth *= 8;
columnWidth *= 1.13;
int width = Double.valueOf(columnWidth).intValue();
//
// System.out.println("height:" + height + ",width:" + width);

// Workbook workBook = new HSSFWorkbook();
// Sheet sheet = workBook.createSheet();
CreationHelper helper = workBook.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();

anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
anchor.setCol1(3);
anchor.setRow1(4);
Drawing draw = sheet.createDrawingPatriarch();
Picture pic = draw.createPicture(anchor, workBook.addPicture(createPic(800, 600), Workbook.PICTURE_TYPE_PNG));
pic.resize();

OutputStream out = new FileOutputStream("E:/test1.xls");
workBook.write(out);
out.close();
--------------------编程问答-------------------- 看看这个吧!


public static void main(String[] args) {   
            FileOutputStream fileOut = null;   
            BufferedImage bufferImg =null;   
            BufferedImage bufferImg1 = null;   
            try{   
          //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray   
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
          ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();   
          bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));   
          bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));   
          ImageIO.write(bufferImg,"jpg",byteArrayOut);   
          ImageIO.write(bufferImg1,"jpg",byteArrayOut1);   

        //创建一个工作薄   
       HSSFWorkbook wb = new HSSFWorkbook();   
       HSSFSheet sheet1 = wb.createSheet("poi picT");   
       HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
       HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);   
       HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);   
       anchor1.setAnchorType(2);  
       //插入图片  
       patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
       patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
           fileOut = new FileOutputStream("c:/workbook.xls");   
           //写入excel文件  
           wb.write(fileOut);   
           fileOut.close();   
            }catch(IOException io){   
                    io.printStackTrace();   
                    System.out.println("io erorr : "+ io.getMessage());   
            } finally  
            {   
               if (fileOut != null)   
               {  
                   try {   
                              fileOut.close();   
                         }   
                   catch (IOException e)  
                   {   
                            // TODO Auto-generated catch block   
                            e.printStackTrace();   
                     }   
               }   
            }  
    }  

--------------------编程问答--------------------
引用
看看这个吧!
Java code

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public static void main(String[] args) {   
            FileOutputStream fileOut = null;   
            BufferedImage bufferImg =null;   
            BufferedImage bufferImg1 = null;   
            try{   
          //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray   
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
          ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();   
          bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));   
          bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));   
          ImageIO.write(bufferImg,"jpg",byteArrayOut);   
          ImageIO.write(bufferImg1,"jpg",byteArrayOut1);   
 
        //创建一个工作薄   
       HSSFWorkbook wb = new HSSFWorkbook();   
       HSSFSheet sheet1 = wb.createSheet("poi picT");   
       HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
       HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);   
       HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);   
       anchor1.setAnchorType(2);  
       //插入图片  
       patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
       patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
           fileOut = new FileOutputStream("c:/workbook.xls");   
           //写入excel文件  
           wb.write(fileOut);   
           fileOut.close();   
            }catch(IOException io){   
                    io.printStackTrace();   
                    System.out.println("io erorr : "+ io.getMessage());   
            } finally  
            {   
               if (fileOut != null)   
               {  
                   try {   
                              fileOut.close();   
                         }   
                   catch (IOException e)  
                   {   
                            // TODO Auto-generated catch block   
                            e.printStackTrace();   
                     }   
               }   
            }  
    }  

你好,谢谢你,这个代码我测试过,但是结果还是一样的 --------------------编程问答-------------------- POI有汉字乱码问题,用的时候小心点, 因为有源代码,发现后可以自己修改.
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,