CreationHelper helper = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); int pictureIdx = wb.addPicture(IOUtils.toByteArray(new FileInputStream("img.png")), Workbook.PICTURE_TYPE_PNG); anchor.setRow1(5); anchor.setCol1(3); anchor.setDx1(40); anchor.setDy1(8); anchor.setRow2(5); anchor.setCol2(3); anchor.setDx2(741); anchor.setDy2(152); drawing.createPicture(anchor, pictureIdx);
插入一个301PX * 301 px的图片代码如上。
经过计算
上面的例子列宽度16000,行高度8000
301px ≈ 701dx ≈ 144 dy
dx ≈ (301/701)*(16000/colWidth)*目标像素
dy ≈ (301/144)*(8000/rowHeight)*目标像素
dx ≈ (301/701)*(16000/colWidth)*目标像素
dy ≈ (301/144)*(8000/rowHeight)*目标像素
/** * excel的长度计算复杂有没有文档,也没找到相关蚊帐。 * POI EXCEL 默认colWidth 1023, rowHeight 255,单位未知,Anchor里坐标和colWidth, rowHeight有关。 * ExcelUtil方便我们将px值变为POI的坐标值或者宽高,但是会有些许误差。 * @author lazy_ * */ static class ExcelUtil{ public static int getAnchorX(int px, int colWidth){ return (int) Math.round(( (double)701*16000.0/301)*((double)1/colWidth)*px); } public static int getAnchorY(int px, int rowHeight){ return (int) Math.round(( (double)144 * 8000/301)*((double)1/rowHeight)*px); } public static int getRowHeight(int px){ return (int) Math.round(((double)4480/300) * px); } public static int getColWidth(int px){ return (int) Math.round(((double)10971/300) * px); } }
P
package com.lazyunderscore; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; public class ExcelOperation { public static void main(String[] args) throws IOException{ final int COL_WIDTH = 13000; final int ROW_HEIGHT = 5000; String name = "name"; String remark = "remark"; String period = "2013-01-01" + " ~ " + "2014-01-01"; String memo = "memo"; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet"); for(int i=0;i<4;i++){ sheet.setColumnWidth(i, 5000); } sheet.setColumnWidth(3, COL_WIDTH); //header style Font headerFont = wb.createFont(); headerFont.setBoldweight((short) 700); CellStyle headerStyle = wb.createCellStyle(); headerStyle.setFont(headerFont); headerStyle.setAlignment(CellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(CellStyle.BORDER_THIN); headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderLeft(CellStyle.BORDER_THIN); headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderRight(CellStyle.BORDER_THIN); headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderTop(CellStyle.BORDER_THIN); headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //th style Font thFont = wb.createFont(); thFont.setBoldweight((short) 700); CellStyle thStyle = wb.createCellStyle(); thStyle.setFont(headerFont); thStyle.setAlignment(CellStyle.ALIGN_LEFT); thStyle.setBorderBottom(CellStyle.BORDER_THIN); thStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); thStyle.setBorderLeft(CellStyle.BORDER_THIN); thStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); thStyle.setBorderRight(CellStyle.BORDER_THIN); thStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); thStyle.setBorderTop(CellStyle.BORDER_THIN); thStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //td style CellStyle tdStyle = wb.createCellStyle(); tdStyle.setAlignment(CellStyle.ALIGN_LEFT); tdStyle.setBorderBottom(CellStyle.BORDER_THIN); tdStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); tdStyle.setBorderLeft(CellStyle.BORDER_THIN); tdStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); tdStyle.setBorderRight(CellStyle.BORDER_THIN); tdStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); tdStyle.setBorderTop(CellStyle.BORDER_THIN); tdStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); LinkedHashMap<String,String> voucherInfoMap = new LinkedHashMap<String,String>(); voucherInfoMap.put("优惠券名称", name); voucherInfoMap.put("优惠券描述", remark); voucherInfoMap.put("有效期 ", period); voucherInfoMap.put("使用说明", memo); //1.头部优惠券信息 int rowCount = 0; for(Map.Entry<String, String> entry: voucherInfoMap.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); Row row = sheet.createRow(rowCount); Cell cell0 = row.createCell(0); cell0.setCellValue(key); cell0.setCellStyle(headerStyle); Cell cell1 = row.createCell(1); cell1.setCellValue(value); cell1.setCellStyle(headerStyle); sheet.addMergedRegion(new CellRangeAddress(rowCount,rowCount,1,3)); row.createCell(2).setCellStyle(headerStyle); row.createCell(3).setCellStyle(headerStyle); rowCount++; } //2.列表头部 List<String> thNames =new ArrayList<String>(); thNames.add("序号"); thNames.add("门店"); thNames.add("门店名称"); thNames.add("二维码"); Row row = sheet.createRow(rowCount); int thCellCount = 0; for(String thName : thNames){ Cell cell = row.createCell(thCellCount); cell.setCellValue(thName); cell.setCellStyle(thStyle); thCellCount++; } rowCount++; //3.列表内容 row = sheet.createRow(rowCount); row.setHeight((short) ROW_HEIGHT); CreationHelper helper = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); int pictureIdx = wb.addPicture(IOUtils.toByteArray(new FileInputStream("img.png")), Workbook.PICTURE_TYPE_PNG);//一个300*300的图片 anchor.setRow1(5); anchor.setCol1(3); anchor.setDx1(getAnchorX(5,COL_WIDTH)); anchor.setDy1(getAnchorY(5,ROW_HEIGHT)); anchor.setRow2(5); anchor.setCol2(3); anchor.setDx2(getAnchorX(305,COL_WIDTH)); anchor.setDy2(getAnchorY(305,ROW_HEIGHT)); drawing.createPicture(anchor, pictureIdx); // Write the output to a file ByteArrayOutputStream bos = new ByteArrayOutputStream(); wb.write(bos); byte[] bytes = bos.toByteArray(); IOUtils.copy(new ByteArrayInputStream(bytes), new FileOutputStream("workbook.xls")); } private static int getAnchorX(int px, int colWidth){ return (int) Math.round(( (double)701*16000.0/301)*((double)1/colWidth)*px); } private static int getAnchorY(int px, int rowHeight){ return (int) Math.round(( (double)144 * 8000/301)*((double)1/rowHeight)*px); } private static int getRowHeight(int px){ return (int) Math.round(((double)4480/300) * px); } private static int getColWidth(int px){ return (int) Math.round(((double)10971/300) * px); } }
S:完整一个POI EXCEL 测试例子如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lazyunderscore</groupId> <artifactId>Word2HtmlExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Word2HtmlExample</name> <url>http://maven.apache.org</url> <dependencies> <!-- <dependency> --> <!-- <groupId>org.apache.poi</groupId> --> <!-- <artifactId>poi-scratchpad</artifactId> --> <!-- <version>3.9</version> --> <!-- </dependency> --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
相关推荐
POI向excel中插入图片
通过POI生成插入图片的PPT。 包括图片的自动生成。
POI对Word的操作,特别是针对生成报告模板时,向word中写入文本和图片。
完整的Java project demo,包含所需的所有jar包,示例文件,使用说明等
POI导出Excel工具类,自动设置标题 列名 文件名,可插入图片,合并单元格
解决POI3.10在添加图片的时候自身有一个BUG。纠结了很久都没解决,今天终于弄好了,文件包中附带了修改好的jar包bsj-poi-ooxml-3.10-FINAL-20140208.jar。主要问题是出在:org.apache.poi.xwpf.usermodel.XWPFRun....
完美市县了DOCX格式的模板文字替换与插入浮于文字上方的图片。
通过POI将图片插入word文档中,写文字就简单了自己摸索
利用POI技术将图片插入到excel中,希望对大家有用,直接运行ExportExcel类,即可看到效果!!!
在网上看到很少POI对word文档的操作,所以自己写一些供参考,不懂的可以留言,我看到就及时回复
poi获取图片位置,输出到指定位置
主要介绍了java poi设置生成的word的图片为上下型环绕以及其位置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
使用POI 对 图片插入 目录插入 不用模板文件。 绝对好使的。
poi导入word,代码简单包含pom文件
java使用poi操作.doc word模板替换,循环插入表格
poi读取excel不能直接获取行中的图片,可以通过图片中心所在行获取所在行图片
使用poi替换word中的图片,无需加书签,doc/docx均可,亲测。
1、poi版本3.13 2、可进行表格替换。复制、图片导出 3、可直接运行
Java Poi流根据Word模板插入相应的文本、表格和图片,并生成新的Word报告。文档提供相应的jar包和Word模板文件,表格支持动态添加数据,插入图片支持多种格式!
利用java往excel中插入图片 POI利用java往excel中插入图片POI形式