Java创建Excel并插入值代码记录

package com.test;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Gouzao {
    public static String tables = "学生的信息";
    public static String outputFile = "D:\\工程认证\\15级成绩录入\\15级课程成绩单\\计算机网络原理\\随机数.xls";
    public static int i=0;
    public static List<Shiyan> list=new ArrayList<>();
    public static Integer total=40;
    public static void excel(List<Shiyan> list) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            int rowNum = 1;
            try {
                HSSFSheet sheet = workbook.createSheet(tables);
                try {
                    HSSFRow row1 = sheet.createRow(0);
                    for (int i = 1; i <= 10; i++) {
                        sheet.autoSizeColumn(i);
                        HSSFCell cell1 = row1.createCell(i - 1);
                        cell1.setCellValue("作业"+i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                while (i<total) {
                    HSSFRow row = sheet.createRow(rowNum);
                    for (int j = 0; j < 10; j++) {
                        HSSFCell cell = row.createCell(j);
                        sheet.autoSizeColumn(i);
                        if(j==0){
                            cell.setCellValue(list.get(i).getS1());
                        }else if(j==1){
                            cell.setCellValue(list.get(i).getS2());
                        }else if(j==2){
                            cell.setCellValue(list.get(i).getS3());
                        }else if(j==3){
                            cell.setCellValue(list.get(i).getS4());
                        }else if(j==4){
                            cell.setCellValue(list.get(i).getS5());
                        }else if(j==5){
                            cell.setCellValue(list.get(i).getS6());
                        }else if(j==6){
                            cell.setCellValue(list.get(i).getS7());
                        }else if(j==7){
                            cell.setCellValue(list.get(i).getS8());
                        }else if(j==8){
                            cell.setCellValue(list.get(i).getS9());
                        }else if(j==9){
                            cell.setCellValue(list.get(i).getS10());
                        }
                    }
                    rowNum++;
                    i++;
                }
            }catch (Exception e1) {
                e1.printStackTrace();
            }
            FileOutputStream fOut = new FileOutputStream(outputFile);
            workbook.write(fOut);
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void jisun(Integer input[]){
        for (int i=0;i<input.length;i++){
            while(true){
                int s5max=5;
                int s5min=0;
                Random random = new Random();
                int s1 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s2 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s3 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s4 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s9 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s10 = random.nextInt(s5max)%(s5max-s5min+1) + s5min;
                int s20max=20;
                int s20min=5;
                int s5 = random.nextInt(s20max)%(s20max-s20min+1) + s20min;
                int s15max=15;
                int s15min=5;
                int s7 = random.nextInt(s15max)%(s15max-s15min+1) + s15min;
                int s8= random.nextInt(s15max)%(s15max-s15min+1) + s15min;
                int s6=input[i]-s1-s2-s3-s4-s5-s8-s7-s9-s10;
                if(s6<0){
                    continue;
                }else if(s6>20){
                    continue;
                }else{
                    Shiyan shiyan=new Shiyan( s1,  s2,  s3,  s4,  s5,  s6,  s7,  s8,  s9,  s10);
                    list.add(shiyan);
                    System.out.println(i+1+"--"+input[i]+"--"+shiyan);
                    break;
                }
            }
        }
        Gouzao.excel(list);
    }
   public static void main(String[] args){
       Integer a[]=new Integer[total];
       Scanner sc = new Scanner(System.in);
       for (int i=0;i<total;i++){
           int t = sc.nextInt();
           a[i]=t;
           System.out.println(i+1);
       }
       Gouzao.jisun(a);
   }
}

package com.test;

public class Shiyan {
    private Integer s1;
    private Integer s2;
    private Integer s3;
    private Integer s4;
    private Integer s5;
    private Integer s6;
    private Integer s7;
    private Integer s8;
    private Integer s9;
    private Integer s10;

    public Shiyan(Integer s1, Integer s2, Integer s3, Integer s4, Integer s5, Integer s6, Integer s7, Integer s8, Integer s9, Integer s10) {
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
        this.s4 = s4;
        this.s5 = s5;
        this.s6 = s6;
        this.s7 = s7;
        this.s8 = s8;
        this.s9 = s9;
        this.s10 = s10;
    }

    public Integer getS1() {
        return s1;
    }

    public void setS1(Integer s1) {
        this.s1 = s1;
    }

    public Integer getS2() {
        return s2;
    }

    public void setS2(Integer s2) {
        this.s2 = s2;
    }

    public Integer getS3() {
        return s3;
    }

    public void setS3(Integer s3) {
        this.s3 = s3;
    }

    public Integer getS4() {
        return s4;
    }

    public void setS4(Integer s4) {
        this.s4 = s4;
    }

    public Integer getS5() {
        return s5;
    }

    public void setS5(Integer s5) {
        this.s5 = s5;
    }

    public Integer getS6() {
        return s6;
    }

    public void setS6(Integer s6) {
        this.s6 = s6;
    }

    public Integer getS7() {
        return s7;
    }

    public void setS7(Integer s7) {
        this.s7 = s7;
    }

    public Integer getS8() {
        return s8;
    }

    public void setS8(Integer s8) {
        this.s8 = s8;
    }

    public Integer getS9() {
        return s9;
    }

    public void setS9(Integer s9) {
        this.s9 = s9;
    }

    public Integer getS10() {
        return s10;
    }

    public void setS10(Integer s10) {
        this.s10 = s10;
    }

    @Override
    public String toString() {
        return "Shiyan{" + "s1=" + s1 + ", s2=" + s2 + ", s3=" + s3 + ", s4=" + s4 + ", s5=" + s5 + ", s6=" + s6 + ", s7=" + s7 + ", s8=" + s8 + ", s9=" + s9 + ", s10=" + s10 + '}';
    }
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值