package cn.com.lzt.excel; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import cn.com.lzt.excel.entity.HGLExcelResult; public class HGLExcelImportUtils { /** * 读取出filePath中的所有数据信息 * @param filePath excel文件的绝对路径 * */ public static HGLExcelResult getDataFromExcel(InputStream fis,int titlerows,int headrows) { HGLExcelResult result = new HGLExcelResult(); Workbook wookbook = null; try { wookbook = new XSSFWorkbook(fis);//得到工作簿 } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } //得到一个工作表 Sheet sheet = wookbook.getSheetAt(0); //获得表头 Row rowHead = sheet.getRow(titlerows + headrows - 1 ); for(int i = 0; i < rowHead.getLastCellNum();i++) { if(null != rowHead.getCell(i)) result.getHeaderList().add(rowHead.getCell(i).getStringCellValue()); } //获得数据的总行数 int totalRowNum = sheet.getLastRowNum(); boolean bEnd = false; //获得所有数据 for(int i = titlerows+headrows ; i <= totalRowNum ; i++) { //获得第i行对象 Row row = sheet.getRow(i); List record = new ArrayList(); for(int j = 0; j < result.getHeaderList().size();j++) { if(row.getCell(j) == null) { bEnd = true; break; } record.add(row.getCell(j).getStringCellValue()); } if(bEnd) break; result.dataList.add(record); } return result; } public static String getCheckMsg(InputStream fis,int sheetNum,int checkRow,int checkCol ) { String result = ""; Workbook wookbook = null; try { wookbook = new XSSFWorkbook(fis);//得到工作簿 } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } try { //得到一个工作表 Sheet sheet = wookbook.getSheetAt(sheetNum); //获得表头 Cell checkCell = sheet.getRow(checkRow).getCell(checkCol); result = checkCell.getStringCellValue(); }catch(Exception e) { result = "false"; } return result; } }