POI - Excel 单元格中图像的固定宽度
POI - Fixed width of image in Excel cell
我按照 添加了带有 POI 的新图像。
cell.getRow().setHeight(img.getHeightForExcel());
sheet.setColumnWidth(cell.getColumnIndex(), img.getWidthForExcel());
final int picID = workBook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
/* Create the drawing container */
final XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
// ========adding image START
final XSSFClientAnchor myAnchor = new XSSFClientAnchor();
myAnchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
/* Define top left corner, and we can resize picture suitable from there */
myAnchor.setCol1(cell.getColumnIndex()); // Column start
myAnchor.setRow1(rowNum - 1); // Row start
myAnchor.setCol2(cell.getColumnIndex() + 2); // Column end (covers two columns)
myAnchor.setRow2(rowNum); // Row end
/* Invoke createPicture and pass the anchor point and ID */
final XSSFPicture myPicture = drawing.createPicture(myAnchor, picID);
原则上这很有效。我在开头用图像的宽度指定列的宽度。 (还有身高)。
我面临的主要问题是一旦我 运行 autoadjust
喜欢
for (; i < max; i++) {
xlsWorkbook.getSheet().autoSizeColumn(i);
}
我发现前两列的大小也被调整了。但是随着这个图像的宽度也被调整了。由于宽度可能很长(或很窄),我不想影响图像大小。
有没有办法设置图像的宽度而不考虑列宽?
如果您不想在列宽发生变化时调整图像大小,则不能使用该方法。这种方法明确指出图像的大小应与其锚定的单元格大小相同。因此,如果单元格大小发生变化,图片大小也会发生变化。
您可能认为 ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE
应该保护图像不被调整大小。但这只有在 Excel
GUI
中打开时才有价值。 Apache poi
在自动调整列大小时不遵守 ClientAnchor.AnchorType
。可能这会在以后的版本中改变。但在当前版本 apache poi 5.0.0
中没有。
因此,为了满足您的要求,您只设置了一个单元格锚点。即只有anchor.setCol1
和anchor.setRow1
作为图片的左上位置。然后您需要稍后调整图片大小以设置右下角的位置。您必须在设置所有列宽和行高后 调整大小 。因此 after 自动调整列的大小。否则调整列的大小将再次调整图片的大小。
完整示例:
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;
class ImageTest {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream("./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();
//Set anchor type; only valuable in Excel GUI
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
//Create an anchor with upper left cell only
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
//Create a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
//pict.resize(); // don't do this before autosize column
//Create cell in column B to auto sizing that column
Cell cell = sheet.createRow(0).createCell(1);
cell.setCellValue("12345678901234567890");
sheet.autoSizeColumn(1);
//Reset the image to the original size
//pict.resize();
//Reset the image to half the original size
pict.resize(0.5);
//Write the Excel file
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("./myFile.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
我按照
cell.getRow().setHeight(img.getHeightForExcel());
sheet.setColumnWidth(cell.getColumnIndex(), img.getWidthForExcel());
final int picID = workBook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
/* Create the drawing container */
final XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
// ========adding image START
final XSSFClientAnchor myAnchor = new XSSFClientAnchor();
myAnchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
/* Define top left corner, and we can resize picture suitable from there */
myAnchor.setCol1(cell.getColumnIndex()); // Column start
myAnchor.setRow1(rowNum - 1); // Row start
myAnchor.setCol2(cell.getColumnIndex() + 2); // Column end (covers two columns)
myAnchor.setRow2(rowNum); // Row end
/* Invoke createPicture and pass the anchor point and ID */
final XSSFPicture myPicture = drawing.createPicture(myAnchor, picID);
原则上这很有效。我在开头用图像的宽度指定列的宽度。 (还有身高)。
我面临的主要问题是一旦我 运行 autoadjust
喜欢
for (; i < max; i++) {
xlsWorkbook.getSheet().autoSizeColumn(i);
}
我发现前两列的大小也被调整了。但是随着这个图像的宽度也被调整了。由于宽度可能很长(或很窄),我不想影响图像大小。
有没有办法设置图像的宽度而不考虑列宽?
如果您不想在列宽发生变化时调整图像大小,则不能使用该方法。这种方法明确指出图像的大小应与其锚定的单元格大小相同。因此,如果单元格大小发生变化,图片大小也会发生变化。
您可能认为 ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE
应该保护图像不被调整大小。但这只有在 Excel
GUI
中打开时才有价值。 Apache poi
在自动调整列大小时不遵守 ClientAnchor.AnchorType
。可能这会在以后的版本中改变。但在当前版本 apache poi 5.0.0
中没有。
因此,为了满足您的要求,您只设置了一个单元格锚点。即只有anchor.setCol1
和anchor.setRow1
作为图片的左上位置。然后您需要稍后调整图片大小以设置右下角的位置。您必须在设置所有列宽和行高后 调整大小 。因此 after 自动调整列的大小。否则调整列的大小将再次调整图片的大小。
完整示例:
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;
class ImageTest {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream("./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();
//Set anchor type; only valuable in Excel GUI
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
//Create an anchor with upper left cell only
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
//Create a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
//pict.resize(); // don't do this before autosize column
//Create cell in column B to auto sizing that column
Cell cell = sheet.createRow(0).createCell(1);
cell.setCellValue("12345678901234567890");
sheet.autoSizeColumn(1);
//Reset the image to the original size
//pict.resize();
//Reset the image to half the original size
pict.resize(0.5);
//Write the Excel file
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("./myFile.xlsx");
wb.write(fileOut);
fileOut.close();
}
}