java写入数据到excel

转自:https://blog.csdn.net/gaopeng096/article/details/83055702

所需pom 

<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>

代码 

  public void writeExcel(){
                //开始写入excel,创建模型文件头
                String[] titleA = {"id","Cgonghao","Cname"};        
                //创建Excel文件,B库CD表文件
                File fileA = new File("e:/TestFile.xls");
                if(fileA.exists()){
                    //如果文件存在就删除
                    fileA.delete();
                }
                try {
                    fileA.createNewFile();
                    //创建工作簿
                    WritableWorkbook workbookA = Workbook.createWorkbook(fileA);            
                    //创建sheet
                    WritableSheet sheetA = workbookA.createSheet("sheet1", 0);
                    Label labelA = null;            
                    //设置列名
                    for (int i = 0; i < titleA.length; i++) {
                        labelA = new Label(i,0,titleA[i]);
                        sheetA.addCell(labelA);    
                    }            
                    //获取数据源
                    for (int i = 1; i < 10; i++) {
                        labelA = new Label(0,i,i+" ");
                        sheetA.addCell(labelA);
                        labelA = new Label(1,i,"20180101" + i);
                        sheetA.addCell(labelA);
                        labelA = new Label(2,i,"开发00"+i);
                        sheetA.addCell(labelA);
                    }
                    workbookA.write();    //写入数据        
                    workbookA.close();  //关闭连接
                    logger.info("成功写入文件,请前往E盘查看文件!");
 
                } catch (Exception e) {
                    logger.info("文件写入失败,报异常...");
                }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值