java poi之Excel的读取

xiaoxiao2021-02-28  2

poi之Excel的读取

package com.imooc.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Poi_Read_Excel { /** * * @param args */ public static void main(String[] args) { File file=new File("D:/jxl_poi_text.xls"); try { FileInputStream stream=new FileInputStream(file); HSSFWorkbook workbook=new HSSFWorkbook(stream); //HSSFSheet sheet=workbook.getSheet("Sheet0"); HSSFSheet sheet=workbook.getSheetAt(0); int firstRowNum=0; //获取sheet中最后一行行号 int lastRowNum=sheet.getLastRowNum(); for (int i = firstRowNum; i < lastRowNum; i++) { HSSFRow row=sheet.getRow(i); //获取当前行最后单元格列号 int lastCellNum=row.getLastCellNum(); for (int j = 0; j < lastCellNum+1; j++) { HSSFCell cell=row.getCell((short) j); String value=cell.getStringCellValue(); System.out.print(value+" "); } System.out.println(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-1750240.html

最新回复(0)