1、读取xlsx 文件数据
package com.wuhuhuan.util;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
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 class testread {
public static void main(String [] args){
File file = new File("d:/xssf.xlsx");
try {
XSSFWorkbook workbook = new XSSFWorkbook(FileUtils.openInputStream(file));
XSSFSheet sheet = workbook.getSheetAt(0);
int firstrowNum = 0;
int lastRowNum = sheet.getLastRowNum();
for(int i = firstrowNum;i<lastRowNum;i++){
XSSFRow row = sheet.getRow(i);
int lastcellNum = row.getLastCellNum();
for(int j = 0;j<lastcellNum;j++){
XSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value+" ");
}
System.out.println("");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、读取xls 文件数据
package com.wuhuhuan.util;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
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 PoiReadExcel {
public static void main(String [] args){
File file = new File("d:/aa.xls");
try {
//创建excel读取文件内容
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
//获取第一个工作表
// HSSFSheet sheet = workbook.getSheet("Sheet0");
//读取默认第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum = 0;
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;j++){
HSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value+" ");
}
System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}