java: 用 jxl: 精确控制图片的位置和大小

java: 用 jxl: 精确控制图片的位置和大小

        ......
  /*
  // ok: 从本地文件得到图片资源,写到xls中
  java.io.File image8 = new java.io.File("E://logo.png");
  WritableImage wimage = new WritableImage( 2, 7, 2.5, 2.5, image8 );
  ws.addImage( wimage );
  */
        
        // ok: 从internet得到图片资源,写到xls中
        HttpClient client = new HttpClient();
        String url76 = "http://www.google.cn/images/nav_logo3.png";
        GetMethod get77 = new GetMethod( url76 );
        client.executeMethod( get77 );
        byte[] bb77 = get77.getResponseBody();
        java.io.InputStream is77 = get77.getResponseBodyAsStream();
        // 关闭 HttpClient 连接
        get77.releaseConnection();
       
        // 获取图象大小, ok: 2007-12-11 17:29:35
        BufferedImage bi7 = ImageIO.read( is77 );
       
        // another interface such as: read(InputStream input)
        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 )
                ntTool.ntSysOut("enwl_print_report_xls: 446: 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 )
                ntTool.ntSysOut("enwl_print_report_xls: 446: 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( picBeginCol,picBeginRow,  picCellWidth,picCellHeight, bb77 );
       
        // WritableImage wimage = new WritableImage( 2.2, 3.2,  2.3, 2.8, bb77 );
       
        // 图片写到xls中
        ws.addImage( wimage );
        // 写图片: end
       
       
       
        // 保护, 不让修改, protect:
        if( is_rpt_readOnly )
        {
            ws.getSettings().setProtected(true);
            ws.getSettings().setPassword( rpt_readOnly_pwd );
        }

        jxl.write.WritableCell wc = null; // equal to global

   
    // 处理第 i_sheet 张 Sheet表
    Sheet rs = rwb.getSheet( i_sheet );
   
    ......

如果您要问技术的最高境界是什么?......“简单”

刘涛
Mobile: 136,1308,6264
QQ: 30234923   MSN: newtower@hotmail.com
个人网站: http://13613086264.com
深圳市福田区

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值