[英]Protect/freeze the position and size of image in xslx sheet using apache POI
我正在使用Apache POI創建一個xlsx文件,其中在左上角插入了徽標。 問題是,每當單元大小增加時,圖像也會擴大。 我如何凍結或保護圖像的大小和位置,以便當像元大小增加時,圖像保持不變(不會擴展)。
以下是插入圖像的代碼。
rowNo = 1;
row = sheet.createRow(rowNo);
//For Adding sample Logo
addImage(1, 1, 3, 4, appPath + "/images/sample_logo.png");
public boolean addImage(int col1, int row1, int col2, int row2, String FileName) {
try {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(FileName);
} catch (FileNotFoundException e) {
return false;
}
ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
int b;
try {
while ((b = fileInputStream.read()) != -1) {
imgBytes.write(b);
}
fileInputStream.close();
} catch (IOException e) {
return false;
}
sheet.addMergedRegion(new CellRangeAddress(row1, row2 - 1, col1, col2 - 1));
// sheet.addMergedRegion(new CellRangeAddress(1, 3, 1, 2));
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, col1, row1, col2, row2);
int index = workBook.addPicture(imgBytes.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG); //index = 4
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, index);
anchor.setAnchorType(2);
} catch (Exception ex) {
LOGGER.error(ex);
}
return true;
}
請幫我。
使用XSSFClientAnchor.setAnchorType設置為ClientAnchor.AnchorType DONT_MOVE_AND_RESIZE
可以在apache poi
中設置“設置Format Picture
- Size and Properties
- Don't move or size with cells
設置。
例:
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.util.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.xssf.usermodel.*;
class CreateExcelPictures {
static String excelPath = "./ExcelWithLogo.xlsx";
static String appPath = "./";
static XSSFWorkbook workbook;
static XSSFSheet sheet;
static void addImage(int col1, int row1, int col2, int row2, String imageFileName, ClientAnchor.AnchorType anchorType) throws Exception {
InputStream imageInputStream = new FileInputStream(imageFileName);
byte[] bytes = IOUtils.toByteArray(imageInputStream);
int pictureId = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
imageInputStream .close();
XSSFClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setAnchorType(anchorType);
anchor.setCol1(col1);
anchor.setRow1(row1);
anchor.setCol2(col2);
anchor.setRow2(row2);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFPicture picture = drawing.createPicture(anchor, pictureId);
}
public static void main(String args[]) throws Exception {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet();
addImage(1, 1, 3, 4, appPath + "sample_logo.png", ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
FileOutputStream fos = new FileOutputStream(excelPath);
workbook.write(fos);
fos.close();
workbook.close();
}
}
之后,錨定在B4:C4
中的圖片sample_logo.png
將不會通過更改單元格大小來移動和/或調整大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.