POI讀取指定Excel中行與列的數據



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.InputStream; public class PoiExcelTest { public static XSSFWorkbook workbook; // 工作簿 public static XSSFSheet sheet; // 工作表 public static XSSFRow row; // public static XSSFCell cell; // public static void main(String[] args) throws Exception{ String fielName = "E:\\ExcelTest.xlsx"; String sheetName = "AI研發中心"; readExcelData(fielName,sheetName,2,1); readExcelData(fielName,sheetName,2,2); } public static void readExcelData(String fielName,String sheetName,int rownum,int cellnum) throws Exception{ InputStream in = new FileInputStream(fielName); workbook = new XSSFWorkbook(in); sheet = workbook.getSheet(sheetName); row = sheet.getRow(rownum); cell = row.getCell(cellnum); switch (cell.getCellType()){ case XSSFCell.CELL_TYPE_NUMERIC: System.out.println("第"+(rownum+1)+"行"+"第"+(cellnum+1)+"列的值: "+String.valueOf(cell.getNumericCellValue())); break; case XSSFCell.CELL_TYPE_STRING: System.out.println("第"+(rownum+1)+"行"+"第"+(cellnum+1)+"列的值: "+cell.getStringCellValue()); break; default: System.out.println("第"+(rownum+1)+"行"+"第"+(cellnum+1)+"列的值: "+cell.getStringCellValue()); break; } } }

Excel數據:

讀取的數據:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM