栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将图像放在Excel Java单元格中?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将图像放在Excel Java单元格中?

您已经在做的是将锚定图像定位到左上方的单元格

B3
anchor.setCol1(1);anchor.setRow1(2);
)。然后,您已经将图像调整为原始大小。

如果图像适合该单元格,

B3
则必须使用左上单元格 右下单元格创建锚点。而且,请勿将图像调整为原始大小。

例:

import org.apache.poi.xssf.usermodel.*;import org.apache.poi.ss.usermodel.*;import org.apache.poi.util.IOUtils;import java.io.InputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;class ImageTest { public static void main(String[] args) {  try {   Workbook wb = new XSSFWorkbook();   Sheet sheet = wb.createSheet("My Sample Excel");   //FileInputStream obtains input bytes from the image file   InputStream inputStream = new FileInputStream("/home/axel/Bilder/Wasserlilien.jpg");   //Get the contents of an InputStream as a byte[].   byte[] bytes = IOUtils.toByteArray(inputStream);   //Adds a picture to the workbook   int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);   //close the input stream   inputStream.close();   //Returns an object that handles instantiating concrete classes   CreationHelper helper = wb.getCreationHelper();   //Creates the top-level drawing patriarch.   Drawing drawing = sheet.createDrawingPatriarch();   //Create an anchor that is attached to the worksheet   ClientAnchor anchor = helper.createClientAnchor();   //create an anchor with upper left cell _and_ bottom right cell   anchor.setCol1(1); //Column B   anchor.setRow1(2); //Row 3   anchor.setCol2(2); //Column C   anchor.setRow2(3); //Row 4   //Creates a picture   Picture pict = drawing.createPicture(anchor, pictureIdx);   //Reset the image to the original size   //pict.resize(); //don't do that. Let the anchor resize the image!   //Create the Cell B3   Cell cell = sheet.createRow(2).createCell(1);   //set width to n character widths = count characters * 256   //int widthUnits = 20*256;   //sheet.setColumnWidth(1, widthUnits);   //set height to n points in twips = n * 20   //short heightUnits = 60*20;   //cell.getRow().setHeight(heightUnits);   //Write the Excel file   FileOutputStream fileOut = null;   fileOut = new FileOutputStream("myFile.xlsx");   wb.write(fileOut);   fileOut.close();  } catch (IOException ioex) {  } }}

如果从程序行中删除注释符号

...   //set width to n character widths = count characters * 256   int widthUnits = 20*256;   sheet.setColumnWidth(1, widthUnits);   //set height to n points in twips = n * 20   short heightUnits = 60*20;   cell.getRow().setHeight(heightUnits);...

您可以调整单元格的大小

B3
,从而调整图像的大小。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号