2016-07-25 45 views
2

情况是,我合并了第一行的所有五个单元格,并将图像插入第一行的第一个单元格。我的要求是使图像水平居中的第一行。使用apache poi在合并单元格中水平居中图像

我试过cellStyle.setAlignment(CellStyle.ALIGN_CENTER);,但它以文字为中心而不是图像。

任何人都可以帮忙吗?

注意:所有的单元格都有不同的宽度。

回答

3

将图片放置在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); 

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

+0

这太好了。谢谢。有没有一种方法来中心**调整大小的**图像? –

+1

@尤慕李:查看我的补充内容。 –

+0

谢谢先生。这在XSSFWorkbook中有效。但是,当使用XSSFWorkbook你知道如何在单元格上设置粗体文本吗?[相关](http://stackoverflow.com/questions/38583593/xssfworkbook-bold-font-not-working) –