java向excel里面写数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hexu_blog/article/details/74334434

1---------导入包

      commons-collections4-4.1.jar

        poi-3.17-beta1.jar
        poi-examples-3.17-beta1.jar
        poi-excelant-3.17-beta1.jar
        poi-ooxml-3.17-beta1.jar
        poi-ooxml-schemas-3.17-beta1.jar
        poi-scratchpad-3.17-beta1.jar
        xmlbeans-2.6.0.jar


2----------写

    //创建路径
        File file=new File("e:\\share.xlsx");
        //创建工作本
        XSSFWorkbook xw = new XSSFWorkbook();
        //创建工作簿
        XSSFSheet sheet=xw.createSheet("sheet1");
        //得到第一行
        XSSFRow row=sheet.createRow(0);
        Scanner sc=new Scanner(System.in);
        //创建第一列
        XSSFCell cell=row.createCell(0);
        cell.setCellValue("学号");
        //创建第二列
        XSSFCell cell2=row.createCell(1);
        cell2.setCellValue("姓名");
        //创建第三列
        XSSFCell cell3=row.createCell(2);
        cell3.setCellValue("年龄");
        //循环输入表格类容
        for(int i=1;i<=2;i++){
            System.out.print("请输入编号:");
            String id=sc.next();
            System.out.print("请输入名字:");
            String name=sc.next();
            System.out.print("请输入年龄:");
            String age=sc.next();
            //添加内容

            row = sheet.createRow(i);
            //创建第一列
            XSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(id);
            //创建第二列
            XSSFCell cell22 = row.createCell(1);
            cell22.setCellValue(name);
            //创建第三列
            XSSFCell cell32 = row.createCell(2);
            cell32.setCellValue(age);
        }
        //写入
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            xw.write(out);
            out.close();
            System.out.println("写入成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



3-----------excel里面的效果


     学号    姓名    年龄    
      1    aa    11    
      2    bb    22

   

猜你喜欢

转载自blog.csdn.net/hexu_blog/article/details/74334434