代码
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
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.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ReadExcel {
public static void main(String[] args) throws IOException, InvalidFormatException {
try {
String fileName = "C:/Users/lenovo/Desktop/XX.xlsx";
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(fileName));
XSSFSheet xSheet = xwb.getSheetAt(3);
int count = xSheet.getLastRowNum();
for (int i = 0; i <= count; i++) {
if (xSheet.getRow(i) == null) {
continue;
}
Row row = xSheet.getRow(i);
Object list6 = boolType(row.getCell(6, MissingCellPolicy.RETURN_BLANK_AS_NULL));
Object list7 = boolType(row.getCell(7, MissingCellPolicy.RETURN_BLANK_AS_NULL));
for (int j = 0; j <= count; j++) {
XSSFCell l2 = xSheet.getRow(j).getCell(2);
XSSFRow row222 = xSheet.getRow(j);
Object list2 = boolType(row222.getCell(2, MissingCellPolicy.RETURN_BLANK_AS_NULL));
boolean isok = list6.toString().equals(list2.toString());
if (isok == true) {
l2.setCellValue(list7.toString());
System.out.println(j + " j > list2=" + list2.toString() + " ? list6=" + list6.toString() + " = "
+ list6.toString().equals(list2.toString()));
}
}
System.out.println("i > " + i + " list7=" + list7);
if (i == 2268)
break;
}
FileOutputStream out = new FileOutputStream(fileName);
xwb.write(out);
out.close();
System.out.println("<--------修改完成-------->");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object boolType(Cell cell) {
Object val = "";
switch (cell.getCellType()) {
case _NONE:
break;
case BLANK:
break;
case BOOLEAN:
val = cell.getBooleanCellValue();
break;
case ERROR:
val = cell.getErrorCellValue();
break;
case FORMULA:
val = cell.getCellFormula();
break;
case NUMERIC:
val = (int) cell.getNumericCellValue();
break;
case STRING:
val = cell.getStringCellValue();
break;
}
return val;
}
}
依赖包
点我下载
commons-collections4-4.4.jar
commons-compress-1.20.jar
dom4j-1.6.1.jar
poi-3.7.jar
poi-4.1.2.jar
poi-ooxml-3.7.jar
poi-ooxml-4.1.2.jar
poi-ooxml-schemas-3.7.jar
poi-ooxml-schemas-4.1.2.jar
xmlbeans-3.1.0.jar