poi excel

 

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>



import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class ExcelUtil {

    public static class Value {
        private String value;
        private Short color;

        public Value(String value) {
            this.value = value;
        }

        public Value(String value, Short color) {
            this.value = value;
            this.color = color;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public Short getColor() {
            return color;
        }

        public void setColor(Short color) {
            this.color = color;
        }
    }

    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, Value[][] values, HSSFWorkbook wb) {
        HSSFSheet sheet = wb.createSheet(sheetName);
        for (int i = 0; i < title.length; i++) {
            sheet.setColumnWidth(i, 20 * 256);
        }
        HSSFRow row = sheet.createRow(0);
        HSSFFont headFont = wb.createFont();
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        headFont.setColor(HSSFColor.WHITE.index);
        HSSFCellStyle headCellStyle = wb.createCellStyle();
        headCellStyle.setFont(headFont);//选择需要用到的字体格式
        headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        headCellStyle.setFillForegroundColor(HSSFColor.BLACK.index);
        headCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        for (int i = 0; i < title.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(headCellStyle);
        }

        HSSFFont redFont = wb.createFont();
        redFont.setColor(HSSFColor.RED.index);
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                HSSFCell cur = row.createCell(j);
                if (values[i][j] == null || StringUtils.isBlank(values[i][j].getValue())) {
                    continue;
                }
                cur.setCellValue(values[i][j].getValue());
                if (ObjectUtils.equals(values[i][j].getColor(), HSSFColor.RED.index)) {
                    HSSFCellStyle cellStyle = wb.createCellStyle();
                    cur.setCellStyle(cellStyle);
                    cellStyle.setFont(redFont);
                }
            }
        }
        return wb;
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值