ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] ์—‘์…€ ํŒŒ์ผ(.xlsx) ์“ฐ๊ธฐ / ์Šคํƒ€์ผ ์ ์šฉ
    DEV ๐Ÿ’™/JAVA 2020. 3. 17. 21:34
    ๋ฐ˜์‘ํ˜•

     

    โ–ถ previous

    1 POI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ & ํ”„๋กœ์ ํŠธ์— ์ ์šฉ
    2 ์—‘์…€ํŒŒ์ผ(.xlsx) ์ฝ์–ด์˜ค๊ธฐ & list์— ์ €์žฅ

     

    [ ์—‘์…€ํŒŒ์ผ(.xlsx) ์“ฐ๊ธฐ(์ƒ์„ฑ)/์ €์žฅ/์Šคํƒ€์ผ ์ ์šฉ ]

     

    1 Font (๊ธ€๊ผด)

    XSSFFont Font = workbook.createFont();
    → ํฐํŠธ ์Šคํƒ€์ผ ์ƒ์„ฑ
    Font.setFontHeightInPoints((short)14);
    → ๊ธ€์ž ํฌ๊ธฐ
    Font.setFontName("๋ง‘์€ ๊ณ ๋”•");
    → ๊ธ€๊ผด ์ง€์ •
    Font.setBold(true);
    → ์ง„ํ•˜๊ฒŒ(true)

    Style.setFont(Font);
    → ๋งŒ๋“ค์–ด๋†“์€ CellStyle์— ํฐํŠธ ์ ์šฉ

     


     

    2 CellStyle

    CellStyle Style = workbook.createCellStyle();
    → ์…€ ์Šคํƒ€์ผ ์ƒ์„ฑ
    Style.setAlignment(HorizontalAlignment.CENTER);
    → ๊ฐ€์šด๋ฐ์ •๋ ฌ
    Style.setVerticalAlignment(VerticalAlignment.CENTER);
    → ์„ธ๋กœ ๊ธฐ์ค€ ๊ฐ€์šด๋ฐ์ •๋ ฌ
    Style.setBorderTop(BorderStyle.THIN);
    Style.setBorderBottom(BorderStyle.THIN);
    Style.setBorderLeft(BorderStyle.THIN);
    Style.setBorderRight(BorderStyle.THIN);
    → ๊ฐ€๋Š”์„  ํ…Œ๋‘๋ฆฌ

    Style.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
    Style.setFillPattern(FillPAtternType.SOLID_FOREGROUND);
    → ์…€ ๋ฐฐ๊ฒฝ์ƒ‰ ์ง€์ • (์–˜๋„ค ๋‘˜์€ ์Ž—๋šœ์Ž—๋šœ)

    cell.setCellStyle(Style);
    → ์…€์— ์Šคํƒ€์ผ ์ ์šฉ

     


     

    3 writeExcel() ์—‘์…€ ์ž‘์„ฑ, ์ €์žฅ

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    → workbook & sheet ์ƒ์„ฑ

    curRow = sheet.createRow(n);

    → n ํ–‰ ์ƒ์„ฑ
    cell = curRow.createCell(n);

    → ์ƒ์„ฑ๋œ nํ–‰(curRow)์— ์…€ ์ƒ์„ฑ
    cell.setCellValue("...");

    → ์ƒ์„ฑ๋œ ์…€์— ๊ฐ’ ๋„ฃ๊ธฐ

    sheet.setColumnWidth(index, ๋„ˆ๋น„);
    → ๋„ˆ๋น„ ์ง€์ •

    workbook.write(fileout);

    fileout.close();
    → ์ž‘์„ฑ ํ›„ ์ €์žฅ, ์ข…๋ฃŒ

    public void writeExcel(ArrayList<exData> list) {
    	String path = Work4.class.getResource("").getPath();	//์ €์žฅํ•  ํŒŒ์ผ ๊ฒฝ๋กœ
    		
    	try {
    		File file = new File(path + "ExcelExFile.xlsx");
    		FileOutputStream fileout = new FileOutputStream(file);
    			
    		XSSFWorkbook xworkbook = new XSSFWorkbook();
    			
    		XSSFSheet xsheet = xworkbook.createSheet("๋ฉ”๊ฐ€๋ฐ•์Šค");		//์‹œํŠธ ์ƒ์„ฑ
    		XSSFRow curRow;
    			
    		int row = list.size();		//๋ฆฌ์ŠคํŠธ ํฌ๊ธฐ
    		Cell cell = null;
    			
    		//Title
    		curRow = xsheet.createRow(0);
    		cell = curRow.createCell(0);
    		cell.setCellValue("์ „๊ตญ ๋ฉ”๊ฐ€๋ฐ•์Šค ์ง€์ ");
    			
    		//Head
    		curRow = xsheet.createRow(1);
    		cell = curRow.createCell(0);
    		cell.setCellValue("์ด๋ฆ„");
    			
    		cell = curRow.createCell(1);
    		cell.setCellValue("๋น„์œจ");
    			
    		cell = curRow.createCell(2);
    		cell.setCellValue("%");
    			
    		//Body
    		for(int i=2;i<row;i++){
    			curRow = xsheet.createRow(i); // row ์ƒ์„ฑ
    				
    			cell = curRow.createCell(0);
    			cell.setCellValue(list.get(i).getName());
                    
    			//์ด๋ฆ„์ด '๋™ํƒ„'์ง€์ ์˜ ํ–‰์— PointStyle ์ ์šฉ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
                    
    			cell = curRow.createCell(1);
    			cell.setCellValue(list.get(i).getNum());
                    
    			//์…€์Šคํƒ€์ผ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
                    
    			cell = curRow.createCell(2);
    			cell.setCellValue(list.get(i).getPer());
                    
    			//์…€์Šคํƒ€์ผ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
    		}
    			
    		//์—ด ๋„ˆ๋น„ ์„ค์ •
    		for(int i=0;i<3;i++) {
    			xsheet.autoSizeColumn(i);
    			xsheet.setColumnWidth(i, (xsheet.getColumnWidth(i))+256);
    		}
    			
    		xworkbook.write(fileout);
    		fileout.close();
    			
    	} catch(FileNotFoundException e) {
    		e.printStackTrace();
    	} catch(IOException e) {
    		e.printStackTrace();
    	}
    }

     


    wirteExcel() ์ „์ฒด ์ฝ”๋“œ

    public void writeExcel(ArrayList<exData> list) {
    	String path = Work4.class.getResource("").getPath();	//์ €์žฅํ•  ํŒŒ์ผ ๊ฒฝ๋กœ
    		
    	try {
    		File file = new File(path + "ExcelExFile.xlsx");
    		FileOutputStream fileout = new FileOutputStream(file);
    			
    		XSSFWorkbook xworkbook = new XSSFWorkbook();
    			
    		//Title
    		XSSFFont TitleFont = xworkbook.createFont();
    		TitleFont.setFontHeightInPoints((short)14);
    		TitleFont.setFontName("๋ง‘์€ ๊ณ ๋”•");
    		TitleFont.setBold(true);
    			
    		CellStyle TitleStyle = xworkbook.createCellStyle();
    		TitleStyle.setAlignment(HorizontalAlignment.CENTER);
    		TitleStyle.setFont(TitleFont);
    			
    		//Body
    		XSSFFont BodyFont = xworkbook.createFont();
    		BodyFont.setFontHeightInPoints((short)11);
    		BodyFont.setFontName("๋ง‘์€ ๊ณ ๋”•");
    			
    		CellStyle BodyStyle = xworkbook.createCellStyle();
    		BodyStyle.setAlignment(HorizontalAlignment.CENTER);
    		BodyStyle.setBorderTop(BorderStyle.THIN);
    		BodyStyle.setBorderBottom(BorderStyle.THIN);
    		BodyStyle.setBorderLeft(BorderStyle.THIN);
    		BodyStyle.setBorderRight(BorderStyle.THIN);
    		BodyStyle.setFont(BodyFont);
    			
    		//Point
    		CellStyle PointStyle = xworkbook.createCellStyle();
    		PointStyle.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
    		PointStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    		PointStyle.setAlignment(HorizontalAlignment.CENTER);
    		PointStyle.setBorderTop(BorderStyle.THIN);
    		PointStyle.setBorderBottom(BorderStyle.THIN);
    		PointStyle.setBorderLeft(BorderStyle.THIN);
    		PointStyle.setBorderRight(BorderStyle.THIN);
    		PointStyle.setFont(BodyFont);
    			
    			//Header
    		XSSFFont HeadFont = xworkbook.createFont();
    		HeadFont.setBold(true);
    		HeadFont.setFontHeightInPoints((short)11);
    		HeadFont.setFontName("๋ง‘์€ ๊ณ ๋”•");
    			
    		CellStyle HeadStyle = xworkbook.createCellStyle();
    		HeadStyle.setAlignment(HorizontalAlignment.CENTER);
    		HeadStyle.setFillForegroundColor(HSSFColorPredefined.GREY_25_PERCENT.getIndex());
    		HeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    		HeadStyle.setBorderTop(BorderStyle.THIN);
    		HeadStyle.setBorderBottom(BorderStyle.THIN);
    		HeadStyle.setBorderLeft(BorderStyle.THIN);
    		HeadStyle.setBorderRight(BorderStyle.THIN);
    		HeadStyle.setFont(HeadFont);
    			
    		XSSFSheet xsheet = xworkbook.createSheet("๋ฉ”๊ฐ€๋ฐ•์Šค");		//์‹œํŠธ ์ƒ์„ฑ
    		XSSFRow curRow;
    			
    		int row = list.size();		//๋ฆฌ์ŠคํŠธ ํฌ๊ธฐ
    		Cell cell = null;
    			
    		//Title
    		curRow = xsheet.createRow(0);
    		cell = curRow.createCell(0);
    		cell.setCellValue("์ „๊ตญ ๋ฉ”๊ฐ€๋ฐ•์Šค ์ง€์ ");
    		cell.setCellStyle(TitleStyle);
    			
    		//Head
    		curRow = xsheet.createRow(1);
    		cell = curRow.createCell(0);
    		cell.setCellValue("์ด๋ฆ„");
    		cell.setCellStyle(HeadStyle);
    			
    		cell = curRow.createCell(1);
    		cell.setCellValue("๋น„์œจ");
    		cell.setCellStyle(HeadStyle);
    			
    		cell = curRow.createCell(2);
    		cell.setCellValue("%");
    		cell.setCellStyle(HeadStyle);
    			
    		//Body
    		for(int i=2;i<row;i++){
    			curRow = xsheet.createRow(i); // row ์ƒ์„ฑ
    				
    			cell = curRow.createCell(0);
    			cell.setCellValue(list.get(i).getName());
                    
    			//์…€์Šคํƒ€์ผ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
                    
    			cell = curRow.createCell(1);
    			cell.setCellValue(list.get(i).getNum());
                    
    			//์…€์Šคํƒ€์ผ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
                    
    			cell = curRow.createCell(2);
    			cell.setCellValue(list.get(i).getPer());
                    
    			//์…€์Šคํƒ€์ผ
    			if(list.get(i).getName().equals("๋™ํƒ„")) {
    				cell.setCellStyle(PointStyle);
    			} else{ cell.setCellStyle(BodyStyle); }
    		}
    			
    		//์—ด ๋„ˆ๋น„ ์„ค์ •
    		for(int i=0;i<3;i++) {
    			xsheet.autoSizeColumn(i);
    			xsheet.setColumnWidth(i, (xsheet.getColumnWidth(i))+256);
    		}
    			
    		xworkbook.write(fileout);
    		fileout.close();
    			
    	} catch(FileNotFoundException e) {
    		e.printStackTrace();
    	} catch(IOException e) {
    		e.printStackTrace();
    	}
    }
    ๋ฐ˜์‘ํ˜•
Designed by Tistory.