java获取excel的.xlsx格式的具体某一列

来源:互联网 发布:武义网络电视 编辑:程序博客网 时间:2024/05/19 01:11

import org.apache.poi.ss.usermodel.Cell;
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;


public static void main(String[] args) throws IOException {

String path="C:\\Users\\admin\\Desktop\\测试.xlsx";
InputStream iStream=new FileInputStream(path);
// .xlsx
XSSFWorkbook workbook=new XSSFWorkbook(iStream);
//循环每一页,并处理当前页
for (XSSFSheet xssfSheet : workbook) {
//处理当前页循环读取每一行
if (xssfSheet==null) {
continue;
}
for (int runNum =0; runNum <xssfSheet.getLastRowNum();runNum++) {
XSSFRow row=xssfSheet.getRow(runNum);
int minColIx=row.getFirstCellNum();
int maxColIx=row.getLastCellNum();
//遍历该行,获取每个cell元素
for (int colIx = minColIx; colIx <maxColIx; colIx++) {
XSSFCell cell=row.getCell(colIx);
  cell.setCellType(Cell.CELL_TYPE_STRING);
  //获取指定的一列
  if (cell.getStringCellValue().equals("3")) {
for (int runNum1 =1; runNum1 <xssfSheet.getLastRowNum();runNum1++) {
XSSFRow row1=xssfSheet.getRow(runNum1);
//遍历该行,获取每个cell元素
XSSFCell cell2=row1.getCell(0);
  cell2.setCellType(Cell.CELL_TYPE_STRING);
  System.out.println(cell2.getStringCellValue());
}
}else {
continue;
}
  

}

}

}
}
阅读全文
0 0
原创粉丝点击