==jxl导入图片问题。周五了呵呵==

amdgaming 2009-11-06 02:57:45
下面是网上 拷贝的一个程序 。。。
现在是无法获取图片的真实大小,只能获取到他的 像素大小,但是加入excel中
需要知道 真是 尺寸,才能在excel加入原图一样大小的图片大家有遇到这个问题吗?



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();
}

}
}

...全文
737 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianyashashou502 2012-09-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
jxl是个什么东东,poi好像更强大吧
[/Quote]POI不能操作图片,JXL只能操作图片跟STRING
yzk116688 2010-01-22
  • 打赏
  • 举报
回复
嘿嘿,没弄过,只操作过文本~~(*^__^*) 嘻嘻……~
xiebin19880914 2010-01-21
  • 打赏
  • 举报
回复
能用吗?
那牛 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 swandragon 的回复:]
可以直接写入png格式的图片
WritableImage wi = new WritableImage(10, 15, 12, 17, new File("d:/01.png"));
sheet.addImage(wi);
[/Quote]

 WritableSheet sheet = wwb.createSheet("pic", 0);
BufferedImage bi7 = ImageIO.read(new File(file));
int width = bi7.getWidth();
int height = bi7.getHeight();
WritableImage wimage = new WritableImage( 5,5, width,height,new File(file)); sheet.addImage(wimage);


这样子操作可以获取图片宽高,可是Excel中没有图片,为什么呢?
zhouxingyu896 2009-11-10
  • 打赏
  • 举报
回复
学习

学习
amdgaming 2009-11-10
  • 打赏
  • 举报
回复
已经搞定了。。测试 那个固定系数 除一下 即可。。
junkor 2009-11-10
  • 打赏
  • 举报
回复
帮忙顶一下
mystery1004 2009-11-10
  • 打赏
  • 举报
回复
看看
smallbear923 2009-11-10
  • 打赏
  • 举报
回复
帮顶~
那牛 2009-11-10
  • 打赏
  • 举报
回复
来看看 过几天估计会用到这个了
amdgaming 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 zl3450341 的回复:]
http://www.java2000.net/p10743
去看看java jxl 插入图片 png
[/Quote]
这个是没有问题了。。。呵呵
zl3450341 2009-11-09
  • 打赏
  • 举报
回复
http://www.java2000.net/p10743
去看看 java jxl 插入图片 png
zl3450341 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 amdgaming 的回复:]
没人 再看看。。。
[/Quote]

不会

帮顶下
xfxuelan1986 2009-11-09
  • 打赏
  • 举报
回复
学习
97095639 2009-11-07
  • 打赏
  • 举报
回复
mark
amdgaming 2009-11-07
  • 打赏
  • 举报
回复
没人 再看看。。。
goodmrning 2009-11-07
  • 打赏
  • 举报
回复
mark!
btob 2009-11-07
  • 打赏
  • 举报
回复
顶起,学习
config_man 2009-11-06
  • 打赏
  • 举报
回复
路过过...
zealVampire 2009-11-06
  • 打赏
  • 举报
回复
处理EXCEL显示经常又跑到处理图形之类的去了呵, 挺麻烦的, 我想MS Excel也是类似做了那么多的事情吧。
加载更多回复(12)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧