poi根据列头查询列序号

poi 文章 2022-08-22 10:16 683 0 全屏看文

AI助手支持GPT4.0

原理就是先取指定的有列头的行,然后比对行内容。

代码如下:

public static void main(String[] args) throws IOException {
	int wmsNo = getCellIndexByCellValue("WMS出货单号");
	System.out.println(wmsNo);
}

private static int getCellIndexByCellValue(String cellName) {
	Sheet s = ExcelUtil.getReader(excelPath).getSheets().get(1); // 自己修改sheet所在页
	Row r = s.getRow(1); // 自己修改列头所在的行数
	short minColIx = r.getFirstCellNum();
	short maxColIx = r.getLastCellNum();
	int cellNum = -1;
	for (short colIx = minColIx; colIx < maxColIx; colIx++) {
		Cell cell = r.getCell(colIx);
		if (cell.getStringCellValue().trim().equals(cellName)) {
			cellNum = colIx;
			break;
		}
	}
	return cellNum;
}

比如取:

image.png


程序输出结果就是:

image.png

对应下哈 

A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7)

果然H就是第7列了

-EOF-

AI助手支持GPT4.0