当前位置: 首页 > 面试题库 >

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

席言
2023-03-14
问题内容

我试图将图像放入带有Java的Excel单元格中,但没有成功,这是我正在使用的代码,但是我唯一要做的就是将图像放入excel工作表中,而不放在指定的单元格中

XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream("C:/images/logo.png");
//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();
anchor.setCol1(1);
anchor.setRow1(2);
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
pict.resize();
//Write the Excel file
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("C:/data/myFile.xlsx");
wb.write(fileOut);
fileOut.close();

问题答案:

您已经在做的是将锚定图像定位到左上方的单元格B3anchor.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,从而调整图像的大小。



 类似资料:
  • Word显示图像有问题。我真的不知道问题出在哪里

  • 我有一个. xlsx格式的Excel文件。我通过合并单元格以形成各种列来存储数据。我正在通过JavaWeb应用程序读取Excel文件并将其数据保存到数据库(MySQL)。但是当我从合并的单元格中读取时,我会得到空值以及存储在列和标题中的内容。我使用Apache POI。我的代码是: 我在网上搜索了答案,但没有找到任何相关的。

  • 问题内容: 我开发了Eclipse RCP应用程序,并且遇到了问题。我们数据库中有一些布尔格式的数据,用户希望使用来查看该字段。 我试图使用来实现它作为 表编辑器 ,但它的工作速度太慢:( 我尝试使用2张图片-选中和未选中的复选框,都可以,但是我无法将它们居中对齐,它们会自动向左对齐。 我什至找到了如何捕获和事件以及如何通过更改字段手动处理它们,但是我遇到了一个问题- 我现在无法测量或绘制哪个列,

  • 我创建了一个可以将图像导入标签的Swing应用程序,但我想做的是将导入的图像放入一个内部框架中,该框架将在图像删除后创建。 将图像放入Java应用程序 创建一个内部框架 使图像出现在内部框架中,而不是标签出现在JFrame中

  • 问题内容: 该单元格仅包含一个复选框。由于表标题行中的文本,所以它很宽。如何将复选框居中(在HTML中包含内联CSS?(我知道)) 我试过了 但这没用。我究竟做错了什么? 更新:这是一个测试页。有人可以更正它吗(在HTML中使用CSS内联),以使复选框位于其列的中心? 我已经接受@Hristo的回答-这就是内联格式… 问题答案: 更新 HTML CSS 我希望这有帮助。