2016-07-25 49 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) –