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
我试图用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();
1 个解决方案
#1
9
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.
您正在做的是将带有锚点的图像定位到左上角的单元格B3(anchor.setCol1(1); anchor.setRow1(2);)。然后,您已经将图像调整为原始大小。
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.
如果图像适合单元格B3,则必须创建一个左上角单元格和右下角单元格的锚点。并且您不能将图像调整为原始大小。
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.
您可以调整单元格B3的大小,以便调整图像大小。
#1
9
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.
您正在做的是将带有锚点的图像定位到左上角的单元格B3(anchor.setCol1(1); anchor.setRow1(2);)。然后,您已经将图像调整为原始大小。
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.
如果图像适合单元格B3,则必须创建一个左上角单元格和右下角单元格的锚点。并且您不能将图像调整为原始大小。
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.
您可以调整单元格B3的大小,以便调整图像大小。