如何将图像放入excel java的单元格中? [英] How put a image in a cell of excel java?

查看:41
本文介绍了如何将图像放入excel java的单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试使用 Java 将图像放入 Excel 单元格中,但没有取得多大成功,这是我正在使用的代码,但我所做的唯一一件事就是将图像放入 Excel 工作表中,但未放入指定的单元格中

XSSFWorkbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet("My Excel Sample");//FileInputStream 从图像文件中获取输入字节InputStream inputStream = new FileInputStream("C:/images/logo.png");//以字节[]的形式获取InputStream的内容.byte[] bytes = IOUtils.toByteArray(inputStream);//将图片添加到工作簿int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);//关闭输入流inputStream.close();//返回一个处理实例化具体类的对象CreationHelper 助手 = wb.getCreationHelper();//创建顶级绘图族长.绘图绘图 = sheet.createDrawingPatriarch();//创建一个附加到工作表的锚点ClientAnchor 锚点 = helper.createClientAnchor();锚点.setCol1(1);锚点.setRow1(2);//创建图片图片 pict = drawing.createPicture(anchor, pictureIdx);//将图片重置为原始大小pict.resize();//写入Excel文件FileOutputStream fileOut = null;fileOut = new FileOutputStream("C:/data/myFile.xlsx");wb.write(fileOut);fileOut.close();

解决方案

您已经在做的是将带有锚点的图像定位到左上角单元格 B3 (anchor.setCol1(1);anchor.setRow1(2);).然后您已经将图像调整为原始大小.

如果图像适合单元格B3,那么您必须创建一个带有左上角单元格右下角单元格的锚点.并且您不得将图像调整为原始大小.

示例:

import org.apache.poi.xssf.usermodel.*;导入 org.apache.poi.ss.usermodel.*;导入 org.apache.poi.util.IOUtils;导入 java.io.InputStream;导入 java.io.FileInputStream;导入 java.io.FileOutputStream;导入 java.io.IOException;类图像测试{公共静态无效主(字符串 [] args){尝试 {工作簿 wb = new XSSFWorkbook();Sheet sheet = wb.createSheet("My Excel Sample");//FileInputStream 从图像文件中获取输入字节InputStream inputStream = new FileInputStream("/home/axel/Bilder/Wasserlilien.jpg");//以字节[]的形式获取InputStream的内容.byte[] bytes = IOUtils.toByteArray(inputStream);//将图片添加到工作簿int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);//关闭输入流inputStream.close();//返回一个处理实例化具体类的对象CreationHelper 助手 = wb.getCreationHelper();//创建顶级绘图族长.绘图绘图 = sheet.createDrawingPatriarch();//创建一个附加到工作表的锚点ClientAnchor 锚点 = helper.createClientAnchor();//创建一个带有左上角单元格和右下角单元格的锚点锚点.setCol1(1);//B列锚点.setRow1(2);//第3行锚点.setCol2(2);//C列锚点.setRow2(3);//第4行//创建图片图片 pict = drawing.createPicture(anchor, pictureIdx);//将图片重置为原始大小//pict.resize();//不要那样做.让锚调整图像大小!//创建单元格B3单元格单元格 = sheet.createRow(2).createCell(1);//将宽度设置为 n 个字符宽度 = 计数字符 * 256//int widthUnits = 20*256;//sheet.setColumnWidth(1, widthUnits);//以缇为单位将高度设置为 n 个点 = n * 20//短heightUnits = 60*20;//cell.getRow().setHeight(heightUnits);//写入Excel文件FileOutputStream fileOut = null;fileOut = new FileOutputStream("myFile.xlsx");wb.write(fileOut);fileOut.close();} 捕捉(IOException ioex){}}}

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

<预><代码>...//将宽度设置为 n 个字符宽度 = 计数字符 * 256int widthUnits = 20*256;sheet.setColumnWidth(1, widthUnits);//以缇为单位将高度设置为 n 个点 = n * 20短高度单位 = 60*20;cell.getRow().setHeight(heightUnits);...

您可以调整单元格 B3 的大小,从而调整图像大小.

I have tried to put an image into an Excel cell with java but without much success this is the code I was working but the only thing I've done is put the image on excel sheet but not in a cell specified

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();

解决方案

What you are doing already is positioning the image with the anchor to upper left cell B3 (anchor.setCol1(1);anchor.setRow1(2);). Then you already resize the image to it's native size.

If the image shall fit into the cell B3 then you must create an anchor with upper left cell and bottom right cell. And you must not resize the image to it's native size.

Example:

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) {
  }
 }
}

If you remove the comment signs form the program rows

...
   //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);
...

you can resize the cell B3 and so the image resizes.

这篇关于如何将图像放入excel java的单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆