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

巴克·麦地那

我试图将图像放入带有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();
阿克塞尔·里希特(Axel Richter)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将图像放在整个单元格中

来自分类Dev

如何将div放在表格单元格的底部?

来自分类Dev

如何在iText Java中更改单元格颜色

来自分类Dev

Java-如何使JTable中的单元格可编辑?

来自分类Dev

如何从uitableview单元格中删除单元格

来自分类Dev

如何将图像放在引导程序中的图像之上;

来自分类Dev

如何将单元格中的文本扩展到单元格之外?

来自分类Dev

如何将图像和文本放入iText Android表的1个单元格中?

来自分类Dev

如何将单元格中的结果(显示)转换为Excel中的文本

来自分类Dev

在单元格中展开图像,MATLAB

来自分类Dev

在表格单元格中显示图像

来自分类Dev

表格视图单元格中的图像

来自分类Dev

如何将图像放在特定的JPanel中?

来自分类Dev

如何将文本放在堆栈中的彼此图像上方?

来自分类Dev

如何将图像放在自定义路径中

来自分类Dev

如何将图像背景放在变量中?

来自分类Dev

如何将图像放在 ListView 中 TextView 行的底部

来自分类Dev

如何将日期名称输入Excel中的单元格?

来自分类Dev

如何将.jpeg写入Excel中的特定单元格

来自分类Dev

如何将单元格中的数据拆分为 Excel 上的现有列?

来自分类Dev

如何将单元格中的int值传递给excel公式

来自分类Dev

如何将记录/单元格链接到 Excel 中的图表?全系列

来自分类Dev

如何将附加值复制到 Excel 工作表中的单元格

来自分类Dev

减少Excel中的单元格数量

来自分类Dev

公式中的Excel单元格地址

来自分类Dev

VBA Excel中的单元格范围

来自分类Dev

在Excel中累积单元格值

来自分类Dev

减少Excel中的单元格数量

来自分类Dev

javascript中的excel单元格引用

Related 相关文章

热门标签

归档