状況は、最初の行の5つのセルをすべてマージし、最初の行の最初のセルにイメージを挿入しました。私の要件は、イメージを最初の行の中央に水平にすることです。apache poiを使用して、マージされたセルのイメージを水平に中央に配置します
私はcellStyle.setAlignment(CellStyle.ALIGN_CENTER);
を試しましたが、イメージではなくテキストを中心にしています。
誰でも手助けできますか?
注:すべてのセルの幅が異なります。
状況は、最初の行の5つのセルをすべてマージし、最初の行の最初のセルにイメージを挿入しました。私の要件は、イメージを最初の行の中央に水平にすることです。apache poiを使用して、マージされたセルのイメージを水平に中央に配置します
私はcellStyle.setAlignment(CellStyle.ALIGN_CENTER);
を試しましたが、イメージではなくテキストを中心にしています。
誰でも手助けできますか?
注:すべてのセルの幅が異なります。
画像をExcelシートに配置するのは、画像が2つのセルに固定されているため、難しいことです。左上のアンカーセルに加えて、これに対するデルタ-xおよびデルタ-yは、ピクチャの左上端の位置を決定する。右下のアンカーセルにdelta-xとdelta-yを加えたものがサイズを決定します。
セルがマージされているかどうかは、このプロセスでは重要ではありません。
したがって、水平方向にセンタリングするには、左上のアンカーセルにdelta-xを加えたものを計算する必要があります。幸運なことに、右下のアンカーセルにデルタ-xとデルタ-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);
注:最初に元のサイズにサイズ変更してください。右下のアンカー位置を元のサイズで決定します。次に、サイズを変更します。だから、スケールされたサイズで右下のアンカー位置を取得します。
これは素晴らしいです。ありがとうございました。 **サイズ変更された**イメージを中央に配置する方法はありますか? –
@尤慕李:私の追加を参照してください。 –
ありがとうございます。これはXSSFWorkbookで機能します。しかし、XSSFWorkbookを使用する場合、太字のテキストをセルに設定する方法は分かりますか?[関連](関連リンク)(http://stackoverflow.com/questions/38583593/xssfworkbook-bold-font-not-working) –