Excel data POI operation data

Java code

Simple Excel operation of extracting numbers

This code can be run directly. The file address needs to be changed to your own local file address through row.getCell(); this method obtains the values ​​in the rows and columns. My personal understanding is mainly to traverse the data through a loop and print it on the console.

package com.guo;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.n3r.core.collection.RMap;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

public class FileReader {
    
    

    String PATH = "D:\\Desktop\\";

    @Test
    public void test() throws Exception {
    
    
            //获取文件流
            FileInputStream fis = new FileInputStream(PATH+"20230601_B301_callsMonthApi_Y200_0001.xlsx");
            //获取一个工作簿
            Workbook workbook = new XSSFWorkbook(fis);

            //获取一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            //获取第一行内容
            Row row = sheet.getRow(0);
            if (row != null){
    
    
                int Cells = row.getPhysicalNumberOfCells();
                    Cell cell = row.getCell(1);
                    Cell cell1 = row.getCell(4);
                if (cell != null && cell1 != null){
    
    
                    String title = cell.getStringCellValue();
                    String title1 = cell1.getStringCellValue();
                    System.out.println(title+" | "+title1);
                    int rowCount = sheet.getPhysicalNumberOfRows();
                    Map map = new HashMap();
                    for (int rowNum = 1;rowNum < rowCount;rowNum++){
    
    
                        Row rowData = sheet.getRow(rowNum);
                        if (rowData != null){
    
    
                            Cell cellData = rowData.getCell(1);
                            Cell cellData1 = rowData.getCell(4);
                            if (cellData != null && cellData1 != null){
    
    
                                String cellKey = cellData.getStringCellValue();
                                String cellValue = cellData1.getStringCellValue();
                                if (!StringUtils.isEmpty(RMap.getStr(map,cellKey))){
    
    
                                    String str = RMap.getStr(map, cellKey);
                                    int i = Integer.parseInt(str);
                                    int i1 = Integer.parseInt(cellValue);
                                    int result = i+i1;
                                    map.put(cellKey,result);
                                }else {
    
    
                                    map.put(cellKey,cellValue);
                                }
                            }
                        }
                    }
                    System.out.print(map);
                }
            }
            fis.close();
        }
    }

The above operation is to read the data on Excel and then perform various addition and subtraction operations.
After the console prints it out, update it to Excel. There should be a method here. It is most likely to be implemented using the Stream input and output stream, but I didn't figure it out at the time, so there is another stupid method.
You can first convert the data output from the console into an html file. The html file is line by line and conforms to the format of Excel. The txt does not. You can replace the symbols in txt and convert it to Excel. The specific operation is to convert the txt file into an Excel file.
In this way, the number lifting operation can be realized.

Guess you like

Origin blog.csdn.net/lj20020302/article/details/133140345