java生成Excel

依赖

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
    </dependencies>

代码

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ExcelTest {

    public static void main(String[] args) {
        ExcelTest excelTest=new ExcelTest();
        excelTest.createExcel();
    }

    public void createExcel(){
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("mySheet");
        sheet.setDefaultColumnWidth(20);// 默认列宽
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0); //此代码只生成一行数据

        //单元格样式
        HSSFCellStyle cellStyle = wb.createCellStyle(); // 单元格样式
        //字体样式
        Font fontStyle = wb.createFont();
        fontStyle.setBold(true); // 加粗
        fontStyle.setFontName("黑体"); // 字体
        fontStyle.setFontHeightInPoints((short) 11); // 大小
        fontStyle.setColor(Font.COLOR_RED);//颜色
        //字体样式添加到单元格样式中
        cellStyle.setFont(fontStyle);

        //单元格风格
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//左右居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中

        //单元格背景色
        cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        for(int i=0;i<3;i++){
            //生成单元格
            HSSFCell cell=row.createCell((int)i);
            //单元格内容
            cell.setCellValue(i);
            //设置单元格样式
            cell.setCellStyle(cellStyle);
        }

        //合并单元格
        CellRangeAddress cra =new CellRangeAddress(0, 3, 2, 2); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra);

        //输出Excel文件
        FileOutputStream output=null;
        try {
            output= new FileOutputStream("F:\\学习\\workbook.xls");
            wb.write(output);
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(output!=null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

效果图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值