ExcelUtil

 Java代码 复制代码 收藏代码

  1. package org.swj.site.util;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7.  
  8. import org.apache.poi.xssf.usermodel.XSSFRow;
  9. import org.apache.poi.xssf.usermodel.XSSFSheet;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11.  
  12. public class ExcelUtil
  13. {
  14. public static void readExcel(String excelFileName,int sheetIndex,int rowNum)
  15. {
  16. XSSFWorkbook wb;
  17. try
  18. {
  19. wb = new XSSFWorkbook(new FileInputStream(excelFileName));
  20. XSSFSheet sheet = wb.getSheetAt(sheetIndex);
  21. int totalRows = sheet.getLastRowNum();
  22. for(int i = rowNum; i <= totalRows; i++) {
  23. XSSFRow row = sheet.getRow(i);
  24. if(row == null) {
  25. continue;
  26. }
  27. int totalCells = row.getLastCellNum();
  28. System.out.println(totalCells);
  29. for(int j=0;j < totalCells;j++){
  30. System.out.println(row.getCell(j).toString());
  31. }
  32. }
  33. }
  34. catch (FileNotFoundException e)
  35. {
  36. e.printStackTrace();
  37. }
  38. catch (IOException e)
  39. {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. public static void readExcel(File excelFile,int sheetIndex,int rowNum)
  45. {
  46. XSSFWorkbook wb;
  47. try
  48. {
  49. wb = new XSSFWorkbook(new FileInputStream(excelFile));
  50. XSSFSheet sheet = wb.getSheetAt(sheetIndex);
  51. int totalRows = sheet.getLastRowNum();
  52. for(int i = rowNum; i <= totalRows; i++) {
  53. XSSFRow row = sheet.getRow(i);
  54. if(row == null) {
  55. continue;
  56. }
  57. int totalCells = row.getLastCellNum();
  58. System.out.println(totalCells);
  59. for(int j=0;j < totalCells;j++){
  60. System.out.println(row.getCell(j).toString());
  61. }
  62. }
  63. }
  64. catch (FileNotFoundException e)
  65. {
  66. e.printStackTrace();
  67. }
  68. catch (IOException e)
  69. {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74.  
  75. public static void main(String[] args)
  76. {
  77. readExcel("D:\\test.xlsx",2,1);
  78. }
  79. }  

你可能感兴趣的:(S)