java程序中excel的导出和导入

读写Excel的常用三种技术

  1. POI(Apache)
  2. JXL
  3. FASTEXCEL
    在这里插入图片描述
    在这里插入图片描述

用JXL生成excel

  1. 导包:由于我是用maven的,所以我就到pom加入:
<dependency>
          <groupId>net.sourceforge.jexcelapi</groupId>
          <artifactId>jxl</artifactId>
          <version>2.6.10</version>
      </dependency>
  1. java代码:
    public static void main(String[] args){
        //设置表头
        String[] title = {"id","name","sex"};
        //创建excel文件
        File file = new File("d:/jxl_text.xls");
        try {
            file.createNewFile();
            //创建工作簿
            WritableWorkbook workBook = Workbook.createWorkbook(file);
            WritableSheet sheet = workBook.createSheet("Sheet1",0);
            Label label = null;
            //第一行设置列名,第一行的时候for循环中的i要为0
            for (int i = 0;i<title.length;i++){
                label = new Label(i,0,title[i]);//第几列:i,第几行:0,值:title[i]
                sheet.addCell(label);
            }
            //追加数据
            for (int i = 1;i<10;i++){
                label = new Label(0,i,"a"+1);//第几列:0,第几行:i,值:a1
                sheet.addCell(label);
                label = new Label(1,i,"user"+1);//第几列:1,第几行:i,值:user1
                sheet.addCell(label);
                label = new Label(2,i,"男");//第几列:2,第几行:i,值:男
                sheet.addCell(label);
            }
            //写入数据
            workBook.write();
            //关闭流
            workBook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (WriteException e){
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值