1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java poi居中_使用apache poi在合并单元格中水平居中图像

java poi居中_使用apache poi在合并单元格中水平居中图像

时间:2024-03-26 11:04:38

相关推荐

java poi居中_使用apache poi在合并单元格中水平居中图像

将图片放置在Excel表格中是一件棘手的事情,因为图片被锚定在两个单元格上。左上角的锚点单元加上delta-x和delta-y来确定图片左上角的位置。右下角锚点单元格加上delta-x和delta-y来确定大小。

单元格是否合并对此过程并不重要。

因此,为了水平居中,我们需要计算哪一个是左上角的锚点单元加上delta-x。幸运的是,右下角锚点单元格加上delta-x和delta-y,可以通过在设置左上角锚点单元格之后将图像调整为原始大小来自动确定。当然只有当照片应该以其原始尺寸出现时。

实施例与评论:

import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.*;

import org.apache.poi.util.IOUtils;

import org.apache.poi.util.Units;

import java.io.InputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

class CenterImageTest {

public static void main(String[] args) {

try {

Workbook wb = new XSSFWorkbook();

Sheet sheet = wb.createSheet("Sheet1");

//create the cells A1:F1 with different widths

Cell cell = null;

Row row = sheet.createRow(0);

for (int col = 0; col < 6; col++) {

cell = row.createCell(col);

sheet.setColumnWidth(col, (col+1)*5*256);

}

//merge A1:F1

sheet.addMergedRegion(new CellRangeAddress(0,0,0,5));

//load the picture

InputStream inputStream = new FileInputStream("/home/axel/Bilder/Unbenannt.png");

byte[] bytes = IOUtils.toByteArray(inputStream);

int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);

inputStream.close();

//create an anchor with upper left cell A1

CreationHelper helper = wb.getCreationHelper();

ClientAnchor anchor = helper.createClientAnchor();

anchor.setCol1(0); //Column A

anchor.setRow1(0); //Row 1

//create a picture anchored to A1

Drawing drawing = sheet.createDrawingPatriarch();

Picture pict = drawing.createPicture(anchor, pictureIdx);

//resize the pictutre to original size

pict.resize();

//get the picture width

int pictWidthPx = pict.getImageDimension().width;

System.out.println(pictWidthPx);

//get the cell width A1:F1

float cellWidthPx = 0f;

for (int col = 0; col < 6; col++) {

cellWidthPx += sheet.getColumnWidthInPixels(col);

}

System.out.println(cellWidthPx);

//calculate the center position

int centerPosPx = Math.round(cellWidthPx/2f - (float)pictWidthPx/2f);

System.out.println(centerPosPx);

//determine the new first anchor column dependent of the center position

//and the remaining pixels as Dx

int anchorCol1 = 0;

for (int col = 0; col < 6; col++) {

if (Math.round(sheet.getColumnWidthInPixels(col)) < centerPosPx) {

centerPosPx -= Math.round(sheet.getColumnWidthInPixels(col));

anchorCol1 = col + 1;

} else {

break;

}

}

System.out.println(anchorCol1);

System.out.println(centerPosPx);

//set the new upper left anchor position

anchor.setCol1(anchorCol1);

//set the remaining pixels up to the center position as Dx in unit EMU

anchor.setDx1(centerPosPx * Units.EMU_PER_PIXEL);

//resize the pictutre to original size again

//this will determine the new bottom rigth anchor position

pict.resize();

FileOutputStream fileOut = new FileOutputStream("CenterImageTest.xlsx");

wb.write(fileOut);

fileOut.close();

} catch (IOException ioex) {

}

}

}

对于缩放的图像具有的比例因子:

double scaleFactor = 0.25d;

然后:

//calculate the center position

int centerPosPx = Math.round(cellWidthPx/2f - (float)pictWidthPx/2f*(float)scaleFactor);

和:

//resize the pictutre to original size again

//this will determine the new bottom rigth anchor position with original size

pict.resize();

//resize the pictutre to scaled size

//this will determine the new bottom rigth anchor position with scaled size

pict.resize(scaleFactor);

注意:首先调整为原始大小。因此,确定原始尺寸的右下角锚点位置。然后调整缩放比例。因此,请按比例缩小大小以获得右下角的锚位置。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。