JAVA

java / POI / excel

girin_dev 2022. 2. 3. 21:09
728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿฅ• ์ฝ”๋“œ๋จผ์ € ๊ธฐ๋ก.

package a.test;

import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.io.*;

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

import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
 
public class PoiExcelImageTest {

	public static void main(String[] args){
		Workbook wb = new XSSFWorkbook();
		CellStyle styleVertAlingTop = wb.createCellStyle();
		styleVertAlingTop.setVerticalAlignment(VerticalAlignment.TOP);

		Sheet sheet = wb.createSheet();
		sheet.setColumnWidth(0, 15 * 256); //15 default characters width
		sheet.setColumnWidth(1, 30 * 256); //30 default characters width

		Row row = sheet.createRow(2);
		row.setHeight((short)(100 * 20)); //100pt height * 20 = twips (twentieth of an inch point)

		Cell cell = row.createCell(0);
		cell.setCellValue("Task 1");

		cell = row.createCell(1);
		cell.setCellValue("Replace Kemplon-Pipe");
		cell.setCellStyle(styleVertAlingTop);

		InputStream inputstream;
		try {
			System.out.println("testestsetsetset");
			inputstream = new FileInputStream("D:/test1123.png");
			byte[] bytes = IOUtils.toByteArray(inputstream);
			int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
			inputstream.close();
	
			int left = 20; // 20px
			int top = 20; // 20pt
			int width = Math.round(sheet.getColumnWidthInPixels(1) - left - left); //width in px
			int height = Math.round(row.getHeightInPoints() - top - 10/*pt*/); //height in pt
	
			drawImageOnExcelSheet((XSSFSheet)sheet, 2, 1, left, top, width, height, pictureIdx);
	
			wb.write(new FileOutputStream("D:/ExcelDrawImagesOverCell.xlsx"));
			wb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// Writing Image on Excel 
	public static void drawImageOnExcelSheet(XSSFSheet sheet
			, int row
			, int col
			, int left/*in px*/
			, int top/*in pt*/
			, int width/*in px*/
			, int height/*in pt*/
			, int pictureIdx) throws Exception{

		CreationHelper helper = sheet.getWorkbook().getCreationHelper();
		Drawing drawing = sheet.createDrawingPatriarch();
		ClientAnchor anchor = helper.createClientAnchor();
		anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);

		anchor.setCol1(col); //first anchor determines upper left position
		anchor.setRow1(row);
		anchor.setDx1(Units.pixelToEMU(left)); //dx = left in px
		anchor.setDy1(Units.toEMU(top)); //dy = top in pt

		anchor.setCol2(col); //second anchor determines bottom right position
		anchor.setRow2(row); 
		anchor.setDx2(Units.pixelToEMU(left + width)); //dx = left + wanted width in px
		anchor.setDy2(Units.toEMU(top + height)); //dy= top + wanted height in pt

		drawing.createPicture(anchor, pictureIdx);
	}

}

 

์ถœ์ฒ˜ : https://yjacket.tistory.com/66

 

poi ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์ž‘๋‹ค๋ฉด, 

๋ฌผ๋ก  row ๊ฐœ์ˆ˜๊ฐ€ ์—„์ฒญ ๋งŽ์€ ํŒŒ์ผ๋„ ๊ฐ€๋” ์กด์žฌํ•˜๋‚˜, 

์†๋„๊ฐ€ ์กฐ๊ธˆ ๋А๋ฆด ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ํ˜„์žฌ๋Š” ๋‹ค์šด๋กœ๋“œ๋ฐ›์•„ ๋†“๋Š” ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง์„ ๋Œ€๋น„ํ•ด์„œ ํŒŒ์ผ ํฌ๊ธฐ๋Š” ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์‚ฌ์šฉ.

 

pom.xml ์ฃผ์˜์‚ฌํ•ญ : 
    poi /// poi-ooxml 2๊ฐœ ๋ฒ„์ ผ์ด ๋™์ผํ•ด์•ผ ํ•จ.

์‹คํ–‰ ๊ฒฐ๊ณผ : 

 

์ž…๋ ฅ ํ™•์ธ.

 

320x100
๋ฐ˜์‘ํ˜•