Java代码
简单的Excle提数操作
此代码可以直接运行,文件地址需要改成自己本地的文件地址,通过row.getCell();这个方法获取行列里的数值,个人理解主要是通过循环来遍历数据,打印在控制台。
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();
}
}
上面操作是读取Excle上的数据然后进行加减各种操作。
控制台打印出来之后,再更新到Excle上面,这里应该是有一个方法的,大概率是使用Stream输入输出流来实现,但是我当时没有整出来,所以还有一个笨方法。
可以先把控制台输出的数据转一下格式,整成一个html文件,html文件是一条一条的,符合Excle的格式,txt的不符合,可以用txt上替换一下符号啥的,那个转Excle上有具体的操作,通过txt文件转成Excle文件。
这样就可以实现提数操作的实现啦。