Java 는 Intellij Idea 로 Execel 파일 을 읽 고 계산 한 후 새 파일 을 기록 합 니 다.

Java 는 Intellij Idea 로 Execel 파일 을 읽 고 계산 한 후 새 파일 을 기록 합 니 다.
1. Maven 프로젝트 를 새로 만 든 후 pom. xml 파일 에 poi 를 설정 합 니 다 (엑셀 프레임 워 크 읽 기)
       
...    

        
            org.apache.poi
            poi
            3.14
        
        
        
            org.apache.poi
            poi-ooxml
            3.14
        
   

2. 엑셀 파일 경로 가 져 오기 (
D:\\ItelliJ_Idea\\WorkSplace\\smallwalnut\\src\\main\\resources\\(3)_15.xlsx


3. 주요 자바 파일 (kotlin 으로 도 사용 가능)
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;

public class ReadWriteExcel {

    public static void main(String[] argv) {
        ReadWriteExcel readWriteExcel = new ReadWriteExcel();
        try {
            readWriteExcel.readInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void readInfo() throws Exception {
        //Excel  
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File("D:\\ItelliJ_Idea\\WorkSplace\\smallwalnut\\src\\main\\resources\\(3)_15.xlsx")));//      
        for (int j = 0; j < wb.getNumberOfSheets(); j++) {
            //    
            XSSFSheet sheet = wb.getSheetAt(j);
            String sheetName = sheet.getSheetName();
            System.out.println("sheet=" + j + ",sheetName=" + sheetName);

            //        , 0     
            if (sheetName.equals("Sheet1")) {
                DecimalFormat df = new DecimalFormat("0.00");//     ,    0
                for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                    XSSFRow row = sheet.getRow(i);
                    XSSFCell codeCell = row.getCell(0);
                    if (codeCell != null) {
                        try {
                            if (i == 0) { //       ,     ,        7,8,9      
                                row.createCell(7).setCellValue("new7");
                                row.createCell(8).setCellValue("new8");
                                row.createCell(9).setCellValue("new9");
                                continue;
                            }
                            codeCell.setCellType(XSSFCell.CELL_TYPE_STRING); //        String,                  cell
                            String code = codeCell.getStringCellValue();
                            codeCell.getRow().getRowNum();
                            Double firstS = row.getCell(1).getNumericCellValue();  //        
                            Double secondPercent = row.getCell(2).getNumericCellValue();  //        
                            Double thirdDelay = row.getCell(3).getNumericCellValue();  //        

                            caculateThirdDate(j, code, firstS, secondPercent, thirdDelay,row,df);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        //   
        FileOutputStream fileOut = new FileOutputStream(new File("D:\\ItelliJ_Idea\\WorkSplace\\smallwalnut\\src\\main\\resources\\(3)_16.xlsx"));//      

        //  workbook      
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
        wb.close();
    }

    private void caculateThirdDate(int j, String name, Double firstS, Double secondPercent, Double thirdDelay, XSSFRow row, DecimalFormat df) {
        double firstSC = firstS * 1024;
        double speedScore = 100;
        if (firstS >= 6) {
            speedScore = 100;
        } else if (firstS >= 4 && firstS < 6) {
            speedScore = (double) (100 - 90) / (6 - 4) * (firstS - 4) + 90;
        } else if (firstS >= 2 && firstS < 4) {
            speedScore = (double) (90 - 80) / (4 - 2) * (firstS - 2) + 80;
        } else if (firstS >= 1 && firstS < 2) {
            speedScore = (double) (80 - 70) / (2 - 1) * (firstS - 1) + 70;
        } else if (firstSC >= 512 && firstSC < 1024) {
            speedScore = (double) (70 - 60) / (1024 - 512) * (firstSC - 512) + 60;
        } else if (firstSC >= 128 && firstSC < 512) {
            speedScore = (double) (60 - 30) / (512 - 128) * (firstSC - 128) + 30;
        } else if (firstSC < 128) {
            speedScore = 0;
        }

        int successScore = 100;
        if (secondPercent >= 0.98) {
            successScore = 100;
        } else if (secondPercent >= 0.96 && secondPercent < 0.98) {
            successScore = (int) ((100 - 90) / (0.98 - 0.96) * (secondPercent - 0.96) + 90);
        } else if (secondPercent >= 0.94 && secondPercent < 0.96) {
            successScore = (int) ((90 - 80) / (0.96 - 0.94) * (secondPercent - 0.94) + 80);
        } else if (secondPercent >= 0.92 && firstS < 0.94) {
            successScore = (int) ((80 - 70) / (0.94 - 0.92) * (secondPercent - 0.92) + 70);
        } else if (secondPercent >= 0.9 && secondPercent < 0.92) {
            successScore = (int) ((70 - 60) / (0.92 - 0.9) * (secondPercent - 0.9) + 60);
        } else if (secondPercent >= 0.8 && secondPercent < 0.9) {
            successScore = (int) ((60 - 30) / (0.9 - 0.8) * (secondPercent - 0.8) + 30);
        } else if (secondPercent < 0.8) {
            successScore = 0;
        }


        double delayScore = 0;
        if (thirdDelay <= 1000) {
            delayScore = 100;
        } else if (thirdDelay > 1000 && thirdDelay <= 2000) {
            delayScore = ((double)(100 - 90) / (1000 - 2000) * (thirdDelay - 2000)) + 90;
        } else if (thirdDelay > 2000 && thirdDelay <= 3000) {
            delayScore = (double) (90 - 80) / (2000 - 3000) * (thirdDelay - 3000) + 80;
        } else if (thirdDelay > 3000 && thirdDelay <= 4000) {
            delayScore = (double) (80 - 70) / (3000-4000) * (thirdDelay - 4000) + 70;
        } else if (thirdDelay > 4000 && thirdDelay <= 5000) {
            delayScore = (double) (70 - 60) / (4000 - 5000) * (thirdDelay - 5000) + 60;
        } else if (thirdDelay > 5000 && thirdDelay <= 8000) {
            delayScore = (double) (60 - 30) / (5000 - 8000) * (thirdDelay - 8000) + 30;
        } else{
            delayScore = 0;
        }



//  7,8,9     
        row.createCell(7).setCellValue(df.format(speedScore));
        row.createCell(8).setCellValue(successScore);
        row.createCell(9).setCellValue(df.format(delayScore));


        System.out.println("name=" + name +",sheet=" + j + ",firstS = " + firstS + ",firstSC=" + firstSC
                + ",secondPercent=" + secondPercent + ",thirdDelay=" + thirdDelay
                + ",speedScore=" + speedScore + ",successScore=" + successScore
                + ",delayScore=" + delayScore
        );

    }



}

PS: 구체 적 인 파일 기록 방식 은 자 유 롭 게 맞 출 수 있 습 니 다.

좋은 웹페이지 즐겨찾기