POI Excel 스타일 설정

3299 단어 코드 세그먼트
/*
  • Copyright 2002-2019 Petro-CyberWorks Information Technology Co. Ltd. All rights reserved.
  • PCITC PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.


  • */
    package com.pcitc.scl.business.productoil.util.excel;
    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Iterator;
    import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.http.MediaType;
    /**
  • @description

  • @author sufangfang
  • @version 1.0,2019년 2월 22일 */public class Test {public static void main(String [] args) {File newFile = Excelutil.createNewFile("경영관리-판매관리-수출판매.xlsx");
     //  , *****************************************************
     InputStream is = null;
     XSSFWorkbook workbook = null;
     XSSFSheet sheet = null;
     try {
         is = new FileInputStream(newFile);//  excel 
         workbook = new XSSFWorkbook(is);//  workbook,
         //  sheet
         sheet = workbook.getSheetAt(0);
     } catch (Exception e1) {
         e1.printStackTrace();
     }
    
     if (sheet != null) {
         try {
             //  
             FileOutputStream fos = new FileOutputStream(newFile);
             XSSFRow row = sheet.getRow(0);
             if (row == null) {
                 row = sheet.createRow(0);
             } else {
                 Iterator it = row.cellIterator();
                 while (it.hasNext()) {
                     Cell cell = it.next();
                     cell.setCellStyle(buildHeaderStyle(workbook));
                 }
             }
             XSSFCell cell = row.getCell(0);
             if (cell == null) {
                 cell = row.createCell(0);
             }
             workbook.write(fos);
             fos.flush();
             fos.close();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 if (null != is) {
                     is.close();
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }
    
    }public static XSSFCellStyle buildHeaderStyle(XSSFWorkbook 워크북) {XSSFCellStyle = workbook.createCellStyle();style.setAlignment.CENTER);//가로로 가운데 style.setVerticalAlignment(VerticalAlignment.CENTER);//세로로 가운데//테두리 스타일을 설정합니다.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN);//글꼴 스타일 XSSFont 글꼴 = workbook.createFont(); font.setBold(true);//글꼴을 굵게 하다.setColor(Font.COLOR_NORMAL);//검은색 글꼴.setFontName("흑체");font.setFontHeight(12);//글꼴 크기 스타일.setFont(font); return style; }

  • }

    좋은 웹페이지 즐겨찾기