1、引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
2、java 读 excel(基础操作)
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class CreateDwdTableDemo {
public static void main(String[] args) {
listEverySheet();
}
public static void listEverySheet() {
try {
FileInputStream fileInputStream = new FileInputStream(new File("./input/策划域.xlsx"));
Workbook workbook = new XSSFWorkbook(fileInputStream);
int k = workbook.getNumberOfSheets();
Sheet sheet = workbook.getSheetAt(1);
int rowSize = sheet.getLastRowNum() + 1;
System.out.println("总行数: " + rowSize);
int columnSize = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println("总列数: " + columnSize);
for (int i = 1; i < rowSize; i++) {
Row row = sheet.getRow(i);
for (int j = 4; j <= 9; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
System.out.print(cell.toString() + "\t");
System.out.print(cell.getStringCellValue() + "\t");
}
}
System.out.println();
}
workbook.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、待整理
public static void processEverySheet() {
try {
FileInputStream fileInputStream = new FileInputStream(new File("./input/new.xlsx"));
Workbook workbook = new XSSFWorkbook(fileInputStream);
int k = workbook.getNumberOfSheets();
Sheet sheet1 = workbook.getSheetAt(0);
int rowSize = sheet1.getLastRowNum() + 1;
System.out.println("总行数: " + rowSize);
int columnSize = sheet1.getRow(0).getPhysicalNumberOfCells();
System.out.println("总列数: " + columnSize);
Cell newCellTitleLast = sheet1.getRow(0).createCell(sheet1.getRow(0).getLastCellNum());
newCellTitleLast.setCellValue("是否刷单");
boolean flag = false;
for (int i = 1; i < rowSize; i++) {
System.out.println("第" + i + "行!!!");
Row row = sheet1.getRow(i);
double d8L = Double.parseDouble(row.getCell(7).toString());
double d9L = Double.parseDouble(row.getCell(8).toString());
String d11L = row.getCell(10).toString();
String[] strArray = d11L.split(",");
double[] doubleArray = new double[strArray.length];
double maxNum = Double.MIN_VALUE;
double d14D = 99.9;
Cell d14DCell = row.getCell(13);
if (d14DCell != null) {
String d14dStr = d14DCell.toString();
if (d14dStr != "") {
d14D = Math.abs(Double.parseDouble(d14dStr));
}
}
for (int j = 0; j < strArray.length; j++) {
doubleArray[j] = Double.parseDouble(strArray[j]);
if (doubleArray[j] != d9L) {
if (doubleArray[j] > maxNum) {
maxNum = doubleArray[j];
}
}
}
if (d9L > maxNum * 3.0 || d8L / d9L > 0.9 || d14D > 30) {
flag = true;
} else {
flag = false;
}
Cell newCell = row.createCell(row.getLastCellNum());
newCell.setCellValue(flag);
}
FileOutputStream fileOut = new FileOutputStream("./output/new1.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("over!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}