EasyExcel生成Excel文件

目录

推荐博客

实战

一、添加依赖

二、创建excel文件列格式

 三、设置表头和内容样式

四、执行

删除Excel一行


推荐博客

java生成Excel文件_Oliviajingxiang的博客-CSDN博客_java生成excel文件

实战

一、添加依赖

        ​​​​<!-- 使用 xls 格式-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.6</version>
        </dependency>

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

        <!-- 使用 xlsx 格式-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

二、创建excel文件列格式

package com.upxcy.exceldemo;

import com.alibaba.excel.annotation.ExcelProperty;

public class MarketDataExcel {
    @ExcelProperty("指数代码")
    private String indexCode;
    
    @ExcelProperty("日期")
    private String date;
    
    @ExcelProperty("点位")
    private double close;

    public MarketDataExcel() {
    }

    public MarketDataExcel(String indexCode, String date, double close) {
        this.indexCode = indexCode;
        this.date = date;
        this.close = close;
    }

    public String getIndexCode() {
        return indexCode;
    }

    public void setIndexCode(String indexCode) {
        this.indexCode = indexCode;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public double getClose() {
        return close;
    }

    public void setClose(double close) {
        this.close = close;
    }
}

 三、设置表头和内容样式

package com.upxcy.exceldemo;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;

public class CellStyleStrategy {
    /**
     * 设置表头和内容样式
     */

    public static HorizontalCellStyleStrategy styleStrategy() {
        //表头样式策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        headWriteCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.index);
        headWriteCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.index);
        headWriteCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.index);
        headWriteCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.index);

        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setBold(false);
        headWriteFont.setFontName("Arial");
        headWriteFont.setFontHeightInPoints((short) 10);
        headWriteCellStyle.setWriteFont(headWriteFont);

        //2 内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        //内容大小
        contentWriteFont.setFontName("Arial");
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);

//        //设置自动换行
//        contentWriteCellStyle.setWrapped(true);
//        //设置垂直居中
//        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        //头默认了FillPatternType所以可以不指定
//        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//        //设置水平居中
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//
//        //设置边框样式
//        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

        return new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle);
    }
}

四、执行

public static void main(String[] args) {
        ArrayList<MarketDataExcel> marketDataExcels = new ArrayList<>();
        EasyExcel.write("D:\\excelTest.xlsx",MarketDataExcel.class)
                .registerWriteHandler(CellStyleStrategy.styleStrategy())
                .sheet("Market Data").doWrite(marketDataExcels);
    }

删除Excel一行

java使用poi操作excel删除一整行_年轻就是天堂的博客-CSDN博客_java删除excel行

PS:

//xls 格式
HSSFWorkbook workbook = new HSSFWorkbook(is);

//xlsx 格式
XSSFWorkbook workbook = new XSSFWorkbook(is);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值