POI插入图片的尺寸计算

  1. CreationHelper helper = wb.getCreationHelper();  
  2.         Drawing drawing = sheet.createDrawingPatriarch();  
  3.         ClientAnchor anchor = helper.createClientAnchor();  
  4.         int pictureIdx = wb.addPicture(IOUtils.toByteArray(new FileInputStream("img.png")), Workbook.PICTURE_TYPE_PNG);  
  5.           
  6.         anchor.setRow1(5);  
  7.         anchor.setCol1(3);  
  8.         anchor.setDx1(40);  
  9.         anchor.setDy1(8);  
  10.           
  11.         anchor.setRow2(5);  
  12.         anchor.setCol2(3);  
  13.         anchor.setDx2(741);  
  14.         anchor.setDy2(152);  
  15.           
  16.         drawing.createPicture(anchor, pictureIdx);  

  插入一个301PX * 301 px的图片代码如上。

  经过计算

 

 上面的例子列宽度16000,行高度8000

 

301px ≈ 701dx ≈ 144 dy
dx ≈ (301/701)*(16000/colWidth)*目标像素
dy ≈ (301/144)*(8000/r owHeight )*目标像素

 

Java代码   收藏代码
  1. /** 
  2.      * excel的长度计算复杂有没有文档,也没找到相关蚊帐。 
  3.      * POI EXCEL 默认colWidth 1023, rowHeight 255,单位未知,Anchor里坐标和colWidth, rowHeight有关。 
  4.      * ExcelUtil方便我们将px值变为POI的坐标值或者宽高,但是会有些许误差。 
  5.      * @author lazy_ 
  6.      * 
  7.      */  
  8.     static class ExcelUtil{  
  9.           
  10.         public static  int getAnchorX(int px, int colWidth){  
  11.             return (int) Math.round(( (double)701*16000.0/301)*((double)1/colWidth)*px);  
  12.         }  
  13.           
  14.         public static int getAnchorY(int px, int rowHeight){  
  15.             return (int) Math.round(( (double)144 * 8000/301)*((double)1/rowHeight)*px);  
  16.         }  
  17.           
  18.         public static int getRowHeight(int px){  
  19.             return (int) Math.round(((double)4480/300) * px);  
  20.         }  
  21.           
  22.         public static int getColWidth(int px){  
  23.             return (int) Math.round(((double)10971/300) * px);  
  24.         }  
  25.     }  
Java代码   收藏代码
  1. package com.lazyunderscore;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.util.ArrayList;  
  9. import java.util.LinkedHashMap;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12.   
  13. import org.apache.commons.io.IOUtils;  
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  16. import org.apache.poi.ss.util.CellRangeAddress;  
  17. import org.apache.poi.ss.usermodel.Cell;  
  18. import org.apache.poi.ss.usermodel.CellStyle;  
  19. import org.apache.poi.ss.usermodel.ClientAnchor;  
  20. import org.apache.poi.ss.usermodel.CreationHelper;  
  21. import org.apache.poi.ss.usermodel.Drawing;  
  22. import org.apache.poi.ss.usermodel.Font;  
  23. import org.apache.poi.ss.usermodel.IndexedColors;  
  24. import org.apache.poi.ss.usermodel.Row;  
  25. import org.apache.poi.ss.usermodel.Workbook;  
  26.   
  27. public class ExcelOperation {  
  28.       
  29.       
  30.       
  31.     public static void main(String[] args) throws IOException{  
  32.         final int COL_WIDTH = 13000;  
  33.         final int ROW_HEIGHT = 5000;  
  34.         String name = "name";  
  35.           
  36.         String remark = "remark";  
  37.           
  38.         String period = "2013-01-01" + " ~ " + "2014-01-01";  
  39.           
  40.         String memo = "memo";  
  41.           
  42.         HSSFWorkbook wb = new HSSFWorkbook();  
  43.         HSSFSheet sheet = wb.createSheet("sheet");  
  44.           
  45.         for(int i=0;i<4;i++){  
  46.             sheet.setColumnWidth(i, 5000);  
  47.         }  
  48.         sheet.setColumnWidth(3, COL_WIDTH);  
  49.           
  50.         //header style  
  51.         Font headerFont = wb.createFont();  
  52.         headerFont.setBoldweight((short700);  
  53.         CellStyle headerStyle = wb.createCellStyle();  
  54.         headerStyle.setFont(headerFont);  
  55.         headerStyle.setAlignment(CellStyle.ALIGN_CENTER);  
  56.         headerStyle.setBorderBottom(CellStyle.BORDER_THIN);  
  57.         headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());  
  58.         headerStyle.setBorderLeft(CellStyle.BORDER_THIN);  
  59.         headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());  
  60.         headerStyle.setBorderRight(CellStyle.BORDER_THIN);  
  61.         headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());  
  62.         headerStyle.setBorderTop(CellStyle.BORDER_THIN);  
  63.         headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  
  64.           
  65.         //th style  
  66.         Font thFont = wb.createFont();  
  67.         thFont.setBoldweight((short700);  
  68.         CellStyle thStyle = wb.createCellStyle();  
  69.         thStyle.setFont(headerFont);  
  70.         thStyle.setAlignment(CellStyle.ALIGN_LEFT);  
  71.         thStyle.setBorderBottom(CellStyle.BORDER_THIN);  
  72.         thStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());  
  73.         thStyle.setBorderLeft(CellStyle.BORDER_THIN);  
  74.         thStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());  
  75.         thStyle.setBorderRight(CellStyle.BORDER_THIN);  
  76.         thStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());  
  77.         thStyle.setBorderTop(CellStyle.BORDER_THIN);  
  78.         thStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  
  79.           
  80.         //td style  
  81.         CellStyle tdStyle = wb.createCellStyle();  
  82.         tdStyle.setAlignment(CellStyle.ALIGN_LEFT);  
  83.         tdStyle.setBorderBottom(CellStyle.BORDER_THIN);  
  84.         tdStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());  
  85.         tdStyle.setBorderLeft(CellStyle.BORDER_THIN);  
  86.         tdStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());  
  87.         tdStyle.setBorderRight(CellStyle.BORDER_THIN);  
  88.         tdStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());  
  89.         tdStyle.setBorderTop(CellStyle.BORDER_THIN);  
  90.         tdStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  
  91.           
  92.         LinkedHashMap<String,String> voucherInfoMap = new LinkedHashMap<String,String>();  
  93.         voucherInfoMap.put("优惠券名称", name);  
  94.         voucherInfoMap.put("优惠券描述", remark);  
  95.         voucherInfoMap.put("有效期 ", period);  
  96.         voucherInfoMap.put("使用说明", memo);  
  97.           
  98.         //1.头部优惠券信息  
  99.         int rowCount = 0;  
  100.         for(Map.Entry<String, String> entry: voucherInfoMap.entrySet()){  
  101.             String key = entry.getKey();  
  102.             String value = entry.getValue();  
  103.               
  104.             Row row = sheet.createRow(rowCount);  
  105.              
  106.             Cell cell0 = row.createCell(0);  
  107.             cell0.setCellValue(key);  
  108.             cell0.setCellStyle(headerStyle);  
  109.               
  110.             Cell cell1 = row.createCell(1);  
  111.             cell1.setCellValue(value);  
  112.             cell1.setCellStyle(headerStyle);  
  113.             sheet.addMergedRegion(new CellRangeAddress(rowCount,rowCount,1,3));  
  114.               
  115.             row.createCell(2).setCellStyle(headerStyle);  
  116.             row.createCell(3).setCellStyle(headerStyle);  
  117.               
  118.             rowCount++;  
  119.         }  
  120.         //2.列表头部  
  121.         List<String> thNames =new ArrayList<String>();  
  122.         thNames.add("序号");  
  123.         thNames.add("门店");  
  124.         thNames.add("门店名称");  
  125.         thNames.add("二维码");  
  126.           
  127.         Row row = sheet.createRow(rowCount);  
  128.         int thCellCount = 0;   
  129.         for(String thName : thNames){  
  130.             Cell cell = row.createCell(thCellCount);  
  131.             cell.setCellValue(thName);  
  132.             cell.setCellStyle(thStyle);  
  133.             thCellCount++;  
  134.         }  
  135.         rowCount++;  
  136.           
  137.         //3.列表内容  
  138.         row = sheet.createRow(rowCount);  
  139.           
  140.         row.setHeight((short) ROW_HEIGHT);  
  141.          
  142.           
  143.         CreationHelper helper = wb.getCreationHelper();  
  144.         Drawing drawing = sheet.createDrawingPatriarch();  
  145.         ClientAnchor anchor = helper.createClientAnchor();  
  146.         int pictureIdx = wb.addPicture(IOUtils.toByteArray(new FileInputStream("img.png")), Workbook.PICTURE_TYPE_PNG);//一个300*300的图片  
  147.           
  148.         anchor.setRow1(5);  
  149.         anchor.setCol1(3);  
  150.         anchor.setDx1(getAnchorX(5,COL_WIDTH));  
  151.         anchor.setDy1(getAnchorY(5,ROW_HEIGHT));  
  152.           
  153.         anchor.setRow2(5);  
  154.         anchor.setCol2(3);  
  155.         anchor.setDx2(getAnchorX(305,COL_WIDTH));  
  156.         anchor.setDy2(getAnchorY(305,ROW_HEIGHT));  
  157.           
  158.         drawing.createPicture(anchor, pictureIdx);  
  159.           
  160.         // Write the output to a file  
  161.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  162.         wb.write(bos);  
  163.         byte[] bytes = bos.toByteArray();  
  164.         IOUtils.copy(new ByteArrayInputStream(bytes), new FileOutputStream("workbook.xls"));  
  165.           
  166.     }  
  167.     private static int getAnchorX(int px, int colWidth){  
  168.         return (int) Math.round(( (double)701*16000.0/301)*((double)1/colWidth)*px);  
  169.     }  
  170.       
  171.     private static int getAnchorY(int px, int rowHeight){  
  172.         return (int) Math.round(( (double)144 * 8000/301)*((double)1/rowHeight)*px);  
  173.     }  
  174.       
  175.     private static int getRowHeight(int px){  
  176.         return (int) Math.round(((double)4480/300) * px);  
  177.     }  
  178.       
  179.     private static int getColWidth(int px){  
  180.         return (int) Math.round(((double)10971/300) * px);  
  181.     }  
  182. }  

   S:完整一个POI EXCEL 测试例子如下:

Xml代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.lazyunderscore</groupId>  
  5.   <artifactId>Word2HtmlExample</artifactId>  
  6.   <packaging>jar</packaging>  
  7.   <version>1.0-SNAPSHOT</version>  
  8.   <name>Word2HtmlExample</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11.       
  12. <!--     <dependency> -->  
  13. <!--             <groupId>org.apache.poi</groupId> -->  
  14. <!--             <artifactId>poi-scratchpad</artifactId> -->  
  15. <!--             <version>3.9</version> -->  
  16. <!--     </dependency> -->  
  17.     <dependency>  
  18.             <groupId>org.apache.poi</groupId>  
  19.             <artifactId>poi</artifactId>  
  20.             <version>3.9</version>  
  21.         </dependency>  
  22.             <dependency>  
  23.             <groupId>commons-io</groupId>  
  24.             <artifactId>commons-io</artifactId>  
  25.             <version>1.4</version>  
  26.   
  27.         </dependency>  
  28.       
  29.     <dependency>  
  30.       <groupId>junit</groupId>  
  31.       <artifactId>junit</artifactId>  
  32.       <version>3.8.1</version>  
  33.       <scope>test</scope>  
  34.     </dependency>  
  35.   </dependencies>  
  36. </project>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值