POI 设置Excel样式

/*

  • 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<Cell> 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 workbook) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER); //横向居中
    style.setVerticalAlignment(VerticalAlignment.CENTER); //竖向居中
    //设置边框
    style.setBorderBottom(BorderStyle.THIN);
    style.setBorderLeft(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    //字体样式
    XSSFFont font = workbook.createFont();
    font.setBold(true); //加粗
    font.setColor(Font.COLOR_NORMAL);//黑色
    font.setFontName(“黑体”);
    font.setFontHeight(12); //字体大小
    style.setFont(font);
    return style;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值