第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 apache POI 保護/凍結(jié) xslx 工作表中圖像的位置和大小

使用 apache POI 保護/凍結(jié) xslx 工作表中圖像的位置和大小

德瑪西亞99 2023-02-23 17:38:51
我正在使用 Apache POI 創(chuàng)建一個 xlsx 文件,其中在左上角插入了一個徽標。問題是,當單元格的大小增加時,圖像也會擴展。如何凍結(jié)或保護圖像的大小和位置,以便當單元格大小增加時,圖像保持不變(不擴展)。下面是插入圖片的代碼。rowNo = 1;row = sheet.createRow(rowNo);//For Adding sample LogoaddImage(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;    }
查看完整描述

1 回答

?
富國滬深

TA貢獻1790條經(jīng)驗 獲得超9個贊

設(shè)置Format Picture-?Size and Properties-Don't move or size with cellsapache poi使用XSSFClientAnchor.setAnchorType設(shè)置為ClientAnchor.AnchorType?DONT_MOVE_AND_RESIZE。

例子:


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


?}

}

之后sample_logo.png錨定的圖片B4:C4將不會通過更改單元格大小來移動和/或調(diào)整大小。


查看完整回答
反對 回復(fù) 2023-02-23
  • 1 回答
  • 0 關(guān)注
  • 287 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號