【poi第二节】poi读取excel.java读取excel

x33g5p2x  于2021-12-28 转载在 其他  
字(1.5k)|赞(0)|评价(0)|浏览(479)

poi读取excel.java读取excel

  1. import org.apache.poi.hssf.usermodel.HSSFCell;
  2. import org.apache.poi.hssf.usermodel.HSSFRow;
  3. import org.apache.poi.hssf.usermodel.HSSFSheet;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  6. import org.apache.poi.ss.usermodel.Workbook;
  7. import java.io.FileInputStream;
  8. import java.io.InputStream;
  9. /**
  10. * @ClassName 类名:ExcelDemo2
  11. * @Author作者: hzh
  12. * @Date时间:2018/12/4 9:48
  13. * excel读取
  14. **/
  15. public class ExcelDemo2 {
  16. public static void main(String[] args) throws Exception{
  17. InputStream inputStream = new FileInputStream("D:\\file\\工作薄.xls");
  18. POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
  19. HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
  20. HSSFSheet hssfSheet = wb.getSheetAt(0); //获取第一个sheet
  21. if(hssfSheet == null){
  22. return;
  23. }
  24. //遍历行row
  25. for(int rowNum=0;rowNum<=hssfSheet.getLastRowNum();rowNum++){
  26. HSSFRow hssfRow = hssfSheet.getRow(rowNum);
  27. if(hssfRow == null){
  28. continue;
  29. }
  30. //遍历列cell
  31. for(int cellNum=0;cellNum<=hssfRow.getLastCellNum();cellNum++){
  32. HSSFCell hssfCell = hssfRow.getCell(cellNum);
  33. if(hssfCell == null){
  34. continue;
  35. }
  36. System.out.print(" "+getValue(hssfCell));
  37. }
  38. System.out.println();
  39. }
  40. }
  41. /**
  42. * 值类型转化
  43. * @param hssfCell
  44. * @return
  45. */
  46. private static String getValue(HSSFCell hssfCell){
  47. if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
  48. return String.valueOf(hssfCell.getBooleanCellValue());
  49. }else if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
  50. return String.valueOf(hssfCell.getNumericCellValue());
  51. }else {
  52. return hssfCell.getStringCellValue();
  53. }
  54. }
  55. }

相关文章