XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet 1");
try {
XSSFCellStyle headerStyle = workbook.createCellStyle();
//Font 설정
XSSFFont headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
headerFont.setFontHeightInPoints((short) 20);
headerStyle.setFont(headerFont);
//이탤릭체 설정
XSSFFont italicFont = workbook.createFont();
italicFont.setItalic(true);
italicFont.setBold(false);
XSSFCellStyle tableLeftNoColorItaclic = workbook.createCellStyle();
tableLeftNoColorItaclic.setFont(italicFont); // 이탤릭체 설정
tableLeftNoColorItaclic.setWrapText(true);//자동 줄 바꿈
tableLeftNoColorItaclic.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFCell tempTgCell = tempTgRow.createCell(0);
tempTgCell.setCellStyle(tableLeftNoColorItaclic);
// Background color - grey 설정
byte[] titleGray = new byte[]{(byte) 153, (byte) 153, (byte) 153};
XSSFColor titleGrayColor = new XSSFColor(titleGray, new DefaultIndexedColorMap());
headerStyle.setFillForegroundColor(titleGrayColor);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//merge
CellRangeAddress mergedRegion = new CellRangeAddress(0, 0, 0, 7);
sheet.addMergedRegion(mergedRegion);
XSSFRow row = sheet.createRow(0);
row.setHeight((short) 1500);
//이미지 삽입
InputStream logoStream = new FileInputStream("src/main/resources/static/_img/common/logo.png");
byte[] logoBytes = IOUtils.toByteArray(logoStream);
int pidx = workbook.addPicture(logoBytes, XSSFWorkbook.PICTURE_TYPE_PNG);
logoStream.close();
XSSFCreationHelper logohelper = workbook.getCreationHelper();
XSSFDrawing logodrawing = sheet.createDrawingPatriarch();
XSSFClientAnchor logoanchor = logohelper.createClientAnchor();
// 이미지를 출력할 CELL 위치 선정
logoanchor.setCol1(7);
logoanchor.setRow1(0);
logoanchor.setCol2(9);
logoanchor.setRow2(1);
// 이미지 그리기
XSSFPicture logopict = logodrawing.createPicture(logoanchor, pidx);
//또 다른 방법
// 이미지 크기 조정 (픽셀 단위)
int widthInPixels = 80; // 이미지 너비 (픽셀)
int heightInPixels = 80; // 이미지 높이 (픽셀)
// 이미지를 출력할 CELL 위치 선정
anchor.setCol1(signFirstCol);
anchor.setCol2(signLastCol);
anchor.setRow1(signImgRowCount);
anchor.setRow2(signImgRowCount);
// 픽셀을 EMU로 변환하여 오프셋 설정
anchor.setDx1(30 * Units.EMU_PER_PIXEL); // 시작 x-offset (셀 왼쪽)
anchor.setDy1(20 * Units.EMU_PER_PIXEL); // 시작 y-offset (셀 위쪽)
anchor.setDx2(widthInPixels * Units.EMU_PER_PIXEL); // 끝 x-offset
anchor.setDy2(heightInPixels * Units.EMU_PER_PIXEL); // 끝 y-offset
// 이미지 그리기
XSSFPicture pict = drawing.createPicture(anchor, pictureIdx);
File resultFile = new File(filePath);
resultFile.createNewFile();
FileOutputStream fos = null;
fos = new FileOutputStream(resultFile, false);
workbook.write(fos);
//workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
log.info(e.toString());
}
댓글