日期:2014-05-20  浏览次数:20826 次

==jxl导入图片问题。周五了呵呵==
下面是网上 拷贝的一个程序 。。。
现在是无法获取图片的真实大小,只能获取到他的 像素大小,但是加入excel中
需要知道 真是 尺寸,才能在excel加入原图一样大小的图片大家有遇到这个问题吗?


Java code

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.ImageIcon;

import jxl.Workbook;
import jxl.write.WritableImage;

public class Test
{
    public static void writeImageExcel(OutputStream os) throws Exception
    {
        //一定要流的形式创建这个对象  
        jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
        jxl.write.WritableSheet ws = wwb.createSheet("写入图片", 0);
        File image = new File("d:\\22.png");

        //        ImageIcon imageIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage("d:\\22.png"));
        //
        //        int height = imageIcon.getIconHeight();
        //        int width = imageIcon.getIconWidth();
        //        System.out.println(height + " "+ width);
        java.awt.image.BufferedImage bi7 = null;
        try
        {
            bi7 = javax.imageio.ImageIO.read(image);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        int picWidth = bi7.getWidth(); // 图片宽, 像素 150
        int picHeight = bi7.getHeight(); // 图片高, 像素 105

        //test
        System.out.println("--1703");
        System.out.println("Width=" + picWidth );
        System.out.println("Height=" + picHeight );

        // 输入参数, 图片显示的位置
        double picBeginCol = 1.2;
        double picBeginRow = 1.2;

        /*
            实际像素: 150/105 = 2.78 cm / 3.97 cm = 4832 / 1590
            实际像素: 300/210 =           2倍大小 = 9600 / 3150 比率: 32 / 15
        */
        // 计算参数( picCellWidth, picCellHeight ), 图片显示大小, 默认 100% 显示: begin
        //     图片cell宽度 =  图片实际跨越每个cell所占长度的相对各个cell ratio的和
        //     方法: 根据起始位置,计算图片实际跨越的区域, 然后计算相对ratio,然后累加
        //
        double picCellWidth = 0.0; // 是 cell的跨越个数, 可小数
        double picCellHeight = 0.0;
        // wc = ws.getWritableCell( picBeginCol, picBeginRow ); // 列,行
        // ws.getColumnView( picBeginCol ).getSize();
        // ws.getRowView( picBeginRow ).getSize();

        int _picWidth = picWidth * 32 ; // pic的宽度,循环递减, 是jxl的宽度单位, 32/15
        for(int x=0; x< 1234; x++)
        {
            int bc = (int)Math.floor( picBeginCol + x ); // 3.6 to 3 // 本次循环所在cell位置
            System.out.println("x =" + x ); //test
            System.out.println("bc =" + bc ); //test
            int v = ws.getColumnView( bc ).getSize(); //本次cell宽,jxl单位
            double _offset0 = 0.0; // >= 0 // 离左边的偏移量, 仅 x = 0 的时候才用
            if( 0 == x )
                _offset0 = ( picBeginCol - bc ) * v ; //
           
            System.out.println("_offset0  =" + _offset0 ); //test
            System.out.println("_picWidth =" + _picWidth ); //test
            System.out.println("v =" + v ); //test
            System.out.println("cw 00=" + ws.getColumnView( 0 ).getSize() ); //test
            System.out.println("ch 00=" + ws.getRowView( 0 ).getSize() ); //test
            System.out.println("cw 22=" + ws.getColumnView( 2 ).getSize() ); //test
            System.out.println("ch 22=" + ws.getRowView( 2 ).getSize() ); //test
           
            if( 0.0 + _offset0 + _picWidth > v ) // _picWidth 剩余长度超过一个cell时
            {
                // 计算本次cell内, pic 所占 ratio值, 累加到 picCellWidth
                double _ratio = 1.0;
                if( 0 == x )
                    _ratio = ( 0.0 + v - _offset0 ) / v;
                System.out.println("_ratio =" + _ratio ); //test
                // picCellWidth += 1.0;
                picCellWidth += _ratio;
                _picWidth -= (int)( 0.0 + v - _offset0 ); // int
            }
            else // _picWidth 剩余长度在一个cell内时
            {
                double _ratio = 0.0;
                if( v != 0 )
                    _ratio = ( 0.0 + _picWidth ) / v;
                picCellWidth += _ratio;
                System.out.println("for: picCellWidth =" + picCellWidth ); //test
                break;
            }
            if( x >= 1233 )
                {}
        } // for
        // 此时 picCellWidth 是图片实际的值了

        //
        int _picHeight = picHeight * 15 ; // pic的高度,循环递减, 是jxl的高度单位, 32/15
        for(int x=0; x< 1234; x++)
        {
            int bc = (int)Math.floor( picBeginRow + x ); // 3.6 to 3 // 本次循环所在cell位置
            int v = ws.getRowView( bc ).getSize(); //本次cell高,jxl单位
            double _offset0 = 0.0; // >= 0 // 离顶部的偏移量, 仅 x = 0 的时候才用
            if( 0 == x )
                _offset0 = ( picBeginRow - bc ) * v ; //
            if( 0.0 + _offset0 + _picHeight > v ) // _picHeight 剩余长度超过一个cell时
            {
                // 计算本次cell内, pic 所占 ratio值, 累加到 picCellHeight
                double _ratio = 1.0;
                if( 0 == x )
                    _ratio = ( 0.0 + v - _offset0 ) / v;
                // picCellHeight += 1.0;
                picCellHeight += _ratio;
                _picHeight -= (int)( 0.0 + v - _offset0 ); // int
            }
            else // _picHeight 剩余长度在一个cell内时
            {
                double _ratio = 0.0;
                if( v != 0 )
                    _ratio = ( 0.0 + _picHeight ) / v;
                picCellHeight += _ratio;
                break;
            }
            if( x >= 1233 )
                {}
        } // for
        // 此时 picCellHeight 是图片实际的值了
        // 计算参数( picCellWidth, picCellHeight ), 图片显示大小, 默认 100% 显示: end
       

        //test
        System.out.println("picBeginCol=" + picBeginCol );
        System.out.println("picBeginRow=" + picBeginRow );
        System.out.println("picCellWidth=" + picCellWidth );
        System.out.println("picCellHeight=" + picCellHeight );

        WritableImage wimage = new WritableImage(0, 0, picCellWidth, picCellHeight, image);
        //WritableImage wimage = new WritableImage(0, 0, 3, 2.3, image);
        //WritableImage wimage1 = new WritableImage(3, 0, 3, 2.3, image);
        ws.addImage(wimage);
        //ws.addImage(wimage1);

        // 写入Exel工作表  
        wwb.write();
        // 关闭Excel工作薄对象  
        wwb.close();

    }

    public static void main(String args[]) throws Exception
    {
        try
        {

            File f = new File("d:\\image.xls");
            //这里要以流的形式,不要以文件的形式  
            Test.writeImageExcel(new FileOutputStream(f));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}