ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] Apache Poi 파워포인트 표 만들기
    JAVA 2022. 11. 21. 09:37
    728x90
    728x90

    기존에 생성되었던 ppt파일에 표를 추가하는 예제를 작성해보겠다.

     

    프로그램의 순서
    1. 원본 ppt 파일을 불러온다.
    2. 표를 삽입한다.
    3. ppt를 저장한다.

     

    String original_filePath = "C:\\Users\\Downloads\\originalFile.pptx";
    
    try {
    	// 원본 ppt 불러오기
    	XMLSlideShow original_ppt = new XMLSlideShow(new FileInputStream(original_filePath));
    	List<XSLFSlide> slides = original_ppt.getSlides();
    	XSLFSlide slide = slides.get(0);
    	
    	// 표 생성하는 함수
            createTable(slide);
    	
            // ppt 저장하기
    	String modify_filePath = "C:\\Users\\Downloads\\modifyFile.pptx";
    	FileOutputStream out = new FileOutputStream(modify_filePath);
    	original_ppt.write(out);
    	original_ppt.close();
    	out.close();
    
    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    } catch (Exception e) {
    	e.printStackTrace();
    }

     

     

    표 생성 함수
    private static void createTable(XSLFSlide slide) throws Exception {
            // 표에 들어갈 데이터
    	ArrayList<Torque> torques = getTorqueData("KV-AS142-040-KOR-C", "--6");
    	
        	// 표 생성하기
    	XSLFTable torqueTable = slide.createTable();
        	// 표 위치시키기
    	torqueTable.setAnchor(new Rectangle(860, 0, 190, 166));
    	
    	int numColumns = 3;
    	
    	// header 생성
    	XSLFTableRow th = torqueTable.addRow();
    	th.setHeight(10d);
        
        	// header Cell 배경(하늘색), Border 설정(4면 모두 생성)
    	for(int i = 0; i < numColumns; i++) {
    		XSLFTableCell cell = th.addCell();
    		cell.setFillColor(new Color(187, 224, 227));
    		cell.setBorderColor(BorderEdge.top, new Color(0, 0, 0));
    		cell.setBorderColor(BorderEdge.right, new Color(0, 0, 0));
    		cell.setBorderColor(BorderEdge.bottom, new Color(0, 0, 0));
    		cell.setBorderColor(BorderEdge.left, new Color(0, 0, 0));
    		cell.setBorderWidth(BorderEdge.top, 1.0);
    		cell.setBorderWidth(BorderEdge.right, 1.0);
    		cell.setBorderWidth(BorderEdge.bottom, 1.0);
    		cell.setBorderWidth(BorderEdge.left, 1.0);
    		
    		// Cell 정렬
    		XSLFTextParagraph paragraph = cell.addNewTextParagraph();
    		paragraph.setTextAlign(TextAlign.CENTER);
    		
    		// Font 설정
    		XSLFTextRun textRun = paragraph.addNewTextRun();
    		textRun.setBold(true);
    		textRun.setFontColor(new Color(0, 0, 0));
    		textRun.setFontSize(9.0);
    		
    	}
    	
        	// header width 
    	torqueTable.setColumnWidth(0, 50);
    	torqueTable.setColumnWidth(1, 70);
    	torqueTable.setColumnWidth(2, 70);
    	
        	// header 컬럼명
    	torqueTable.getCell(0, 0).getTextParagraphs()
        		.get(0).getTextRuns().get(0).setText("Col1");
    	torqueTable.getCell(0, 1).getTextParagraphs()
        		.get(0).getTextRuns().get(0).setText("Col2");
    	torqueTable.getCell(0, 2).getTextParagraphs()
        		.get(0).getTextRuns().get(0).setText("Col3");
    	
    	// table Row
    	for(int i = 0; i < torques.size(); i++) {
    		Torque torque = torques.get(i);
    		XSLFTableRow tr = torqueTable.addRow();
    		tr.setHeight(15d);
    		
            	// table Row Cell 배경(흰색), Border 설정(4면 모두 생성)
    		for(int j = 0; j < numColumns; j++) {
    			XSLFTableCell cell = tr.addCell();
    			cell.setFillColor(new Color(255, 255, 255));
    			cell.setBorderColor(BorderEdge.top, new Color(0, 0, 0));
    			cell.setBorderColor(BorderEdge.right, new Color(0, 0, 0));
    			cell.setBorderColor(BorderEdge.bottom, new Color(0, 0, 0));
    			cell.setBorderColor(BorderEdge.left, new Color(0, 0, 0));
    			cell.setBorderWidth(BorderEdge.top, 1.0);
    			cell.setBorderWidth(BorderEdge.right, 1.0);
    			cell.setBorderWidth(BorderEdge.bottom, 1.0);
    			cell.setBorderWidth(BorderEdge.left, 1.0);
                
                		// Cell 수평 정렬
    			cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
    			
                		// Cell 수직 정렬
    			XSLFTextParagraph paragraph = cell.addNewTextParagraph();
    			paragraph.setTextAlign(TextAlign.CENTER);
    			
    		}
    			
            	// 데이터 삽입
                	// (XSLFTable 객체, row index, column index, 데이터, 폰트 크기)
    		writeText(torqueTable, i + 1, 0, "data1", 9d);
    		writeText(torqueTable, i + 1, 1, "data2", 9d);
    		writeText(torqueTable, i + 1, 2, "data3", 9d);
    	}
    	
    }

     

    writeText 함수

    private static void writeText(XSLFTable table, int intRowNo, int intColNo, String strValue,  double fontSize, Insets2D insets) throws Exception {
    	
    	XSLFTableCell cell = null;
    	try {
    		cell = table.getCell(intRowNo, intColNo);
    	
    		if (cell != null) 
    		{
    			cell.setText("");
    			XSLFTextRun aTextRun = cell.appendText(value, false);
    			aTextRun.setFontSize(fontSize);
    			cell.appendText("", false);
    		}
    	}
    	catch (Exception err) {
    		throw err;
    	}												
    }

     

    표 삽입된 모습

    728x90
    728x90
Designed by Tistory.