用JXL插入图片到excel的方法
来源:互联网 发布:小黑屋码字软件 编辑:程序博客网 时间:2024/05/18 00:38
jexcelapi是我们操作excel经常用一个工具。http://jexcelapi.sourceforge.net/
我们想在插入图片的时候经常会出现失真,走形的现象。问题主要出在WritableImage(double x, double y, double width, double height, java.io.File image)
这个方法。
这边的width
和height
并不是图片的像数数值,而是占据几个单元格的意思。API如下:
public WritableImage(double x, double y, double width, double height, java.io.File image)
- Parameters:
x
- the column number at which to position the imagey
- the row number at which to position the imagewidth
- the number of columns cells which the image spansheight
- the number of rows which the image spansimage
- the source image file
/** * 插入图片到EXCEL * * @param picSheet sheet * @param pictureFile 图片file对象 * @param cellRow 行数 * @param cellCol 列数 * @throws Exception 例外 */ private void addPictureToExcel(WritableSheet picSheet, File pictureFile, String cellRow, String cellCol) throws Exception { // 开始位置 double picBeginCol = StringUtil.toInteger(cellCol).intValue() - 1; double picBeginRow = StringUtil.toInteger(cellRow).intValue() - 1; // 图片时间的高度,宽度 double picCellWidth = 0.0; double picCellHeight = 0.0; // 读入图片 BufferedImage picImage = ImageIO.read(pictureFile); // 取得图片的像素高度,宽度 int picWidth = picImage.getWidth(); int picHeight = picImage.getHeight(); // 计算图片的实际宽度 int picWidth_t = picWidth * 32; //具体的实验值,原理不清楚。 for (int x = 0; x < 1234; x++) { int bc = (int) Math.floor(picBeginCol + x); // 得到单元格的宽度 int v = picSheet.getColumnView(bc).getSize(); double offset0_t = 0.0; if (0 == x) offset0_t = (picBeginCol - bc) * v; if (0.0 + offset0_t + picWidth_t > v) { // 剩余宽度超过一个单元格的宽度 double ratio_t = 1.0; if (0 == x) { ratio_t = (0.0 + v - offset0_t) / v; } picCellWidth += ratio_t; picWidth_t -= (int) (0.0 + v - offset0_t); } else { //剩余宽度不足一个单元格的宽度 double ratio_r = 0.0; if (v != 0) ratio_r = (0.0 + picWidth_t) / v; picCellWidth += ratio_r; break; } } // 计算图片的实际高度 int picHeight_t = picHeight * 15; for (int x = 0; x < 1234; x++) { int bc = (int) Math.floor(picBeginRow + x); // 得到单元格的高度 int v = picSheet.getRowView(bc).getSize(); double offset0_r = 0.0; if (0 == x) offset0_r = (picBeginRow - bc) * v; if (0.0 + offset0_r + picHeight_t > v) { // 剩余高度超过一个单元格的高度 double ratio_q = 1.0; if (0 == x) ratio_q = (0.0 + v - offset0_r) / v; picCellHeight += ratio_q; picHeight_t -= (int) (0.0 + v - offset0_r); } else {//剩余高度不足一个单元格的高度 double ratio_m = 0.0; if (v != 0) ratio_m = (0.0 + picHeight_t) / v; picCellHeight += ratio_m; break; } } //生成一个图片对象。 WritableImage image = new WritableImage(picBeginCol, picBeginRow, picCellWidth, picCellHeight, pictureFile); // 把图片插入到sheet picSheet.addImage(image); }
但jxl目前只能支持PNG图片。其他类型的图片的时候需要先转换成png。
还有就是支持excel版本有些局限性。对一写excel的艺术字,输入框等等的数据会丢失。相对来说并不是很好的用的工具。
POI虽然也能够插入图片,但有一个问题就是如果excel模板里面已经有了图片,然后再用POI插入图片的话,原来的图片就会被破坏掉。但这种情况jxl却没有问题。
相对来说是各有优点吧。
- 用JXL插入图片到excel的方法
- jxl导出图片到excel
- jxl添加图片到excel
- jxl对Excel的读、写、更新以及插入图片
- jxl对Excel的读、写、更新以及插入图片(含代码实例)
- jxl对Excel的读、写、更新以及插入图片(含代码实例)
- java excel 实例 jxl ,插入图片,指定格式文字
- jxl操作excel 单元格样式,合并单元格,插入图片,超链接
- jxl操作excel 单元格样式,合并单元格,插入图片,超链接
- 用jxl将mysql的数据结构导入到excel中
- 【jxl】将int型和String型插入excel单元格的方法
- 将图片插入到页面的方法
- jxl 读取Excel图片
- 基于jxl的导出Excel通用方法
- jxl操作excel文件的方法
- jxl导出excel的一些方法
- jxl下载excel的下载方法
- java jxl读写Excel 的方法
- java解惑1-表达式谜题
- <input type="text"> and <html:text> 禁止复制
- 关系型数据库管理系统比较
- sqlserver2005+sa密码反复被修改
- android学习笔记14--------------Internet(1)
- 用JXL插入图片到excel的方法
- 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的
- iOS 5上手视频
- centos file-max
- 关于新闻系统的分析
- javascript权威指南第一部分学习笔记
- iPhone 中使用NSLocalizedString实现国际化
- vs2008试用版升级
- 正则表达式 规则