java创建简单Excel文件并赋值

1.先引用maven坐标

 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
    </dependency>

2.编写程序

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Classname Test
 * @Description TODO
 * @Date 2021/3/16 17:10
 * @Created by Administrator
 */

public class Test {

    public static void main(String[] args) {


        try {
          //  for (int i = 0; i < 10; i++) {
                new Test().CellTest();
         //   }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }


    public void  CellTest() throws Exception{
        Date date  = new Date();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
        //创建一个文件对象
        Workbook book = new HSSFWorkbook();
        //设置一个文件名称
        Sheet sheet = book.createSheet("第一个sheet页");
        //创建一个单元格对象
        Row row = sheet.createRow(0);
        row.createCell(0) .setCellValue("java");   //第一列
        row.createCell(1).setCellValue("C#");  //第二列
        row.createCell(2).setCellValue("C++");//第三列
        row.createCell(3).setCellValue("python");//第四列
        row.createCell(4).setCellValue("Go Language");//第五列
        row.createCell(5).setCellValue("English");//第六列

        //新建一个文件
        File file = new File("D:\\test.xls");
        FileOutputStream fis =null;
        if(!file.exists()){   //主要为了方便调试
            fis =  new FileOutputStream(file);
            book.write(fis);
            fis.close();
            book.close();
            System.out.println("已经写入>>>>>");
        }
        else {
            file.delete();
            System.out.println("已经删除>>>>>");
        }

    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值